是否可以将数据延迟加载到GWT DataGrid中,类似于GWT CellList如何延迟加载数据?
我有一个GWT DataGrid可能会带回数百行,但一次只显示大约20行.发生这种情况时,网格的加载速度非常慢.
我想使用DataGrid而不是CellTList,因为我有多列数据需要显示.我选择了DataGrid而不是CellTable,因为我希望修复标题列.
既然DataGrid不暴露它ScrollPanel,创建一个扩展的内部类,DataGrid并提供对以下内容的引用ScrollPanel:
private static class MyDataGrid<T> extends DataGrid {
public ScrollPanel getScrollPanel() {
HeaderPanel header = (HeaderPanel) getWidget();
return (ScrollPanel) header.getContentWidget();
}
}
Run Code Online (Sandbox Code Playgroud)
初始化此工作所需的变量:
private MyDataGrid<MyDataType> myDataGrid;
private int incrementSize = 20;
private int lastScrollPos = 0;
Run Code Online (Sandbox Code Playgroud)
在构造函数中,创建网格:
myDataGrid = new MyDataGrid<MyDataType>();
Run Code Online (Sandbox Code Playgroud)
然后使用刚刚创建的getScrollPanel()引用添加ScrollHandler:
myDataGrid.getScrollPanel().addScrollHandler(new ScrollHandler(){
@Override
public void onScroll(ScrollEvent event) {
int oldScrollPos = lastScrollPos;
lastScrollPos = myDataGrid.getScrollPanel().getVerticalScrollPosition();
// If scrolling up, ignore the event.
if (oldScrollPos >= lastScrollPos) {
return;
}
//Height of grid contents (including outside the viewable area) - height of the scroll panel
int maxScrollTop = myDataGrid.getScrollPanel().getWidget().getOffsetHeight() -
myDataGrid.getScrollPanel().getOffsetHeight();
if(lastScrollPos >= maxScrollTop) {
myDataGrid.setVisibleRange(0,myDataGrid.getVisibleRange().getLength()+incrementSize);
}
}
});
Run Code Online (Sandbox Code Playgroud)