sort by difference with group in rowGroup

sort by difference with group in rowGroup

wintermelon_loverwintermelon_lover Posts: 6Questions: 5Answers: 0

Link to test case: http://live.datatables.net/bulidolu/3/edit
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
When I group data in datatables, I will group them in pairs or leave one data in a group. Is there a way to sort data so that for group with 2 rows, those with large difference cone first, and group with 2 rows come before group with 1 row? Thanks!

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    columns.orderData is the option you want here I think. It can tell DataTables to use multi-column sorting for a column: http://live.datatables.net/bulidolu/4/edit .

    Allan

  • wintermelon_loverwintermelon_lover Posts: 6Questions: 5Answers: 0

    Thanks for reply! It seems orderdata only consider index. Is there anyway to make it also consider difference within group?
    For instance, in the example, in group a the absolute difference is 20, in group b the absolute difference is 50, in group 3 there is only 1 record. Then the order need to be b -> a -> c. Is there a way to achieve that?

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    edited August 2021

    One way is to calculate the differences in initComplete, update a hidden column with the difference for each group then call draw() to update the table sorting. See this example:
    http://live.datatables.net/bulidolu/5/edit

    It uses the following API's:

    column().data()
    rows().data() with the row-selector as a function
    pluck()
    toArray()

    There might be more efficient ways to do this. You can hide the diff column with columns.visible

    Instead of doing all this at the client you could get the difference each group at the server then place the difference in each row for the group.

    Kevin

Sign In or Register to comment.