Balance Calculation on Search
Balance Calculation on Search
![ashiquem01](https://secure.gravatar.com/avatar/c91cfed84757d4ceb906dc18327845f2/?default=https%3A%2F%2Fvanillicon.com%2Fc91cfed84757d4ceb906dc18327845f2_200.png&rating=g&size=120)
https://live.datatables.net/laqixaso/1/edit
Please check the last column(balance) of the table.
If Debit have value then Subtract else Sum
How can I achieve this calculation on Search (from bottom to up)
This question has an accepted answers - jump to answer
Answers
Are you trying to provide a sum in the footer? See this example.
Or are you trying to perform calculations on a row by row basis? Use
columns.render
. See this example.If this doesn't help then please provide more details of how you want the calculations performed.
Kevin
Not sum in footer
Columns.render only
But its from up to bottom
In need calculation from bottom top
Can you please refer by balance column data
Row are independent from each other in DataTables. You can't refer to the "next" or "previous" row in a rendering function (due to them being moved about by sorting and filtering).
The closest you'll get is something like this: https://datatables.net/examples/api/counter_columns.html .
Allan
Not indexing
In
coumns.render()
calculation happening from to to bottom.I need top cell value is the lates
There was a discussion similar to this recently. See this thread which is based off the example Allan linked to.
Kevin
@kthorngren
This is not working
Here is a start based on the examples linked to previously:
https://live.datatables.net/laqixaso/2/edit
Currently it starts with a beginning balance of 0 using
let total = 0;
.Kevin
@kthorngren
I am trying to achieve like below image
One option is to build an array of the transactions in reverse order using
rows().every()
. Then calculate and update the balance. For example:https://live.datatables.net/laqixaso/4/edit
Kevin
Yes, my example showed indexing, but that wasn't the point of it. It shows how it is possible to change the data inside the cells - in your case you'd need to change the logic to sum, but that's totally do-able.
What you don't get with this approach is the ability to search or sort by the calculated column. Like I said, DataTables treats rows independently and you cannot access another row's data from a rendering function. The approach we've shown you here is a workaround at best.
Allan
@kthorngren
When I search it's not recalculating
Please check with the below example , Search with "Ashique"
https://live.datatables.net/laqixaso/5/edit
I forgot to add the
selector-modifier
of{ search: 'applied', order: 'applied' }
to therows().every()
API. Updated example:https://live.datatables.net/laqixaso/7/edit
Kevin
@kthorngren
Thanks, working fine
@kthorngren thanks. I need this too. it works very well.