I am using Vaadin 7 and I need to do a very special sorting on a particular column, such that when they click the column and it sorts by 2 different values in the Bean. Is there a way to do this in Vaadin 7?
This is how I did it for the table, inside the ListContainer
I use:
@Override
public void sort(final Object[] propertyId, final boolean[]
ascending) {
final boolean sortAscending = ascending[0]
;
final Object sortContainerPropertyId = propertyId[0]
;
Collections.sort(getBackingList(), (o1, o2) -> {
int result = 0;
...
else if ("riskPct".equals(sortContainerPropertyId) ||
"riskPctValue".equals(sortContainerPropertyId) )
{
result = WmsXmlUtil.compareXmlNumbers(o1.getRiskSeverity(), o2.getRiskSeverity());
if( result == 0 )
result = WmsXmlUtil.compareXmlNumbers(o1.getRiskPct(), o2.getRiskPct());
}
...
}
}
“RiskPct” is in the table/grid/ but “RiskSeverity” is not. So that is why I need to do this special sorting.x
Could I get around this by doing something like this:
gridContainer = new BeanItemContainer<WrappedPlan>( WrappedPlan.class, new ArrayList<WrappedPlan>() );
gridContainer.setItemSorter(new DefaultItemSorter(){
@Override
public void setSortProperties(Sortable container, Object[] propertyId, boolean[]
ascending) {
// TODO If one of my sort property ids is "riskPct", redo arrays so that "severity" precedes it
// Will that mean I need to add and then hide the severity column in Grid?
super.setSortProperties(container, propertyId, ascending);
}
});
I guess I could also override protected int compareProperty(Object propertyId, boolean sortDirection, Item item1, Item item2)
in DefaultItemSorter
, or even override sort
like I did with tables in my older code. Might be more efficient then overriding setSortProperties
.
Ok, this works, in case anyone is curious. Rather obvious solution, when I looked at the older table code and the lower level stuff:
gridContainer.setItemSorter(new DefaultItemSorter() {
/**
*
*/
private static final long serialVersionUID = 2063583546056864136L;
@Override
protected int compareProperty(Object propertyId, boolean sortDirection, Item item1, Item item2) {
if ("riskPct".equals(propertyId) ||
"riskPctValue".equals(propertyId) )
{
final Property<?> riskPct1Prop = item1.getItemProperty("riskPct");
final Property<?> riskPct2Prop = item2.getItemProperty("riskPct");
final Property<?> riskSeverity1Prop = item1.getItemProperty("riskSeverity");
final Property<?> riskSeverity2Prop = item2.getItemProperty("riskSeverity");
// Get the values to compare
final Object riskPct1 = (riskPct1Prop == null) ? null : riskPct1Prop.getValue();
final Object riskPct2 = (riskPct2Prop == null) ? null : riskPct2Prop.getValue();
final Object riskSeverity1 = (riskSeverity1Prop == null) ? null : riskSeverity1Prop.getValue();
final Object riskSeverity2 = (riskSeverity2Prop == null) ? null : riskSeverity2Prop.getValue();
if( riskSeverity1 instanceof String &&
riskSeverity2 instanceof String &&
riskPct1 instanceof String &&
riskPct2 instanceof String)
{
int result = WmsXmlUtil.compareXmlNumbers((String)riskSeverity1,
(String)riskSeverity2);
if( result == 0 )
result = WmsXmlUtil.compareXmlNumbers((String)riskPct1,
(String)riskPct2);
if (sortDirection)
{
return result;
}
else
{
return result * -1;
}
}
}
return super.compareProperty(propertyId, sortDirection, item1, item2);
}
});
Since I know the Item
is a BeanItem
, I could have also gotten the underlying bean, I just figured the way I did it was simpler. When I move to Vaadin 14+, the method will use the bean, I imagine, but in that case I can set a comparator for the column, so it will actually be simpler.