使用asyncdata提供程序的CellTable

时间:2014-01-26 16:30:12

标签: gwt gwt-celltable

我正在使用AsyncDataProvider生成CellTable,如本例所示。 http://www.mytechtip.com/2010/11/gwt-celltable-example-using_8168.html

我正在使用第二种方法,您可以调用远程服务来获取此处提到的记录。

// Associate an async data provider to the table
AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>() {
  @Override
  protected void onRangeChanged(HasData<Contact> display) {
    final int start = display.getVisibleRange().getStart();
    int length = display.getVisibleRange().getLength();
    AsyncCallback<List<Contact>> callback = new AsyncCallback<List<Contact>>() {
      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
      }
      @Override
      public void onSuccess(List<Contact> result) {
        updateRowData(start, result);
      }
    };
    // The remote service that should be implemented
    remoteService.fetchPage(start, length, callback);
  }
}

在我的remoteService.fetchPage()方法中返回1000条记录,我不知道如何在页面上显示50条记录。

1 个答案:

答案 0 :(得分:0)

您可以创建一个中间缓冲区,以便每50个结果返回一次,直到它结束。那时只有它回到服务器以进行下一个请求

new AsyncDataProvider<Contact>() { 
  List<Contact> buffer;
  int currentLastIndex;

  @Override
  protected void onRangeChanged(HasData<Contact> display) {
    if( currentLastIndex % 1000 != 0) {  
       //return 50 contacts from buffer
       updateRowData( currentLastIndex, getResultFromBuffer() ); 
    } else {
   AsyncCallback<List<Contact>> callback = new AsyncCallback<List<Contact>>() {
      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
      }
      @Override
      public void onSuccess(List<Contact> result) {
        buffer.addAll(result);
        updateRowData(currentLastIndex, getResultFromBuffer());
      }
    };
    // The remote service that should be implemented
    }

List<Contact> getResultFromBuffer() {
   //I didn't check the javadoc for sublist, but somehow this way
  return buffer.sublist( currentLastIndex, currentLastIndex +50 ); 
}
  }