SDS
SDS

Reputation: 457

Vaadin 8 Grid Pagination

I am trying to implement pagination similar to table for Vaadin 8 Grid. I tried using the add-on https://vaadin.com/directory#!addon/pagination but it doesnot work with Vaadin 8 Grid. Has any one tried implementing Pagination?? I am using Java Grid API shown below :-

    msgGrid = new Grid();

    if (input != null) {
        msgGrid.setItems(input);
    }
    msgGrid.setColumnReorderingAllowed(true);
    msgGrid.setHeaderVisible(true);
    msgGrid.setResponsive(true);
    msgGrid.setRowHeight(25);

Appreciate if you share some info. TIA

Upvotes: 1

Views: 3354

Answers (1)

TacheDeChoco
TacheDeChoco

Reputation: 3913

We have used this particular addon in our Vaadin8 project without any problem. Here is a sample code:

Grid<Sample> datagrid;
Pagination pagination;


datagrid = new Grid<>();  
...
PaginationResource paginationResource = PaginationResource.newBuilder().setPage(1).setLimit(limit).build();
pagination = new Pagination(paginationResource);
pagination.setItemsPerPage(10, 20, 50, 100);
pagination.addPageChangeListener( event -> onPageChange(event.fromIndex(), event.toIndex()) ); 

/**
 MyResultSet is a structure containing
 - a list of <Sample> beans (accessible by getList())
 - a integer with the total number of records (accessible by getTotalnumber())
 */
private void onPageChange(int fromIndex, int toIndex) {     
        MyResultSet data = loadSampleRecords(fromIndex, toIndex);
        this.pagination.setTotalCount( data.getTotalnumber() ); 
        this.datagrid.setItems( data.getList() );
        this.datagrid.getDataProvider().refreshAll();
        this.datagrid.scrollToStart();  
    }

Upvotes: 3

Related Questions