Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get List<items> from Vaadin 8 Grid

Problem: I have a Vaadin 8 Grid , and I can't find a way to extract the items inside of it.

Description: Starting from a grid

Grid<Pojo> myGrid = new Grid<>();

I've configured it so it can take data with lazy loading.

    myGrid.setDataProvider(
            (sortOrd, offset, limit) -> dao.getAllFiltered(offset, limit, filter),
            () -> dao.getCountAllFiltered(filter)
    );

At this point, I want to extraxt all the items from the grid (for putting that into an excel), something like List<Pojo> list = myGrid.getItems();. I've also tried passing through myGrid.getDataProvider() , but there are no useful getter into it.

I can't find any getter, how can I achieve this? Thanks

like image 810
Leviand Avatar asked Jul 12 '18 09:07

Leviand


2 Answers

Have you tried this basically?

List<Pojo> list = grid.getDataProvider()
                      .fetch(new Query<>())
                      .collect(Collectors.toList());
like image 65
O. Ozturk Avatar answered Oct 08 '22 20:10

O. Ozturk


All DataProvider's implement above mentioned fetch(..) method. I.e. that answer is universal.

Also there are other ways, you can do also:

List<Pojo> list = 
grid.getDataCommunicator.fetchItemsWithRange(0,grid.getDataCommunicator.getDataProviderSize());

see also: Use filtered dataProvider contents when FileDownloader is called in Vaadin

The difference to above mentioned fetch(..) method is that DataCommunicator.fetchItemsWithRange will give the items in the way are currently sorted and filtered in Grid.

In case the DataProvider is instance of ListDataProvider also the following is possible and recommended

ListDataProvider dataProvider = (ListDataProvider) grid.getDataProvider();
List<Pojo> list = dataProvider.getItems();

So there is at least three correct answers to the question. Which is the most suitable depends on the application.

It is good to remind, that using fetch(..) or fetchItemsWithRange(..) to get all the items, from a lazy loading data provider, may result in huge and memory consuming database query (i.e. fetching the whole content). And you probably should not do that. That is why getItems() is implemented only in ListDataProvider, but not included in generic DataProvider interface.

like image 34
Tatu Lund Avatar answered Oct 08 '22 19:10

Tatu Lund