2011-10-04 31 views
0

我正在使用GWT 2.4。我想創建一個具有固定頂層節點集的樹,但是在打開每個樹時,都會從服務器動態檢索數據。我找到了AsyncDataProvider類來幫助我,但我無法弄清楚如何預先使用一組初始值填充數據模型。我有這樣的代碼(不工作)...當我啓動我的應用程序GWT:我如何使用AsyncDataModel填充CellTree的初始列表?

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不是正確的類型嗎? - Dave

回答

0

CellTree通過CellTreeNodeView調用setDataDisplay方法返回的NodeInfo對象上有一個自創的NodeCellList實例,所以它看起來像覆蓋了你自己的CellList,並且那個從來沒有用過。

不是創建一個顯示值的CellList,而是應該返回一個返回初始值的數據提供者。我不完全瞭解你如何使用初始集合,只是作爲佔位符或作爲根節點集合,所以我不確定你的實現如何,但你可以看看GWT CellTree展示: http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTree,尤其要看ContactTreeViewModel類的源代碼。

+0

我想初始集是預定義的根節點,但之後的所有內容都應該異步(動態)加載。在你列舉的例子中,我無法弄清楚事情是如何動態加載的。好像所有的數據都是靜態的。感謝您的任何其他想法, - Dave – Dave

+0

在getNodeInfo的例子中,如果value == null,那麼這是根的一個檢查,如果返回DefaultNodeInfo與AsyncDataProvider實例,則在else的部分。您的onRangeChanged應該使用動態檢索的數據調用updateRowData。 –

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