sort data before processing because of running total
sort data before processing because of running total
Hi,
in my datatable i am having a runnung total like
{ data: "BUC_OPBETRAG", title: "runningsum",
render: function (data, type, row, meta ) {
if (meta.row == 0) {
zwisu = Number(data);
return (Number(data));
} else {
zwisu = zwisu + Number(data);
return (zwisu);
}
}
},
which works well .But the data is not displayed in the order it is processed, so the running sum makes no sense at all.
I would have to order data - I believe befor it is processed - by the fields "calenderweek" and "source" - how can I achieve this?
Thanks
Max
Answers
You'd need to do it before it is fed into DataTables. Are you Ajax loading the data or something else? If you could link to a test case showing how you are operating DataTables I'd be able to give a direct answer.
An alternative might be something like this example., but only if you don't care about the actual index assigned to a row, since that is applied regardless of order.
Allan
Hi Allan,
I can not post a test case because we are in a safe environment....very sorry for that.
yes I get the data via ajax.
I also added a "running number" like this:
also this starts with 36, all numbers are mixed through.
So how can I achieve "You'd need to do it before it is fed into DataTables."? I tried to order them on the sql server, but that also does not work....
Thanks
Max
The test case doesn't have to have your actual data since the problem is specific to your data. The test case just needs to represent what you are trying to do. I adapted Allan's example to show how to sum a column based on the order applied.
https://live.datatables.net/yidodabi/1/edit
If you want this to run only once then move the code inside the event handler to
initComplete
.If you still need help then please build a simple test case that has an example of your data. If its easier create a Javascript sourced test case like this example. Also specify the order you want the summation to happen.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Are you client-side or server-side processing with this table (
serverSide
)?I've been re-reading the question, and I'm not yet sure I 100% understand the goal. Do you want to always force the table to sort by
calenderweek
andsource
and then show an index column based on that order? Is the end user able to do apply custom ordering?Or do you want the index to be in
calenderweek / source
order, and then the end user can order in any order they want (which would cause the index to be out of order, unless it was clicked on)?Allan
Hi Kevin, Hi Allan,
no, the user does not need custom ordering, so far Kevins solutions works perfect.
But (I seem do have a tendency to complicated tables) this table also has rowGroups. In the rowgroups I show the total of these rows (works), but now I would also need the last running total ("Zwischensumme") of this group! Aim is to see how much money will be left after each week.
my rowgroup is defined:
I'm not quite getting it - sorry. Could you modify the image to show what would be the expected values?
I'm not certain that what you are looking for will be possible, the grouping levels don't have a concept of higher levels, which I think you would need, but I'm not certain of.
Allan
here you go.
The idea is to have a cash-flow-planning. In the column "Offener Betrag" I see what is paid per week, in "Zwischensumme" I see the sum of all payments up to that week. I have two ideas how to get that value: either to take the value out of the last row of each group (if groups are inserted after all rows have been calculated), or (if groups are inserted while normal rows are also produced) taking the value out of Kevins calculation?
Thanks a lot for your effort!
I'm not totally clear what you are after either. However if you are wanting to get the values from the parent group then I would look at creating a new variable, similar to the
collapsedGroups
variable that keeps track of the summed values for the group level. Then you can access them usingtop
orparent
variables as appropriate.Maybe you can build a simple test case that shows a sample of what you have. Then we can collaborate on a solution.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I think I get it now based on your mocked up screenshot, but I'm afraid that is not something that DataTables will do. You could do it in
draw
and spin over the rows in the table, performing the calculations required, updating the cells, but it would not allow for search or ordering of that column.Allan
Are you using Editor? In case you are you can manipulate the data quite easily server side and also implement complex sorting etc.
All you need to do is to manipulate "data" on "postGet".
In this example I add a total server side:
Here is another one with more complex sorting, elimination of duplicates and filtering. (Editor cannot do SELECT DISTINCT and ORDER BY - so I implement this using PHP).
Just to give you an idea how flexible this can be.
Thanks for all the great inut. In the end I found a completely different solution. I set up a function I call in init-complete and after every table.draw. Works perfect.
Nice one! Thanks for sharing your solution with us.
Allan