Read current grid contents with filter applied in sort order

Hi. I need to get access to the contents of a Grid (GridPro) as a List with the filtering/sorting of the Grid preserved.
The Grid is using a ListDataProvider for the contents so I tried using the ListDataProvider.fetch() method, however it is not obivious how to get the query parameter based on the current Grid status?

I found some example code in the Exporter component
https://vaadin.com/directory/component/exporter
which works and will do for now but seems overly complex/likely to break.

    /** Provide an Ordered (Filtered/Sorted) list of the items in the Grid respecting the current sort/filter */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List<SessionInteraction> getCurrentGridContents() {
        Object filter = null;
        try {
            Method method = DataCommunicator.class.getDeclaredMethod("getFilter");
            method.setAccessible(true);
            filter = method.invoke(grid.getDataCommunicator());
        } catch (Exception ignored) {}
        var query = new Query(0, grid.getDataProvider().size(new Query(filter)), grid.getDataCommunicator().getBackEndSorting(),
                grid.getDataCommunicator().getInMemorySorting(), null);
        Stream<SessionInteraction> dataStream = grid.getDataProvider().fetch(query);

        return dataStream.collect(Collectors.toList());
    }

Is there a better/recommended way to do this?

Hi Jenny

Please see [this stackoverflow answer]
(https://stackoverflow.com/a/60203788/3441504), it answers your question directly.

Super! Thank you.

This gives me:

    /** Provide an Ordered (Filtered/Sorted) list of the items in the Grid respecting the current sort/filter */
    @SuppressWarnings({"unchecked"})
    public List<SessionInteraction> getCurrentGridContents() {
        ListDataProvider<SessionInteraction> dataProvider = (ListDataProvider<SessionInteraction>) grid.getDataProvider();
        int totalSize = dataProvider.getItems().size();
        DataCommunicator<SessionInteraction> dataCommunicator = grid.getDataCommunicator();
        Stream<SessionInteraction> stream = dataProvider.fetch(new Query<>(
                0,
                totalSize,
                dataCommunicator.getBackEndSorting(),
                dataCommunicator.getInMemorySorting(),
                dataProvider.getFilter()));
        return stream.collect(Collectors.toList());
    }

Which is pretty much what I had from the Exporter code but w/o the need to use reflection to get the DataCommunicator. It still looks to me like the DataProvider should provide this directly with something like.

public List<T> getCurrentItems();

and/or

public Stream<T> fetch(int offset, int limit);

Cheers.

it resembles your code yes, the idea is to fetch a list from the dataprovider using a query. But besides not needing reflection, you had some other mistakes, like grid.getDataProvider().size(new Query(filter)), and using null as filter.

It still looks to me like the DataProvider should provide this directly with something like public List<T> getCurrentItems();

I agree :slight_smile: Feel free to open an issue for this in the [github repo]
(https://github.com/vaadin/vaadin-grid-flow/issues)