【问题标题】:GWT: How do I populate an initial list for a CellTree using an AsyncDataModel?GWT:如何使用 AsyncDataModel 填充 CellTree 的初始列表?
【发布时间】:2011-10-04 21:12:05
【问题描述】:

我正在使用 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 是否不适合使用? - 戴夫

【问题讨论】:

    标签: java gwt


    【解决方案1】:
    // 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());
      }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我希望初始集是预定义的根节点,但之后的所有内容都应该异步(动态)加载。在您列出的示例中,我无法弄清楚事物是如何动态加载的。似乎所有数据都是静态的。感谢您的任何其他想法,-戴夫
      • 在 getNodeInfo 的示例中,检查是否 value == null,这是根,如果返回 DefaultNodeInfo 和您的 AsyncDataProvider 实例,则在 else 部分。您的 onRangeChanged 应该使用动态检索的数据调用 updateRowData。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多