Footer sum using data attribute
Footer sum using data attribute
in DataTables
Hi, I would like to use the footer function to get the sum of a column. In the column I display the the number with some html style and if I sum the values I get an error. In the cell I put the integer value on two data attributes, "data-sort" and "data-csv" and I would like to use this value to obtain the sum.
I tried to put the name of the data attribute, with and without "data-" and also with "@" but nothing.
ex. pageTotal = api.column(i).data('data-csv')......
Could you help me?
This discussion has been closed.
Answers
column().data()won't have access to the nodes. Trycolumn().nodes()to get thetd. You can then get the data attributes of thetd.Kevin
Thanks for your reply.
I tried to use column().nodes but I don't understand how to use it.
For exemple if I use
api.column( i ).nodes().to$().data('csv')I get only the first row value.
This example show a couple different loops you can use:
http://live.datatables.net/wifavihe/1/edit
One uses jQuery each() and the other uses
reduce()to sum the data value.Kevin
thank but if I use your code inside the footer callback function I get an error
Please provide a test case (update mine if you want) of what you are trying so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I find the solution, thanks for your suggestion!
Here the solution:
$(document).ready( function () {table = $('#example').DataTable({"footerCallback": function ( row, data, start, end, display ) {var api = this.api(), data;col = 0;total = api.column(col, { page: 'current'}).nodes().reduce( function ( a, b ) {return a + $(b).data('attribute');}, 0 );}});http://live.datatables.net/dobapuru/1/