具有动态DataProvider的GWT CellList

时间:2015-08-11 10:15:57

标签: gwt dataprovider celllist

是否可以制作标准的CellList,他正在使用动态DataProvider? 关于搜索引擎Google的相似性

我浏览页面,CellList从服务器中提取信息。 在标准示例中,开头是完整的DataProvider

每页行数 - 10 我可以从服务器获得的项目数。

1 个答案:

答案 0 :(得分:1)

由于每次按NextPrevious时数据都来自服务器,您可以使用Async Data Provider

这是example

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;

public class CellTableExample implements EntryPoint {

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private final String address;
    private final Date birthday;
    private final String name;

    public Contact(String name, Date birthday, String address) {
      this.name = name;
      this.birthday = birthday;
      this.address = address;
    }
  }

  /**
   * The list of data to display.
   */
  @SuppressWarnings("deprecation")
  private static final List<Contact> CONTACTS = Arrays.asList(
      new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"), 
      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"), 
      new Contact("Tom", new Date(85, 3, 22), "33 Lance Ln"), 
      new Contact("Jack", new Date(85, 4, 22), "44 Lance Ln"), 
      new Contact("Tim", new Date(85, 5, 22), "55 Lance Ln"), 
      new Contact("Mike", new Date(85, 6, 22), "66 Lance Ln"), 
      new Contact("George", new Date(46, 6, 6),"77 Lance Ln"));

  public void onModuleLoad() {
    // Create a CellTable.
    final CellTable<Contact> table = new CellTable<Contact>();
    // Display 3 rows in one page
    table.setPageSize(3);

    // Add a text column to show the name.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.name;
      }
    };
    table.addColumn(nameColumn, "Name");

    // Add a date column to show the birthday.
    DateCell dateCell = new DateCell();
    Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
      @Override
      public Date getValue(Contact object) {
        return object.birthday;
      }
    };
    table.addColumn(dateColumn, "Birthday");

    // Add a text column to show the address.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.address;
      }
    };
    table.addColumn(addressColumn, "Address");

    // Associate an async data provider to the table
    // XXX: Use AsyncCallback in the method onRangeChanged
    // to actaully get the data from the server side
    AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>() {
      @Override
      protected void onRangeChanged(HasData<Contact> display) {
        int start = display.getVisibleRange().getStart();
        int end = start + display.getVisibleRange().getLength();
        end = end >= CONTACTS.size() ? CONTACTS.size() : end;
        List<Contact> sub = CONTACTS.subList(start, end);
        updateRowData(start, sub);
      }
    };
    provider.addDataDisplay(table);
    provider.updateRowCount(CONTACTS.size(), true);

    SimplePager pager = new SimplePager();
    pager.setDisplay(table);

    VerticalPanel vp = new VerticalPanel();
    vp.add(table);
    vp.add(pager);

    // Add it to the root panel.
    RootPanel.get().add(vp);
  }
}
// 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);
      }
    };