table sorting: sort on column with foreign key


I got a table and I use the BeanItemContainer to bind the data. Now certain columns contain data from foreign keys. These colunns are not sortable.
Digging for quite a while I was unable to find any instructions on how I can make these columns sortable. Can anybody help?


Can you provide some sample code?

Here are some excepts (Grails, GORM)

This is a domain class. Category would be a foreign key here. I removed a some code, like the overridden toString() and equals() method or the mappings{} and the constraints{}.

class InventoryItem {
   String name
   String description
   String type
   ItemCategory category

Here’s how the table would be created:

// Get all the items via a call to the service layer
BeanItemContainer<InventoryItem> tableBeans = new BeanItemContainer<InventoryItem>(InventoryItem.class)
List<InventoryItem> items =Grails.get(InventoryItemService).get()

// Create a table
Table table = new Table()

Of course the table than still has to be added to a view. But before I bother you with more code, these bits should give you an Idea of my setting. Since it is systematically the case that columns representing data from foreign keys are not sortable, I get the impression, that maybe some compare() method needs to be implemented somwhere. But so far I honestly did not really dig into the sorting mechanism of the table, since I was hopeing to get some directions here.

Exactly, Java or Vaadin don’t know how to compare/sort custom objects, so you need to implement the Comparable interface in ItemCategory. Alternatively have a look at nested properties. If your toString just returns eg the category name you can use something like tableBeans.addNestedContainerProperty(“”)

Thanks Felix

I opted to implement the java.lang.Comparable interface on the concerned domain classes. It seemed to the proper way to do it, since using nested properties, would lead to modify also the call to table.setVisibleColumns() and then having to do table.setColumnHeader() for each of the modified columns too.

Can you share this solution and any code which performs the actual sort()?