我有部门列表,每个部门可能有父级或没有,部门域对象如下:
- departmentId
- parentDepartmentId (null if current department has no parent i,e should be under root directly, and have value if current department have parent).
.
.
.
查看用于创建基本树的icefaces教程代码:
// create root node with its children expanded
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
IceUserObject rootObject = new IceUserObject(rootTreeNode);
rootObject.setText("Root Node");
rootObject.setExpanded(true);
rootTreeNode.setUserObject(rootObject);
// model is accessed by by the ice:tree component via a getter method, this object is what's needed in the view to display the tree
model = new DefaultTreeModel(rootTreeNode);
// add some child nodes
for (int i = 0; i <3; i++) {
DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
IceUserObject branchObject = new IceUserObject(branchNode);
branchObject.setText("node-" + i);
branchNode.setUserObject(branchObject);
rootTreeNode.add(branchNode);
}
这都是关于构建基本节点并添加子节点。
我的情况很复杂,根下的子A可能有子节点B、C、D和D,例如子节点等等。
所以我正在考虑如何完成类似事情的最佳实践,我需要示例代码或提示(如果有人可以提供帮助)。
最佳答案
您需要一种递归方法来从模型构建树。
public void buildRecursiveTreeNode(DefaultMutableTreeNode parentTreeNode,
String treeId, String treeName, GenericTreeVo modelVo)
{
// if the database model contains more children.
// add the current nodes first and pass in this nodes tree id and name to construct the children for this parent nodes.
}
更新了答案以包括递归示例。
http://www.danzig.us/java_class/recursion.html
刚刚添加了一个递归链接,我想说的是,当您从数据库迭代数据时,您会看到是否有任何子记录,如果您有子记录,您将通过传递 DefaultMutableTreeNode 来调用相同的方法,它将成为父记录。
关于java - JSF/ICEfaces 动态层次结构树示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170793/