DataTables with column rendering - Sum of two or more columns

DataTables with column rendering - Sum of two or more columns

psspss Posts: 19Questions: 0Answers: 0
edited January 2013 in General
Hello all,
I'm using mRender to combine the contents of two cells and then hiding the second cell as in http://www.datatables.net/release-datatables/examples/advanced_init/column_render.html. It works fine for me.
Now, I'm wondering if it is possible to store the sum of two or more columns into another column say sum of col1 to col7 into col8.
If yes, then how?
Any help is highly appreciated.
PS : I've already used fnFooterCallback() to add sum of rows in footer, but not able to use that in above context.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited January 2013
    Yes - in mRender: `return row[1] + row[7];` :-)

    Allan
  • psspss Posts: 19Questions: 0Answers: 0
    Hi Allan,
    Thanks for the help.

    But, above method is producing concatenation result instead of sum.

    Link to example:
    http://live.datatables.net/agexap

    Also, I'm trying to store the result of 4 sub-totals in the last column: Cumulative Total, but no luck here also.

    Pls help where I'm doing it wrong. Thanks in advance.
  • psspss Posts: 19Questions: 0Answers: 0
    Well! Finally solved it. It was just related to javascript sum of array element. ;-P

    Here is updated code:
    http://live.datatables.net/etisag
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Good to hear you have it sorted.

    Just a quick note:

    > But, above method is producing concatenation result instead of sum.

    Then you probably would need to add `parseFloat` or `parseInt` as needed.

    Allan
  • psspss Posts: 19Questions: 0Answers: 0
    edited January 2013
    Dear Allan,
    Thank you for your post and help.

    I tried that too earlier. But, (may be) because some of the columns are empty in the table, parseInt was producing NaN, as you can see it in the test case:

    http://live.datatables.net/oleyob
This discussion has been closed.