adding a Row-Total Column?

adding a Row-Total Column?

kenirwinkenirwin Posts: 6Questions: 0Answers: 0
edited February 2010 in General
Hi all,

Is there a way to add a final column to a table so that it shows the sum of values foreach row? The "footer" functions work beautifully, and I'm thinking that people like to add their rows as well as their columns. Is there a good way to do this?

This seems like an obvious sort of application, but I don't see it addressed anywhere.

Any ideas?

Thanks,
Ken

Replies

  • kenirwinkenirwin Posts: 6Questions: 0Answers: 0
    Ideally, I'd like to be able to have both a footer row and a footer column in the same table, showing sums both across and down.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    How is your table generated? Is it static data (i.e. rendered from HTML?). If so, could you not just have whatever process is creating the table add the extra column in (ie. the PHP)?

    If you want to do it on the client-side, then you could use fnRowCallback to do basically the same as the footer callback: http://datatables.net/usage/callbacks#fnRowCallback

    Allan
  • kenirwinkenirwin Posts: 6Questions: 0Answers: 0
    Thanks Allan. I'm currently generating the footer rows and columns in PHP, but I can see some advantages to using the jQuery approach (like: the footer sum auto-adjusts when I filter the rows).

    Can two callbacks be used together? Can I call both the fnRowCallback and the fnFooterCallback? If so, where is that sort of syntax documented?

    Thanks! This stuff is great!

    Ken
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi Ken,

    Yes indeed they can be used together - just like all the other DataTables options. It's just a Javascript object, so it would be something like:

    [code]
    $('#example').dataTable( {
    "fnDrawCallback": function (...) { ... },
    "fnRowCallback": function (...) { ... }
    } )
    [/code]
    Allan
  • kenirwinkenirwin Posts: 6Questions: 0Answers: 0
    Fabulous -- thank you!

    Ken
  • asacarterasacarter Posts: 3Questions: 1Answers: 0
    edited May 2011
    I am trying to do something similar but my content is generated by php as static html.

    My final row contains totals but I want it to remain at the bottom and not be sortable.

    I can't find anything in the examples that shows how to do this?

    Thanks
  • DavidsDavids Posts: 2Questions: 0Answers: 0
    Hi asacarter:

    Add something like this in your table html
    [code]

    Total
    1000

    [/code]

    dataTables 1.8 support many rows in the footer section.
  • zzipper7zzipper7 Posts: 2Questions: 0Answers: 0
    Guys, I'm exausted getting a crash course in jquery, I've been at this for weks.
    Can I please get a working example of Rows summed.
  • zzipper7zzipper7 Posts: 2Questions: 0Answers: 0
    Anyone, I'd like to use this tool but I need a little assistance. Again I'm looking for an example of a row SUM. Your help is much appricated. I can make a donation.

    J
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    There is an example of how to do a sum into the footer here: http://datatables.net/release-datatables/examples/advanced_init/footer_callback.html which might help (use fnRowCallback, or fnDrawCallback as I indicated above). If you'd like me to build you a working example, just hit the support link at the top of the page :-) http://datatables.net/support .

    Allan
  • d_vineetd_vineet Posts: 19Questions: 0Answers: 0
    @Davids,
    Thanks a lot for your suggestion of using for total.
    It did the trick.

    --- Vineet
  • michelinnimichelinni Posts: 1Questions: 0Answers: 0
    edited October 2012
    Hi Guys!, I am trying to use the fnFooterCallback to sum the amounts in a colum as a total, the part that I can not figure out yet is, that I need the total for that page wich im getting fine from the aaData, but also the total from the whole set any ideas on how to achieve this on server-side processing(PHP)?

    Ricardo
  • jasonalecjasonalec Posts: 3Questions: 0Answers: 0
    Hey all, Is there a sample code to show how to incorporate the subtotals/grand total rows using server side query? Having a tough time figuring out the syntax and would appreciate anything to get me on the right track. Thanks!!
  • zuckerjzuckerj Posts: 17Questions: 0Answers: 0
    I'm having a curious issue that I've spent MANY hours trying to debug, but I just don't understand what is going on, nor how to fix it.

    I'm using fnFooterCallback to generate Complete Totals and (Page Totals) at the bottom of my columns.

    I'm also using Grouping for the second column in the table, with hiding set to true for that column.

    This was working fine when the first column with a Total was the first column following the grouped hidden column. (4 columns with totals overall).

    This also worked fine when I turned hiding off for that column.

    When I add three more columns WITHOUT totals, just before the first totaled column, with hiding set to false for grouping, EVERYTHING still looks great.

    NOW HERE'S THE WEIRD PART:

    If I then enable hiding, at this point, the first totaled column gets duplicated into the newly added column immediately before it.

    Examples ALL with Grouping:

    [With Col1 hidden] - Works Great!
    Col0 - (Col1) - Col2/Tot2 - Col3/Tot3 - Col4/Tot4 - Col5/Tot5

    [With None hidden] - Works Great!
    Col0 - Col1 - Col2/Tot2 - Col3/Tot3 - Col4/Tot4 - Col5/Tot5

    [With None hidden and added columns] - Works Great!
    Col0 - Col1 - Col2 - Col3 - Col4 - Col5/Tot5 - Col6/Tot6 - Col7/Tot7 - Col8/Tot8

    [With Col1 hidden and added columns] -
    Breaks! Duplicates first totaled column onto preceeding column
    Col0 - (Col1) - Col2 - Col3 - Col4/Tot5 - Col5/Tot5 - Col6/Tot6 - Col7/Tot7 - Col8/Tot8

    Anyone get an epiphany looking at this pattern???

    I've been scouring the code and can't for the life of me figure out why this is happening.

    Joel - Any help VERY APPRECIATED!
  • zuckerjzuckerj Posts: 17Questions: 0Answers: 0
    OK, I solved my issue, but I'm not entirely sure why I had to solve it.

    Apparently the fnFooterCallback is getting executed 3 times before initial display. The first time it appears to execute before the Grouping and the column hiding takes place causing there to be one more footer column than anticipated so the footer values are offset to the left.

    Then it executes again after the Grouping and Column hiding has taken place and overwrites the previous footer values. But of course the first one does get over written because it was off set to the previous column that is not supposed to have a footer value.

    Then a third time it is executed.

    Again, I don't know why DataTables is executing the code three times.

    So, anyway, the solve is simply to use jquery to wipe out all the footer totals whenever the fnFooterCallback function is executed.

    Just thought I'd share.

    Jz.
This discussion has been closed.