GWT: How do I populate an initial list for a CellTree using an AsyncDataModel?
I'm using GWT 2.4. I want to create a tree with a fixed set of top level nodes but upon opening each, the data is dynamically retrieved from the server. I have found the AsyncDataProvider class to help me, but I'm having trouble figuring out how to pre-populate the data model with an initial set of values. I have this code (not working) ...
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.
}
开发者_JAVA百科 };
// 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);
}
Nothing appears when I launch my application and I've confirmed that the initial cell list contains 6 items. Any ideas why they are not displaying? Is TextCell not the right type to use when constructing a CellList meant for a CellTree? - Dave
// 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());
}
}
CellTree via CellTreeNodeView calls setDataDisplay on the returned NodeInfo object with a self created NodeCellList instance, so it looks like it overrides your own CellList and that one never used.
Instead of creating a CellList that displays the values you should return a dataprovider that returns the initial values. I don't fully understand how you use the initial set, just as a placeholder or as the set of root nodes, so I'm not sure how your implemenation would look like, but you might take a look at the GWT CellTree showcase: http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTree, especially look at the source code of the ContactTreeViewModel class.
精彩评论