Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement SimplePager with DataGrid and AsyncDataProvider

Tags:

java

gwt

How to implement the SimplePager with AsyncDataProvider when the data grid is provided with values fectched from the server.

like image 378
user2617252 Avatar asked Nov 18 '25 18:11

user2617252


1 Answers

You have to create a class extending AsyncDataProvider. In that class you can override the onRangeChanged-method.

My class for example looks like this:

public class AsyncListProviderVisit extends AsyncDataProvider<MyObject> {

    @Override
    protected void onRangeChanged(HasData<MyObject> display) {
        // Get the new range.
        final Range range = display.getVisibleRange();

        /*
         * Query the data asynchronously. If you are using a database, you can
         * make an RPC call here. We'll use a Timer to simulate a delay.
         */

        final int start = range.getStart();
        int length = range.getLength();

        Service.Util.getInstance().getPartOfImmoObjects(start, length, new AsyncCallback<List<MyObject>>() {

            @Override
            public void onFailure(Throwable caught) {
                ConfirmationPanel cp = new ConfirmationPanel();
                cp.confirm("Error!", "An Error occurred during data-loading.");
            }

            @Override
            public void onSuccess(List<MyObject> result) {
                if (result != null) {
                    updateRowData(start, result);
                }
            }
        });
    }
}

Then you need to create the DataGrid, the AsyncProvider and the Pager, like this:

// Create a CellList.
DataGrid<LcVisits> grid = new DataGrid<LcVisits>();

// Create a data provider.
AsyncListProviderVisit dataProvider = new AsyncListProviderVisit();

// Add the cellList to the dataProvider.
dataProvider.addDataDisplay(grid);

// Create paging controls.
SimplePager pager = new SimplePager();
pager.setDisplay(grid);

// and add them to your panel, container, whatever
container.add(grid);
container.add(pager);

edit

as Andre pointed out in his comment you also need to fetch the correct row-count for the query. I did this with a "fake-object", which I add to my list and then delete it on the client side. You can then call updateRowCount(rowCount, isExact) where isExcact is a boolean indicating whether the row-count you entered is the exact count or just estimated.

like image 136
tommueller Avatar answered Nov 21 '25 08:11

tommueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!