0

I am using AsyncDataProvider to generate the CellTable as mentioned in this example. http://www.mytechtip.com/2010/11/gwt-celltable-example-using_8168.html

I am using second approach where you call remote service to fetch the records as mentioned here.

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

In my remoteService.fetchPage() method return 1000 record and I am not sure how to display 50 record on the page.

Ashish
  • 14,295
  • 21
  • 82
  • 127

1 Answers1

0

You can create an intermediate buffer to return the results per 50 until it reaches the end. At that time only it gets back to the server for the next request

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 ); 
}
  }
Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40