Sum of columns
Sum of columns
CaptainNemo
Posts: 13Questions: 6Answers: 1
Hi every all,
I made a fiddle to explain the case.
http://live.datatables.net/nudusuto/1/edit?html,js,output
Actually, i made the sum of columns (not all... No goal to make a sum of text :p ) : Salary June / Salary July.
This is OK.
I want to make the sum of salary, for each person. And i want to set the result in the last cell on each row.
And this is not OK :p .
If you can help me...
Thanks !
This question has accepted answers - jump to:
This discussion has been closed.
Answers
http://live.datatables.net/nudusuto/2/edit
Thanks, it's a first step.
In my case, number of columns could be different. In fact, the table is the result of a query, i.e : Display all months of salary, from 2015-02 to 2015-06, or 2013-01 to 2015-01, or etc.
So i can't write in my javascript file specific columns indexes, range month could be different.
And, contrary to column sum, if i use an instruction like "table.cell(3,2).data(5000).draw(), sum column are automatically recalculated, but not sum row with your script proposal ;)
http://live.datatables.net/nudusuto/4/edit?html,js,output
Don't be changing the requirements mid-project :)
If you can put a class in the column headers of the then you should be able to use the createdRow callback to get the values of the selected cells.
Here is how you can do it if each TD has a class on it.
That could be tightened up, but it would be harder to read.
Figuring out how to add the class is left as an exercise for the student. I'm adding the class manually to show that it works.
Re :D
A last thing.
In this fiddle : http://live.datatables.net/nudusuto/6/edit?html,js
sum of rows is OK, sum of column is OK.
Except for the ultimate sum, the sum of all sum :)
In the cell, top-right.
If I click on my button test "Click to change value", the cell (3,3) was modified. The sum of concerned row was recalculated, and column sum too..
But the ultimatum sum stay to 0.
Any idea ?
The last column is summing the data that DataTables knows about (you are using
column().data()
after all) - that data is all zeros for that last column - hence the issue.You need to tell DataTables about the data value change rather than just writing it into the DOM (since DataTables can't know that you've done that). I would suggest calculating the value of the last column in the table using
columns.render
as a function.Allan
Thanks Allan.
If I understand, i define the columns.render option in the Datatables Initialisation, specifying the column concerned. Target -1 in my case.
But no result... I f you can help me...
http://live.datatables.net/nudusuto/7/edit?js,output
http://live.datatables.net/nudusuto/8/edit
The function used for the renderer was returning 0 for whatever reason. I've simplified it a little and it works now.
I've also change the sum loop to use
cells().render()
to get the rendered value.Allan