Sort grid on component column

I created a column using the below logic:

column = grid.addColumn("POLink", Component.class);
column.setRenderer(new ComponentRenderer());
column.setHeaderCaption("POLink");
column.setEditable(false);
column.setSortable(true); // Doesn't work here

BeanItemContainer<SupplierPortalReplOrder> gridContainer = new BeanItemContainer<SupplierPortalReplOrder>( SupplierPortalReplOrder.class,
				new ArrayList<SupplierPortalReplOrder>() );
GeneratedPropertyContainer gpcontainer = new GeneratedPropertyContainer(gridContainer);
gpcontainer.addGeneratedProperty("POLink", new PropertyValueGenerator<Component>() {
    @Override
    public Component getValue(Item item, Object itemId, Object propertyId) {
        // Code to create button or label, based on row of data, and return it/
    }
});
grid.setContainerDataSource(gpcontainer);
		
grid.getColumn("POLink").setSortable(true); // Also doesn't work here, same error.

When I run it in the debugger, I get the following at column.setSortable(true): “java.lang.IllegalStateException: Can’t set column Column[propertyId:POLink] sortable. Container doesn’t support sorting by property POLink”. Formerly this was a normal text column, so it worked fine, allowing sorting. But I need to add a button to this column now, thus the code change. For now I can disable sorting for that column, but the underlying data supports sorting just fine. Any ideas on making this sortable when I use components?

I rarely call column.setSortable directly. Normally I just let the lower level logic handle things based on the container, but I did just realize that anyplace I have buttons in the grid doesn’t let me sort by that column. I can sort by other columns. I never noticed this because these tended to be columns people wouldn’t sort by anyway.

I wonder if, in this case, I could somehow make a column sortable and clickable using another method? If the above request is not possible, maybe there is a work around. My ultimate goal is a column with, where appropriate. links that will open a PDF file in another browser tab. With buttons this is easy, but then I cannot sort.

Thanks.

Have you tried to use a comparator to archive your goal? framework/server/src/main/java/com/vaadin/ui/Grid.java at e310099595309ce730336f940326d878a16731f4 · vaadin/framework · GitHub

To me it is clear that setSortable(true) throws if you call it before you have configured the generated property, as the property does not exists yet. It has been so long time I have done anything with generated properties and I have not used the feature with ComponentRenderer, which is a third party add-on. One thing you might try is to configure the generated property before addColumn call.

Thanks Tatu, that was worth a try. Didn’t work, sadly. All the other columns allow me to setSortable(true) just fine, and originally, when this was just a simple text column, I could setSortable(true) just fine. If I step into the code in the debugger, it fails because it cannot find “POLink” in the list of sortable property ids. When it goes through all the property ids and gets to “POLink” column, because “Component” is not comparable according to this rule:

            if (Comparable.class.isAssignableFrom(propertyType)
                    || propertyType.isPrimitive()) {
                sortables.add(propertyId);
            }

it does not add it to the list of sortable properties. I guess I need to think of some way to make this component Comparable. Since I am using either a Label or a Button, currently, that would be difficult, at least to do directly. I could make a CustomComponent and implement Comparable interface on that. Since the CustomComponent would know the true component being used, it would be easy to write a compareTo method. This would probably also fix the “filter”, which I just noticed is not working. I need to think about this.

Also, setComparator won’t work for Vaadin 7. Sorry, forgot to mention this was in Vaadin 7.

Not that there’s anything wrong with using CustomComponent, but wouldn’t it be enough to extend Label or Button and implement Comparable?

2 Likes