Sorting of tree implemented with NatTable

489 Views Asked by At

I'm trying to implement sorting of tree implemented with NatTable but I can't really understand how it works. My problem is that after the sorting of any column other than 'tree' column child nodes can move to invalid parent. Though the order of elements is correct on all levels of hierarchy. So basically I don't understand the purpose of TreeList.Format.getComparator() and how it related to comparators registred for the columns, and I don't understand when and why node can change its parent.

I've started with the example TreeGridExample and I've managed to create test data with which I have the same problem

private void createDatums() {       
    createDatum(null, "a", 2);        
    createDatum("a", "aa1", 0);        
    createDatum(null, "b", 0);        
    createDatum("b", "bb1", 0);
    createDatum(null, "m", 1);
    createDatum(null, "n", 0);
}

If I sort column bar, bb1 node jumps from b to n and when sorting removed, it again is child of b

2

There are 2 best solutions below

0
On

Regarding our example, maybe our SortableTreeComparator is not working correctly. And the answer is not trivial.

First, in order to make the TreeList work correctly you need to bring the elements in the list into the correct order. This is the tree comparator.

What you additionally want is to add a custom sorting on the lowest level. So in that case you need to extend your comparator to check for the tree level.

The reason why the parent changes is because your comparator changes the order in the collection and therefore breaks the whole tree structure.

We managed to make it work with the GroupBy feature in the GroupByComparator, but there we have the GroupByObjects in the list. And I remember it was hard work to get everything working together.

Please create a ticket to fix the SortableTreeComparator, not sure when I'm able to work on it.

2
On

Your comparator need to take into account the tree hierarchy.

See here and here some more information.