I’m wondering how it is possible to sort generated properties added by
GeneratedPropertyContainer.addGeneratedProperty(id, new PropertyValueGenerator<...>()
Book of Vaadin only says (8.5.7. GeneratedPropertyContainer - Sorting)
“Even though the GeneratedPropertyContainer implements Container.Sortable, the wrapped container must also support it or otherwise sorting is disabled. Also, the generated properties are
not
normally sortable , but
require special handling to enable sorting .”
without explaining on what sort of
special handling is required (I wrapped ye’ olde IndexedContainer is anyone is wondering).
So far I’ve tried to
make sure the generated property is included in the sortablePropertyIds by adding
@Override
public SortOrder getSortProperties(final SortOrder order) {
return new SortOrder{new SortOrder(this, order.getDirection())};
}
to the PropertyValueGenerator - debug shows that the generated property is included.
implement a CustomItemSorter by extending the DefaultItemSorter, also calling the setSortProperties and passing the GeneratedPropertyContainer to it, but a debug shows that for whatever reason it only picks up the regular property in the container, not the generated one.
Progress! I managed to get my generated property to sort - not sure if there is a better way but for those wondering:
First of all:
return new SortOrder{new SortOrder([b]
this
[/b], order.getDirection())};
of course has to be
return new SortOrder{new SortOrder([b]
actualPropertyId
[/b], order.getDirection())};
with the actualPropertyId (aka the pid of the property I’m - in my case - replacing with the generated one) being passed to the PropertyValueGenerator. I wonder if there is a better/easier way to get the pid?
Next I passed my CustomItemSorter a reference to the GeneratedPropertyContainer and made sure it will always use that one
@Override
public void setSortProperties(final Sortable container, final Object propertyId, final boolean ascending) {
super.setSortProperties(this.myGeneratedPropertyContainer, propertyId, ascending);
}
this way when
ItemSorter calls
getSortableContainerPropertyIds() it will use the
GeneratedPropertyContainer instead of the underlying
IndexedContainer and pick up the generatedProperty.
Don’t forget to override the compareProperty(…) method if neccessary.