使用DataGrid和AsyncDataProvider实现SimplePager

时间:2013-07-25 13:43:28

标签: java gwt

如果为数据网格提供了从服务器中获取的值,如何使用AsyncDataProvider实现SimplePager。

1 个答案:

答案 0 :(得分:7)

您必须创建一个扩展AsyncDataProvider的类。在该课程中,您可以覆盖onRangeChanged - 方法。

我的课程就像这样:

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);
                }
            }
        });
    }
}

然后你需要创建DataGrid,AsyncProvider和Pager,如下所示:

// 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);

修改

安德烈在评论中指出,你还需要为查询获取正确的行数。我用“假对象”做了这个,我将其添加到我的列表中,然后在客户端删除它。然后,您可以致电updateRowCount(rowCount, isExact),其中isExcactboolean,表示您输入的行数是精确计数还是仅估计。