Java, Vaadin 8, lazy loading with grid

2019-08-25 02:24发布

问题:

I am trying to implement lazy loading with Grid in Vaadin 8. But it only shows an empty table. What do I do wrong? Also, how do I set the number of items to load (limit) to for example 20 items (the default is 40 items)?

private Grid<Image> makeLazyTable()
{
  Grid<Image> grid = new Grid<Image>();     
  DataProvider<Image, Void> dataProvider = DataProvider.fromCallbacks(
    query -> {
      int offset = query.getOffset();
      int limit = query.getLimit();        
      OffsetRequest request = new OffsetRequest();
      request.setLimit(limit);
      request.setOffset(offset);  
      List<QuerySortOrder> sort = query.getSortOrders();
      return ImagesRepository.findAll(request, sort);
    },
    query -> ImagesRepository.getImageCount()
  );    
  grid.setDataProvider(dataProvider);     
  return grid;
}

回答1:

I did not add any columns. That was the reason why the table was empty. This way it works:

Column<Image, String> filenameColumn = grid.addColumn(Image::getFilename);
Column<Image, String> orientationColumn = grid.addColumn(Image::getOrientation);
Column<Image, String> latitudeColumn = grid.addColumn(Image::getLatitude);
Column<Image, String> longitudeColumn = grid.addColumn(Image::getLongitude);