问题
I have a problem related to a smartgwt Tree structure.
I'm creating a Tree (called nodesTree
) with values from a DB, and each node has a name set up.
For ex I'm having 4 nodes (the numbers are the names of the nodes):
1
2
3
4
for (TreeNode node : nodesTree.getAllNodes())
System.out.print(node.getName());
Output: 1 2 3 4
If I drag node 4 under node 1(at the same level as 2)
1
2
4
3
I want to have the order of nodes: 1 2 4 3
but if I repeat the above "for" statement the output will be the same as above: 1 2 3 4
.
TreeGrid.refreshFields()
does not work.
I'm using smartgwt 2.4.
Thank you.
Later edit:
The tree is manually populated. I'm adding the nodes to the tree manually from code, accoding to some values that I'm receiving from DB.
Tree nodesTree = new Tree();
TreeGrid navTreeGrid = new TreeGrid();
navTreeGrid.setData(nodesTree);
navTreeGrid.setCanReorderRecords(true);
navTreeGrid.setCanReparentNodes(true);
Later edit (15 febr. 2011): Some more code:
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());
}
}
Later edit:
I've added a DataChangedHandler
to Tree
widget like this:
nodesTree.addDataChangedHandler(new com.smartgwt.client.widgets.tree.DataChangedHandler()
{
@Override
public void onDataChanged(com.smartgwt.client.widgets.tree.DataChangedEvent dataChangedEvent)
{
resetNodesAttributes();
}
});
It is working fine but when I drag& drop for example node "abc" under node "a" the output is:
a ab a abc ab a abc ab
so, seems that the method resetNodesAttributes
it is called 3 times - and I do not want this - I need to be called just one time. Also the name of nodes are changing sometimes and than I do not want this method to be called.
Later edit (18 february):
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());
}
}
}
回答1:
Wrap your output in a DeferredCommand
, and you'll get the correct output.
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;
}
}
来源:https://stackoverflow.com/questions/4967266/tree-dragdrop-nodes-refresh