Enabling ordering in the UI

As we have previously discussed, the DataProvider interface uses an object of type List<QuerySortOrder> to provide the ordering configuration. However, the backend service requires an object of type Map<String, Boolean>. We have to implement a helper method that translates between these two types. We can add this method to a separate DataUtils class and implement it as follows:

public class DataUtils {

public static <T, F> Map<String, Boolean> getOrderMap(
Query<T, F> query) {
Map<String, Boolean> map = new LinkedHashMap<>();

for (QuerySortOrder order : query.getSortOrders()) {
String property = order.getSorted();
boolean isAscending = SortDirection.ASCENDING.equals(
order.getDirection());
map.put(property, isAscending);
}

return map;
}
}

The getOrderMap method iterates over the QuerySortOrder objects returned by the query.getSortOrders() method and maps them to entries in a map of type Map<String, Boolean>. Notice how we used the LinkedHasMap type. This allows us to keep the entries in the map in the same order in which they come from the List provided by the query object, something we need if we want to support multiple-column ordering in the Grid (the order by clause should reflect the sequence used when the user clicked the headers in the browser).

We can use this utility method in the DataProvider, as follows:

DataProvider<Call, Void> dataProvider = DataProvider.fromFilteringCallbacks(
query -> CallRepository.find(query.getOffset(), query.getLimit(), filter.getValue(), DataUtils.getOrderMap(query)).stream(),
query -> {
int count = CallRepository.count(filter.getValue());
countLabel.setValue(count + " calls found");
return count;
}
);

The final result is illustrated in the following screenshot:

To complete this chapter's example, we can enable column reordering (the users can drag the columns in the browser to reposition them) as follows:

grid.setColumnReorderingAllowed(true);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset