Knarf
Knarf

Reputation: 2156

Vaadin Grid sorting in vanilla GWT

I have a question concerning the grid widget. This widget is also available in vanilla GWT through the "GWT-Widgets 7.4.3" download.

But my question is how do you enable sorting in this widget in GWT ?

I started from the example provided by Vaadin : https://github.com/Artur-/grid-gwt

But when you do a "setSortable(true)" on a Column the only thing that happens is that an arrow of sort direction is drawn in the table header when clicking it. The table itself isn't re-sorted.

Also executing a sort method on the grid with a column and a direction has no effect.

In the book of vaadin https://vaadin.com/book/-/page/components.grid.html I noted the following : "The container data source must support sorting. At least, it must implement Container.Sortable.".

So I suppose that ListDataSource is not suited for sorting. But what else can I use in GWT so that it sorts ? In the book of Vaadin I see that a BeanItemContainer is used, but this is not available in "GWT-Widgets 7.4.3" as this seems to be a server component.

So it comes down to this question: How do I enable sorting on the Vaadin grid in a vanilla GWT project ?

thanks Frank

Upvotes: 1

Views: 579

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

I have no experience with Vaadin, but in plain GWT you would do something like this (where T is your object):

ListDataProvider<T> dataProvider = new ListDataProvider<T>();
List<T> displayItems = dataProvider.getList();
ListHandler<T> sortHandler = new ListHandler<T>(displayItems);

Then, to make a column sortable, you need to tell the sortHandler how to sort these objects:

dateColumn.setSortable(true);
dateColumn.setDefaultSortAscending(false);
sortHandler.setComparator(dateColumn, new Comparator<Item>() {
    @Override
    public int compare(Item o1, Item o2) {
        if (o1.getDate() != null) {
            return (o2.getDate() != null) ?
                o1.getDate().compareTo(o2.getDate()) : 1;
        }
        return -1;
    }
});

Upvotes: 0

Related Questions