-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I'm using a Tree with a MVVM form to display a two level hierarchy of data, like:
- Category 1
|--- Sub Category 1-1
- Category 2
|--- Sub Category 2-1
|--- Sub Category 2-2
- Category 3
|--- Sub Category 3-1
I've developed by own custom model and node classes to support this tree. The tree supports drag'n drop of items and the form has buttons for moving 'category' items up and down in the order.
However, when I try to move a category item that is not at the top to the top position I'm getting an error.
Here's the method that handles the 'move to top' button event handler...
@Command("moveCategoryTop")
@NotifyChange({"categories","selectedCategory"})
public void moveCategoryTop() {
// find the root node from the tree model
EisTreeModelNode aRoot = getCategories().getRoot();
// find the currently selected node
EisTreeModelNode aSelectedNode = getSelectedCategory();
// remove the selected node from the root child list
aRoot.removeChildNode(aSelectedNode);
// add the selected node back in as the first child of the root
aRoot.addChildNode(0, aSelectedNode);
// setup all top level nodes so that are open
openAllNodes();
// change selection to make it the new top category
getCategories().clearSelection();
int[] aSelectionPath = {0};
getCategories().addSelectionPath(aSelectionPath);
}
After completing the method the app throws an invalid index error inside my model - asking for a child node at an index position outside of what the parent node has.
It's as though after the drag'n'drop action has been completed the tree is trying to repaint itself using the model state BEFORE the drop action code shown above was executed.
Any ideas?
Regarding to move ndoe that is not at the top to the top, the code below works fine with ZK 6.5.1.1
zul
<zk>
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('test.TestVM')">
<tree model="@bind(vm.model)"
width="600px" height="200px"
onSelect="@command('keepSelectedNode')">
<treecols>
<treecol label="data" />
</treecols>
<template name="model" var="node">
<treeitem>
<treerow>
<treecell label="@bind(node.data)" />
</treerow>
</treeitem>
</template>
</tree>
<button label="move to top" onClick="@command('moveToTop')" />
</window>
</zk>
TestVM.java
package test;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;
/**
* tested with ZK 6.5.1.1
*
* @author benbai
*
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {
DefaultTreeModel _model;
DefaultTreeNode _selectedNode;
public TreeModel<DefaultTreeNode<String>> getModel () {
DefaultTreeNode root = new DefaultTreeNode(null,
new DefaultTreeNode[] {
new DefaultTreeNode("aaa"),
new DefaultTreeNode("bbb", new DefaultTreeNode[] {new DefaultTreeNode("ddd")}),
new DefaultTreeNode("ccc")
});
if (_model == null) {
_model = new DefaultTreeModel<String>(root);
}
return _model;
}
@Command
@NotifyChange("selectedNodePath")
public void keepSelectedNode (@ContextParam(ContextType.TRIGGER_EVENT) SelectEvent event) {
_selectedNode = ((Tree)event.getTarget()).getSelectedItem().getValue();
}
@Command
public void moveToTop () {
if (_selectedNode != null) {
DefaultTreeNode root = (DefaultTreeNode)_selectedNode.getModel().getRoot();
_selectedNode.getParent().remove(_selectedNode);
root.insert(_selectedNode, 0);
}
}
}
Regarding drag'n'drop issue, could you provide your scenario more details?
Asked: 2013-05-08 21:06:53 +0800
Seen: 79 times
Last updated: May 20 '13
Large Tree with dynamic columns
Tree width lost after changing col width on maximized panel
MVVM tree example to show add/insert/delete/move/drag?
Error writing 'checked' on type org.zkoss.zul.Checkbox [closed]
zk framework: how to load zul pages from WEB-INF under directory zul