Question

In my Vaadin 7 project, I need to ignore the parent nodes(rows) of a treetable while sorting. I am able to customize the sort using ItemSort, but I don't know how to skip the parent nodes!

FYI: I am able to identify my parent node with its ItemId.

***Update***

I'm using a Hierarchical Container, and my Tree is similar to this:

Name            Nos             salary
+ Male(Node 1)  5               $12000
  + Young       3               $6000
    A           1               $3000
    B           1               $1000
    c           1               $2000
  + Old         2               $6000
    A           1               $3000
    B           1               $3000
+ Female(Node 2)5               $13000
  + Young       3               $5000
    A           1               $2000
    B           1               $1000
    c           1               $2000
  + Old         2               $8000
    A           1               $5000
    B           1               $3000

The problem is when I sort the salary column it includes the summation of each person's salary on "Male, young & old", "Female, young & old" rows and adjusts the position of "Male, Female" and "Young, old" categories.

I just want to skip the parent nodes in my custom sort. It should only the children nodes. In simple words. All the parent nodes should be fixed, and only the child nodes have to be sorted.

Updated USECASE

Two elements having same parents and those two elements has no child nodes then I used DefaultItemSorter.compare(). And I ignored all other use cases.

Was it helpful?

Solution

I had a related Problem, with special tree table sorting. I ended up writing my own Container and Comperator implementation.

Your question depends on the container you are using, since the sorting is done by the the underlying container and not the table. By default TreeTable uses the HierarchicalContainer implementation, where you could use your own ItemSorter implementation. You could extend DefaultItemSorter and distinguish different cases when comparing two elements:

  • Two leaf elements having the same parent:
    use DefaultItemSorter.compare()
  • Two parent elements having the same parent (also null):
    return the value to keep the order you want
  • An element to one in its path to the root:
    return a value so the deeper element is after the higher.
  • Two elements not having the same parent:
    In both paths go up the tree until you find the depth where the next parents are the same and compare these two elements. Start with the deeper element and go up until you are on the same level as the other.

I hope i didn't forget a case. Anyway I advise you to write several unit tests to be sure your ItemSorter does the right thing in all the cases.

If you know that your tree has special characteristics, like the maximal depth is two, you might simplify the ItemSorter. I couldn't see that from your question. Please add more details next time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top