Setting a default sorting order for Table

Hi,

Let’s say we have Order and Order Lines entity relationship (composition). I would like to have a default sorting order in the table of Order Lines of product, price and quantity at the point when the datasource is loaded from the database. Let’s say i want to sort the lines by default by product names.

I tried to use @OrderBy in the collection attribute of the Order entity but it’s only limited to local attributes of the Order Line entity.

Is there another way to make it work?

@OrderBy works only with direct properties or embedded attributes.

You can use collectionDatasource to load Order Lines and make the order by query as want.

https://doc.cuba-platform.com/manual-6.1/datasource_decl_creation.html

You can also sort the table like this:


public class OrderEdit extends AbstractEditor<Order> {
    @Inject
    private Table<OrderLine> linesTable;

    @Override
    protected void postInit() {
        linesTable.sortBy(linesDs.getMetaClass().getPropertyPath("product.name"), true);
    }
}    

Make sure the lines collection is of type List or LinkedHashSet and the table is sortable.

Table.sortBy is working fine. Thank you.

this is deprecated now, does it still work? what is the best way to set a default sorting?

As it is mentioned in JavaDoc of sortBy you can use sort(columnId, SortiDirection).

usersTable.sort("login", Table.SortDirection.ASCENDING);

First of, my apologies for bumping this up after so long.

Just wanted to add that for this kind of sorting, you will need to have the sorting column visible. The previous option was calling the sorting field from the data source.

In my case, the sorting field I want to use is not one that I want to have visible on the table. If anyone can point out how to achieve that, I’ll appreciate. I’ll look around though.

Regards,
Kenince