GWT:如何使用AsyncDataModel填充CellTree的初始列表?

时间:2011-10-04 21:12:05

标签: java gwt

我正在使用GWT 2.4。我想创建一个具有固定顶级节点集的树,但在打开每个节点时,将从服务器动态检索数据。我找到了AsyncDataProvider类来帮助我,但是我无法弄清楚如何用一组初始值预先填充数据模型。我有这个代码(不工作)......

public class CellTreeExample implements EntryPoint {

  /**
   * The model that defines the nodes in the tree.
   */
  private static class CustomTreeModel implements TreeViewModel {

    /**
     * Get the {@link NodeInfo} that provides the children of the specified
     * value.
     */
    public <T> NodeInfo<?> getNodeInfo(T value) {
      /*
       * Create some data in a data provider. Use the parent value as a prefix
       * for the next level.
       */
      AsyncDataProvider<String> dataProvider = new AsyncDataProvider<String>() { 
          @Override
          protected void onRangeChanged(HasData<String> display) {
            // Execute dynamic logic here.
      }
      };

      // Set a default set of nodes.
      TextCell textCell = new TextCell();
      final CellList<String> cellList = new CellList<String>(textCell);
      final List<String> rootNodes = getRootNodes();
      cellList.setRowCount(rootNodes.size(), true);
      dataProvider.addDataDisplay(cellList);

      // Return a node info that pairs the data with a cell.
      return new DefaultNodeInfo<String>(dataProvider, new TextCell());
    }

    public boolean isLeaf(Object value) {
        // some logic
    }
  }

  public void onModuleLoad() {
    // Create a model for the tree.
    TreeViewModel model = new CustomTreeModel();

    /*
     * Create the tree using the model. We specify the default value of the
     * hidden root node as "Item 1".
     */
    CellTree tree = new CellTree(model, "Item 1");

    // Add the tree to the root layout panel.
    RootLayoutPanel.get().add(tree);
  }

启动我的应用程序时没有显示任何内容,并且我已确认初始单元格列表包含6个项目。任何想法为什么他们不显示?在构建CellTree的CellList时,TextCell不是正确的类型吗? - 戴夫

2 个答案:

答案 0 :(得分:3)

// summarizing pseudocode
public <T> NodeInfo<?> getNodeInfo(final T value) {
  if(value == null) {  // root, return static list of top level nodes
    return new DefaultNodeInfo<String<( 
        new ListDataProvider<String>(Arrays.<String>asList("node1", "node2" ... ));
        , new TextCell());
  }
  else {
    AsyncDataProvider<String> dataProvider = new AsyncDataProvider<String>() { 
      @Override
      protected void onRangeChanged(HasData<String> display) {
        // Execute dynamic logic here - and fetch data from server or wherever
        // call updateRowData() in when data is available
        updateRowData(display.getVisibleRange(), /* List<String> results */);
      }
    }  

    return new DefaultNodeInfo<String>(dataProvider, new TextCell());
  }
}

答案 1 :(得分:0)

CellTree通过CellTreeNodeView使用自创建的NodeCellList实例调用返回的NodeInfo对象上的setDataDisplay,因此看起来它覆盖了您自己的CellList并且从未使用过。

您应该返回一个返回初始值的数据提供者,而不是创建显示值的CellList。我不完全理解你如何使用初始集,就像占位符或根节点集一样,所以我不确定你的实现会是什么样子,但是你可以看看GWT CellTree展示: http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTree,特别是看看ContactTreeViewModel类的源代码。