m.gavr
m.gavr

Reputation: 83

Vaadin grid - filtering with lazy loading

I have vaadin grid, and it's great that it has lazy data loading from the box. But for some reasons I have custom filters, which I use via

CallbackDataProvider<> dataProvider.fetch(Query query)

Query object has parameters for loading by portions (offset and limit), so I need to set it dynamically (?) and somehow listen grid scrolling event to load next part of data when user scrolls down (?)

Grid.dataComunicator has field Range pushRows but there no public methods to get it. And all i have is grid with lazy loading without filtered data or grid with eager loading with filtered data.

So, is there any way to implement filtering data with lazy loading in vaadin grid element?

Upvotes: 0

Views: 1903

Answers (2)

Brett Sutton
Brett Sutton

Reputation: 4584

I arrived here using vaadin 22. The answer probably isn't in the same context as the question but given I arrived here I suspect others will.

To create a grid that uses lazy loading and is able to inject a filter into the query use:

class SearchableGrid<E> {

Grid<E> entityGrid = new Grid<>();

private SearchableGrid(DaoDataProvider daoProvider)
    {
        var view = entityGrid.setItems(query ->
            {
                // add the filter to the query
                var q = new Query<E, String>(query.getOffset(), query.getLimit(), query.getSortOrders(), null,
                        getSearchField().getValue());

                return daoProvider.fetchFromBackEnd(q);
            });
        view.setItemCountCallback(query ->
            {
                // add the filter to the query
                var q = new Query<E, String>(query.getOffset(), query.getLimit(), query.getSortOrders(), null,
                        getSearchField().getValue());

                return daoProvider.sizeInBackEnd(q);

            });
    }

I've packaged the methods into a BackEndDataProvider as the same class can be used to as a provider for comboboxes.

public class DaoDataProvider<E extends CrudEntity>
        extends AbstractBackEndDataProvider<E, String>
{
    JpaBaseDao<E> dao;
    GetFilterBuilder<E> getFilterBuilder;

    public DaoDataProvider(JpaBaseDao<E> daoProvider, GetFilterBuilder<E> getFilterBuilder)
    {
        this.dao = daoProvider;
        this.getFilterBuilder = getFilterBuilder;
    }

    

    @Override
    public int sizeInBackEnd(Query<E, String> query)
    {
            var q = getFilterBuilder.builderFilter(query);
            return (int) q.count().intValue();
    }

    @Override
    public Stream<E> fetchFromBackEnd(Query<E, String> query)
    {

            var q = getFilterBuilder.builderFilter(query);
            q.startPosition(query.getOffset()).limit(query.getLimit());
            return q.getResultList().stream();
    }
}

The filterBuilder is where you construct your query for your back end data provider.

Upvotes: 0

m.gavr
m.gavr

Reputation: 83

ok, problem solved by using ConfigurableFilterDataProvider<> as wrapper over CallbackDataProvider<>. so, when i filter table, this wrapper adds filtering conditions to all queries, and data loads lazy as usual.

Upvotes: 0

Related Questions