How to sum values in row groups and display calculated values in a column

How to sum values in row groups and display calculated values in a column

nportnport Posts: 23Questions: 6Answers: 0

http://live.datatables.net/topuhayo/1/edit

I have created "row groups" by using the orderFixed attribute in the datatables initialization (in the example linked I am creating row grouping using the office location values).

I am trying to then sum values in these groups using drawCallback (in this example I am summing the salary values). I then want to display the resulting sum in the empty column so that the summation of salaries for that group can be visible. The reason I am not using RowGroup or something similar is because I want to show these summation values in generated reports (csv, excel, pdf) but the RowGroup values are not exported.

My issue is that I am having trouble figuring out how to put the sum of a row groups salary into the empty column, and I don't know how to handle the case where a row group is spread over multiple pages (I don't want to sum over current page, I want to sum all values for that group)

Any help would be appreciated thank you

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    Answer ✓

    Thanks for the test case. You will need to use Datatables APIs for this. The only rows in the DOM are the rows shown so the jQuery selectors won't work. See this example:
    http://live.datatables.net/haweqaza/1/edit

    It uses cells().every() to loop through all the Office cells. It builds an object data structure to store the totals. Then loops through all the Office cells again to update the Sum column.

    Make sure you don't use the draw() API in the drawCallback or you will cause an infinite loop. Does the data change? Maybe use initComplete if the data is static.

    This solution doesn't require the table to be sorted by the Office column.

    You could use RowGroup and hide the Sum column and have the Sum column exported. See this column selector example. It might be possible to export something similar to the RowGroups. I created this example in this thread to insert the child detail rows into an Excel export. You might be able to do something similar with PDF and CSV but I haven't tried. There are threads on the forum where others have exported the RowGroup info. I haven't looked at the solutions and some may involve updating the buttons library itself.

    Kevin

Sign In or Register to comment.