Why does the _group function in datatables.rowgroup.js receive unsorted rows and create duplicates?

Why does the _group function in datatables.rowgroup.js receive unsorted rows and create duplicates?

GirlsAreSoftGirlsAreSoft Posts: 10Questions: 2Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I will create a test case if this question and answer is not self-evident. I am using the latest version of datatables.rowgroup.js. I added a rowGroup definition that uses startRender. I get groups displayed in my table. However, the groups are repeated. Looking at the _groups function in datatables.rowgroup.js I see that the algorithm, in its for loop, makes the assumption that the group value is never repeated. When the group value changes, it creates a new entry in its internal data array and pushes the current datapoint without checking to see if this group value already exists (and thus aggregating to prevent duplicates).

I tried adding the sort parameter to the rowGroup parameter object as well as defining a default sort on the table as a whole but this had no effect to the array of row indexes being passed into the _group() rows parameter. The documentation on this site for the rowgroup extension doesn't mention anything about requiring any sort of sort or pre-sort to get the grouping to work so I'm wondering what I am missing (and how this hasn't come up before on this forum). I know the grouping has worked for me in the past but I am puzzled as to how after looking at _group(). Thanks!

This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 19,470Questions: 26Answers: 4,612
    edited October 2021

    My guess is the primary sort column is not the same as the rowGroup.dataSrc column.


  • GirlsAreSoftGirlsAreSoft Posts: 10Questions: 2Answers: 0

    I'll take a look at that again but I believe I tried making those two the same as I suspected this was the issue.

  • GirlsAreSoftGirlsAreSoft Posts: 10Questions: 2Answers: 0

    That was definitely it. I think the documentation page needs to explicitly state that in order for rowgroup to work the first sorting column must match the sort specified in the rowGroup parameter. Maybe the orderFixed column must be set, as well?

  • GirlsAreSoftGirlsAreSoft Posts: 10Questions: 2Answers: 0

    Additionally, I would add that if the _group() function were adjusted it may not be necessary for the sort to be in place as the row group collection logic could just aggregate the iterator value to any existing category in the data array.

  • allanallan Posts: 60,307Questions: 1Answers: 9,793 Site admin
    Answer ✓


    Thanks for the feedback - I'll see if we can make this clearer in the documentation. I actually think the current behaviour is correct - it groups by the data it is told to, in the current order of the table. If you want to ensure that all like terms are together than something like orderFixed should be used. Otherwise RowGroup would be altering the sorting that is applied to the DataTable.


  • GirlsAreSoftGirlsAreSoft Posts: 10Questions: 2Answers: 0

    I hear what you are saying and obviously you are correct that it does work if all of the prerequisite conditions are in place. My point is that the grouping is a brute force approach that iterates the whole table whether it is sorted or not so you could prevent questions or misunderstandings like mine by adjusting the code slightly to aggregate the results. The performance difference would be essentially nothing unless the table was very, very large (at which point folks should be optimizing in other ways anyway).

    Thanks again.

Sign In or Register to comment.