How to Show Comma for thousand in Footer total
How to Show Comma for thousand in Footer total
atataylor
Posts: 35Questions: 7Answers: 0
I am trying to show the comma to show thousands, for example total showing 120000 should be 120,000.
Here is the code amended from the example, (Footer Call back example) but it is not giving me what I want. I would be grateful if anyone could show me who to amend the code.
```
(document).ready(function() {
$('#example').DataTable( {
"paging": true,
"autoWidth": true,
"language": "",
"decimal": ",",
"thousands": ".",
"footerCallback": function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 2 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 2, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 2 ).footer() ).html(
pageTotal +' ( '+ total +' total)'
);
total = api
.column( 3 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 3, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 3 ).footer() ).html(
'£'+pageTotal +' ( £'+ total +' total)'
);
}
} );
} );
This question has accepted answers - jump to:
Answers
See if this thread regarding the use of the builtin number renderer helps. Something like this:
Kevin
Hi Kevin
Thanks for your input, trying to make sense of it. I am completely new to JavaScript and DataTables. Only started to look at JavaScript since giving DataTables a try out.
Here are the docs for the number renderer:
https://datatables.net/manual/data/renderers#Number-helper
I combined the format from the docs in the example Allan provided in this test case:
http://live.datatables.net/gagixafo/1/edit
If you still need help please provide a test case showing an example what you have so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thanks for your help Kevin. The example I am using is from the DataTables site:
Called, Footer callback showing how to get a total at the bottom of the page.
https://datatables.net/examples/advanced_init/footer_callback.html
All I am trying to do is show the comma and turn $3008160 into $3,008,160
I have looked at the examples provided, thanks for that, the combined one is an example without the Footer callback.
I assume its a matter of adding the code:
var numberRenderer = $.fn.dataTable.render.number( ',', '.', 2, '$' ).display;
alert(numberRenderer(123456));
} );
But I am not sure how to. Thanks again.
Took that example and added the number renderer here:
http://live.datatables.net/zohecoyi/1/edit
Kevin
Thanks Kevin, that’s perfect. I was using the function in the wrong place; I need to spend more time learning JavaScript. Thanks for your patience and thanks again for your help
Creating pages with Datatables is how I learned Javascript
Kevin
I am glad you, did sounds like a very practical way to learn, thanks for the tip
Hi Kevin
Could I pick your brains again please? I am trying to add an additional column to the code you provided to show a row number. After some research I have found this code which does what I want:
By adding the extra column in the table and the <th> tags in the header and footer, it works and places the row number in the first column as expected.
However I am unable to combine this with footerCallback code provided in your code above. I thought it would be a case of adding in the code as below:
This combined code prevents DataTables from working.
Here is the full code combined with a small amount of table data:
If I remove either of the code blocks fnRowCallback or footerCallback the remaining code works as expected but not together. Is there away of getting both code blocks working together? Or am I on completely the wrong path?
If you look at the browser's console you will probably a syntax error. You need a comma separating the options, like this:
Kevin
Wow! thanks Kevin, spent nearly two days looking for that missing comma. Your help is much appreciated and thanks for the tip on the browser console.
Thanks kthorngren, great....! work for me, much appreciated for you.