How to add a total row for some of the columns of data tables
How to add a total row for some of the columns of data tables
amit_100
Posts: 13Questions: 4Answers: 0
I am using this in options :
"footerCallback": function (row, data, start, end, display) {
var api = this.api(), data;
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[, Rs]|(\.\d{2})/g,"")* 1 :
typeof i === 'number' ?
i : 0;
var total2 = api
.column(4)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
$(api.column(4).footer()).html(
' ( $' + total2 + ' total)'
);
};
But is not fired and no sub total for column 4 is shown in data table .
This question has accepted answers - jump to:
This discussion has been closed.
Answers
It looks like your HTML isn't correct. There is no closing '}' to your intVal function at line 5/6.
Try replacing with following:
jr42.gordon : Yes , yo mentioned it write , I need to edit my question . a little bit now .
I had debugged my application where i am using it . I see that :
$(api.column(4).footer()).html(
' ( $' + total2 + ' total)'
);
total2 had the sum of column (4), but it not rendered on the table footer . It is not showing in UI .
Can you please help me with that .
Thanks for your response .
Here is an example using your callback http://live.datatables.net/sadipaji/1/edit?js,output . I believe it works just fine.
this is my changed code as i said '$(api.column(colNo).footer()).html('TOTAL '+ formatMoney(total2,2)+' ₹');' this I can see while debugging but its not shown in table.
I had seen your example, Its working correctly . In my project we had made the .js component of DataTable and we pass some options from our view {where we need to show datatable} . may be your .js component may have some bug .
Thank You Sir .
after lots of debugging, tfoot is coming null from my js component .
thats why "api.column(colNo).footer()" is becoming null .
my component always giving tfoot==null.
is there any setting, through which we can we can enable tfoot always ?
Thanks in advance .
Is tfoot being provided in the HTML? In the test bed I linked to, and example I pulled from, tfoot exists in table HTML declaration.
jr42.gordon : my custom datatable js component , does not have <tfoot> i had modified the component and added tfoot , after adding that . same code snippet worked .
do you have any idea about : https://datatables.net/forums/discussion/35099/how-to-add-custom-html-view-in-header-and-footer-in-printview
If you know please revert .
Thanks for your help