fnfootercallback, sum column after filter

fnfootercallback, sum column after filter

SpringerSpringer Posts: 3Questions: 0Answers: 0
edited June 2010 in General
In the example for percentages it will do the math over the entire table or only the current page.
http://datatables.net/examples/advanced_init/footer_callback.html

var iTotalMarket = 0;
for ( var i=0 ; i

Replies

  • JustinWJustinW Posts: 1Questions: 0Answers: 0
    I'm trying to do the same thing. I need to sum a column after a filter. Like you, I can only find how to sum a page or the whole table.

    Any progress with this?
  • allanallan Posts: 63,540Questions: 1Answers: 10,476 Site admin
    Great question :-). There are a lot of ways of doing this, and ultimately I want to build a plug-in to do it, but your prompt has inspired me to make a couple of small modifications to DataTables 1.9 - in particular the underscore function ( http://datatables.net/docs/DataTables/1.9.beta.3/#_ ). The underscore function will get data from the table based on the selector, however it did only do it for TR elements. I've just modified it a bit to allow TD/TH elements as well, so what you can now do is something as simple as:

    [code]
    $('#example').dataTable()._('td:nth-child(4)')
    [/code]

    That will give you an array of data from the 4th column which you can sum as you would do any other array :-)

    The underscore function has a second argument which might prove useful as well so you can do things such as:

    [code]
    $('#example').dataTable()._('td:nth-child(4)', {"page": "current"}) // get the data from the current page only
    $('#example').dataTable()._('td:nth-child(4)', {"filter": "applied"}) // get the data from all pages, but only the filtered data
    [/code]

    The ideal of the _ and $ functions in 1.9 is to make working with data and nodes much easier for exactly this kind of manipulation :-).

    Allan
This discussion has been closed.