Custom Sum Function and Paging
Custom Sum Function and Paging
ck1guy
Posts: 10Questions: 1Answers: 0
I have a custom sum function that is simply summing up the values of checked checkboxes. The problem I have is when I page to another page with the datatable pagination, the sum of the values starts over from 0. How do I save the sum of each page and use it as the basis for the sum on subsequent pages.
Here is a Fiddle that illustrates the issue:
http://jsfiddle.net/mnp3dhqh/1/
Thanks!
This discussion has been closed.
Answers
There a couple of things coming together here. You are using jquery to iterate over the data, but JQ only sees what is on the visible page.
The footer call back shows two ways to do a total: total on the page vs total overall.
http://datatables.net/examples/advanced_init/footer_callback.html
But you can't use this approach directly because your data is in a div in the td, so dataTables doesn't see the .data() as a number. If you can figure out how to get the number parsed out of that div, you can use the approach in the footer_callbal example.
Thanks, ThomD. Would it be appropriate to add the value of a checkbox into a hidden column on click, then run the footer callback function on the hidden column? Or, is there a more efficient way to do this?
That approach might result in the shortest possible code - but it should be quite possible to use
table.column( ... ).nodes().reduce( ... )
where the function given toreduce()
will look into the element and pluck out the value needed. That would only be a line or two longer I think.Allan
Thanks, Allan. I appreciate your suggestion. I was unable to get it to work, but that's a me issue, not a plugin issue.
I ended up creating a hidden column to store the checkbox values when the checkbox is clicked. I want to sum the values of this hidden column. The problem is, these values are dynamic, and the footercallback function only sums up this column on the initial table draw.
How do I call the footercallback function when the checkbox has been clicked so I'm incorporating this new value in the sum?
Thanks!
In what why are they dynamic? Calculated by
columns.render
? If so, usecells().render()
to get the rendered data.Allan
Allan, when I say dynamic, I only mean that the contents of the hidden table cells can change depending on whether the checkbox is checked or not. So, The footercallback will have to update each time one of the hidden cells' contents has changed.
I've update my fiddle to illustrate how the table behaves currently. You'll notice that the footercallback is only firing on document ready.
http://jsfiddle.net/tgf59ezr/
I did figure this out. I'll post the solution in a bit.