Get List<items> from Vaadin Grid

人盡茶涼 提交于 2021-01-03 03:19:25

问题


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


回答1:


Have you tried this basically?

List<Pojo> list = grid.getDataProvider()
                      .fetch(new Query<>())
                      .collect(Collectors.toList());



回答2:


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.




回答3:


TL;DR: you can't. The grid utilizes the data provider to fetch chunks of data to display (hence the count/limit/offset). The fact that there are eager data source backends (to set the items directly) is just to make it easier for grids with eager data.

So the solution here is to just extract the data from your actual source (your repository etc). So in your case it's something like this:

dao.getAllFiltered(0, dao.getCountAllFiltered(filter), filter)

Or by any means, that make that simpler.



来源:https://stackoverflow.com/questions/51302311/get-listitems-from-vaadin-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!