Lazy loading with the Grid component

A Grid component can take advantage of the offset and limit parameters described in the previous section by using the setDataProvider method, as follows:

grid.setDataProvider(
    (sortOrders, offset, limit) ->
            CallRepository.findAll(offset, limit).stream(),
    () -> CallRepository.count()
);

The previous code defines two lambda expressions:

  • (sortOrders, offset, limit) -> service.find(...): This lambda expression should return all the items used in slice defined by the offset and limit parameters (we will see how to use the sortOrders parameters later)
  • () -> service.count(): This lambda expression should return the total count of items available with no slices

The setDataProvider method we used in the previous example receives an instance of FetchItemsCallback, a functional interface that defines a method to fetch the items (or rows):

@FunctionalInterface
public interface FetchItemsCallback<T> extends Serializable {

public Stream<T> fetchItems(
List<QuerySortOrder> sortOrder, int offset, int limit);
}

You can also use another version of the setDataProvider method that accepts an instance of DataProvider. There's a static helper method in the DataProvider interface that allows you to implement it from lambda expressions similar to the ones we used before:

DataProvider<Call, Void> dataProvider = DataProvider.fromCallbacks(
query -> CallRepository.find(
query.getOffset(),
query.getLimit()).stream(),
query -> CallRepository.count()
);

grid.setDataProvider(dataProvider);

The difference with the previous version is that we get the offset and limit values from a Query instance, so we need to use the corresponding getters.

..................Content has been hidden....................

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