javagwtpaginationuibindercelltable

GWT - Pagination adds and removes pages incorrectly at edge case scenarios


I have a CellTable with Pagination that is fed by a ListDataProvider. I set the page size to 5 and the expectation is that when there are more than multiples of 5 rows in ListDataProvider, the page numbers get incremented, and vice-versa. I have Add and Delete buttons that can be used to add and delete cell rows.

The problem is that Pagination adds and deletes pages at the wrong ListDataProvider size. For example, since the page size is 5, I'm expecting that when I add the 6th row to the table, the page size gets incremented. However, the page size is being incremented when the 7th row is added instead of the 6th.

Page not incremented with 6th entry -

enter image description here

Page incremented with 7th entry -

enter image description here

The issue is the same when deleting entries too. The page is updated when row size is down to 4 instead of 5. The issue does not persist if I refresh the page though. As in, when I add the 6th entry and refresh the page, the page number gets incremented as expected, and vice-versa when deleting.

I tried debugging this but couldn't figure out why this is happening. What am I doing wrong here? My code -

@UiField(provided=true) CellTable<MyClass> myTable = new CellTable<MyClass>();
final ListDataProvider<MyClass> myProvider = new ListDataProvider<MyClass>();
final SingleSelectionModel<MyClass> selectionModel = new SingleSelectionModel<MyClass>();
@UiField(provided=true) Pagination pagination = new Pagination(PaginationSize.SMALL);
SimplePager pager;

myProvider.addDataDisplay(myTable);

// create and add columns

createPagination();

private void createPagination() {
    myTable.setSelectionModel(selectionModel);
    pager = new SimplePager(TextLocation.CENTER, ((SimplePager.Resources)GWT.create(SimplePager.Resources.class)), false, 0, false);
    pager.setRangeLimited(false);
    pager.setDisplay(myTable);
    pager.setPageSize(5);
    pagination.rebuild(pager);
}

I'm also calling the createPagination() method in the onSuccess() methods for the add and delete functions.

The .ui.xml for the CellTable and Pagination -

<bc:CellTable ui:field="myTable" striped="true" bordered="true" width="800px" styleName="{style.myTableStyle}"/>
<b:Pagination  ui:field="pagination" />

Solution

  • Adam's comment helped me debug the issue.

    After adding or removing data, I needed to flush the ListDataProvider so that the changes take place immediately instead of waiting until the end of the event loop -

    myProvider.flush();