Balance Calculation on Search

Balance Calculation on Search

ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0
edited July 2023 in Free community support

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

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887

    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

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    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

  • allanallan Posts: 62,858Questions: 1Answers: 10,344 Site admin

    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

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0
    edited July 2023

    Not indexing

    In coumns.render()calculation happening from to to bottom.

    I need top cell value is the lates

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887

    There was a discussion similar to this recently. See this thread which is based off the example Allan linked to.

    Kevin

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0
    edited July 2023

    @kthorngren
    This is not working

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887

    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

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    @kthorngren

    I am trying to achieve like below image

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887
    edited July 2023

    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

  • allanallan Posts: 62,858Questions: 1Answers: 10,344 Site admin

    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

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    @kthorngren
    When I search it's not recalculating
    Please check with the below example , Search with "Ashique"

    https://live.datatables.net/laqixaso/5/edit

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887
    Answer ✓

    I forgot to add the selector-modifier of { search: 'applied', order: 'applied' } to the rows().every() API. Updated example:
    https://live.datatables.net/laqixaso/7/edit

    Kevin

  • ashiquem01ashiquem01 Posts: 24Questions: 5Answers: 0

    @kthorngren
    Thanks, working fine

  • TanbiTanbi Posts: 1Questions: 0Answers: 0

    @kthorngren thanks. I need this too. it works very well.

Sign In or Register to comment.