我遇到了与smartgwt树结构相关的问题。
我正在使用来自DB的值创建一个树(名为nodesTree
),并且每个节点都设置了一个名称。
例如,我有4个节点(数字是节点的名称):
1
2
3
4
for (TreeNode node : nodesTree.getAllNodes())
System.out.print(node.getName());
输出:1 2 3 4
如果我在节点1下拖动节点4(与2相同的级别)
1
2
4
3
我希望得到节点的顺序: 1 2 4 3
但是如果我重复上面的“for”语句,输出将与上面相同:1 2 3 4
。< / p>
TreeGrid.refreshFields()
不起作用。
我正在使用smartgwt 2.4。
谢谢。
稍后修改:
手动填充树。我正在从代码中手动将节点添加到树中,并根据我从DB收到的一些值。
Tree nodesTree = new Tree();
TreeGrid navTreeGrid = new TreeGrid();
navTreeGrid.setData(nodesTree);
navTreeGrid.setCanReorderRecords(true);
navTreeGrid.setCanReparentNodes(true);
稍后编辑(2011年5月15日): 还有一些代码:
private static final String NAVPATH_ROOT = "/";
// Tree structure
Tree nodesTree = new Tree();
nodesTree.setRootValue(NAVPATH_ROOT);
nodesTree.setModelType(TreeModelType.PARENT);
TreeGrid navTreeGrid = new TreeGrid();
navTreeGrid.setCanReorderRecords(true);
navTreeGrid.setCanReparentNodes(true);
navTreeGrid.setData(nodesTree);
// add first node
TreeNode newNode = new TreeNode();
newNode.setName("a");
nodesTree.add(newNode, NAVPATH_ROOT);
// add the second node
newNode = new TreeNode();
newNode.setName("ab");
nodesTree.add(newNode, NAVPATH_ROOT);
// add the third node to the tree
newNode = new TreeNode();
newNode.setName("abc");
nodesTree.add(newNode, NAVPATH_ROOT);
navTreeGrid.addDropHandler(new DropHandler()
{
@Override
public void onDrop(DropEvent event)
{
resetNodesAttributes();
});
// I want in this method to have the order of nodes after drag&drop action was made.
// for example if I drag& drop node "abc" under "a" I want the method below to post
// nodes in this order: a abc ab
public void resetNodesAttributes(){
for (TreeNode node : nodesTree.getAllNodes()) {
System.out.println("---:" + node.getName());
}
}
稍后修改:
我已添加DataChangedHandler
到Tree
小部件,如下所示:
nodesTree.addDataChangedHandler(new com.smartgwt.client.widgets.tree.DataChangedHandler()
{
@Override
public void onDataChanged(com.smartgwt.client.widgets.tree.DataChangedEvent dataChangedEvent)
{
resetNodesAttributes();
}
});
工作正常,但是当我拖延时例如,在节点“a”下输出节点“abc”,输出为:
a ab a abc ab a abc ab
所以,似乎方法resetNodesAttributes
被调用了3次 - 我不想要这个 - 我只需要调用一次。此外,节点的名称有时会发生变化,而且我不希望调用此方法。
稍后编辑(2月18日):
package de.vogella.gwt.helloworld.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.events.DropEvent;
import com.smartgwt.client.widgets.events.DropHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;
public class HelloGwt implements EntryPoint {
private static final String NAVPATH_ROOT = "/";
Tree nodesTree = null;
@Override
public void onModuleLoad() {
// Tree structure
nodesTree = new Tree();
nodesTree.setRootValue(NAVPATH_ROOT);
nodesTree.setModelType(TreeModelType.PARENT);
TreeGrid navTreeGrid = new TreeGrid();
navTreeGrid.setCanReorderRecords(true);
navTreeGrid.setCanReparentNodes(true);
navTreeGrid.setData(nodesTree);
// add first node
TreeNode newNode = new TreeNode();
newNode.setName("a");
nodesTree.add(newNode, NAVPATH_ROOT);
// add the second node
newNode = new TreeNode();
newNode.setName("ab");
nodesTree.add(newNode, NAVPATH_ROOT);
// add the third node to the tree
newNode = new TreeNode();
newNode.setName("abc");
nodesTree.add(newNode, NAVPATH_ROOT);
navTreeGrid.addDropHandler(new DropHandler() {
@Override
public void onDrop(DropEvent event) {
resetNodesAttributes();
}
});
RootPanel.get().add(navTreeGrid);
}
public void resetNodesAttributes() {
// here I want to have the new order of nodes after d&d action was made
for (TreeNode node : nodesTree.getAllNodes()) {
System.out.println(node.getName());
}
}
}
答案 0 :(得分:4)
将输出包裹在DeferredCommand
中,您将获得正确的输出。
public class HelloGwt implements EntryPoint
{
public void onModuleLoad()
{
Tree tree = new Tree();
tree.setModelType(TreeModelType.PARENT);
final TreeGrid grid = new TreeGrid();
grid.setCanReorderRecords(true);
grid.setCanReparentNodes(true);
grid.setData(tree);
tree.add(createNode("a"), tree.getRoot());
tree.add(createNode("b"), tree.getRoot());
tree.add(createNode("c"), tree.getRoot());
grid.addFolderDropHandler(new FolderDropHandler(){
public void onFolderDrop(FolderDropEvent event)
{
DeferredCommand.addCommand(new Command()
{
public void execute()
{
for (ListGridRecord node : grid.getRecords())
System.out.println(Tree.nodeForRecord(node).getName());
}});
}});
RootPanel.get().add(grid);
}
private TreeNode createNode(String name)
{
TreeNode node = new TreeNode();
node.setName(name);
node.setID(name);
return node;
}
}