3

当向数据网格提供从服务器获取的值时,如何使用 AsyncDataProvider 实现 SimplePager。

4

1 回答 1

6

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.

于 2013-11-18T12:09:11.750 回答