Best way to sum in v1.10

Best way to sum in v1.10

norglawnorglaw Posts: 9Questions: 0Answers: 0
edited August 2013 in DataTables 1.9
Hi,

I am calculating sums column by colum on a table with more than 1000 lines and 19 columns.
It takes a few seconds each time, and I'd like it to be fluid.
The trick is that some colums and rows can be hidden, so it can't just be an operation on DOM nodes.
Here the piece of code I use which I guess is not optimized :

[code]
var tabTotal = {};
$(latable.fnSettings().aoData).each(function(index, element) {
for (var index2=0 ; index2

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    > latable.fnSettings()

    If you are using fnSettings, then generally speaking, something has gone wrong somewhere. You should never need to do so.

    In 1.10 by far the best way of calculating a column sum is:

    [code]
    var column0sum = table.column( 0 ).data().reduce( function (a, b) {
    return a + b;
    } );
    [/code]

    Allan
  • norglawnorglaw Posts: 9Questions: 0Answers: 0
    Hi Allan,

    Thx for your help.
    When you write [code]table.column(0)[/code]
    Do you mean ? [code]table.api(true).column(0)[/code]

    Alban
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    If you were to use the `$().dataTable()` initialisation format, then yes. If you use `$().DataTable()` then you get the API instance rather than a jQuery instance.

    Allan
This discussion has been closed.