我正在使用具有以下结构的Primefaces树:
节点1
节点1.1
节点1.2
节点1.3
现在,我想在Node 1.1和Node 1.2之间添加一个节点。我可以通过使用“add(int index,TreeNode element)”方法来实现。但为此我必须知道Node 1.1的索引。
有没有办法通过它的名字或任何其他方式获取Node的索引?
答案 0 :(得分:2)
这是我的示例,它提供了一种在特定节点内添加现在节点的简单方法。
XHTML
<h:form>
<p:tree value="#{treeBasicView.root}"
id="tree"
var="node"
dynamic="true">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:outputLabel value="add node : " />
<p:inputText id="newNodeName"
value="#{treeBasicView.newNodeName}"/>
<p:outputLabel value="to into : " />
<p:inputText id="selectedNodeName"
value="#{treeBasicView.selectedNodeName}"/>
<p:commandButton value="add child"
process="@this,newNodeName,selectedNodeName"
update="tree"
actionListener="#{treeBasicView.addNewNode}"/>
</h:form>
ManagedBean
@ManagedBean(name = "treeBasicView")
@ViewScoped
public class BasicView implements Serializable {
private TreeNode root;
private String selectedNodeName;
private String newNodeName;
public TreeNode getRoot() {
return root;
}
public String getSelectedNodeName() {
return selectedNodeName;
}
public void setSelectedNodeName(String selectedNodeName) {
this.selectedNodeName = selectedNodeName;
}
public String getNewNodeName() {
return newNodeName;
}
public void setNewNodeName(String newNodeName) {
this.newNodeName = newNodeName;
}
@PostConstruct
public void init() {
root = new DefaultTreeNode("Root", null);
TreeNode node0 = new DefaultTreeNode("Node 0", root);
TreeNode node1 = new DefaultTreeNode("Node 1", root);
TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);
TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
node1.getChildren().add(new DefaultTreeNode("Node 1.1"));
node00.getChildren().add(new DefaultTreeNode("Node 0.0.0"));
node00.getChildren().add(new DefaultTreeNode("Node 0.0.1"));
node01.getChildren().add(new DefaultTreeNode("Node 0.1.0"));
node10.getChildren().add(new DefaultTreeNode("Node 1.0.0"));
root.getChildren().add(new DefaultTreeNode("Node 2"));
}
public void addNewNode(ActionEvent event) {
findNode(root);
}
private void findNode(TreeNode node){
List<TreeNode> subChild = node.getChildren();
for (TreeNode treeNode : subChild) {
if(treeNode.getData().equals(selectedNodeName)){
treeNode.getChildren().add(new DefaultTreeNode(newNodeName));
break;
}
findNode(treeNode);
}
}
}
点击添加孩子后
您可以使用treeNode.getChildren().add(i, name)
等来应用此示例。
答案 1 :(得分:0)
这是另一个示例,它提供了一种在特定节点下添加现在节点的简单方法。
XHTML
<h:form>
<p:tree value="#{treeBasicView.root}"
id="tree"
var="node"
dynamic="true">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:outputLabel value="add node : " />
<p:inputText id="newNodeName"
value="#{treeBasicView.newNodeName}"/>
<p:outputLabel value="below : " />
<p:inputText id="selectedNodeName"
value="#{treeBasicView.selectedNodeName}"/>
<p:commandButton value="add child"
process="@this,newNodeName,selectedNodeName"
update="tree"
actionListener="#{treeBasicView.addNewNode}"/>
</h:form>
ManagedBean
@ManagedBean(name="treeBasicView")
@ViewScoped
public class BasicView implements Serializable {
private TreeNode root;
private TreeNode selectedNode;
private int position;
private String selectedNodeName;
private String newNodeName;
public TreeNode getRoot() {
return root;
}
public String getSelectedNodeName() {
return selectedNodeName;
}
public void setSelectedNodeName(String selectedNodeName) {
this.selectedNodeName = selectedNodeName;
}
public String getNewNodeName() {
return newNodeName;
}
public void setNewNodeName(String newNodeName) {
this.newNodeName = newNodeName;
}
@PostConstruct
public void init() {
root = new DefaultTreeNode("Root", null);
TreeNode node0 = new DefaultTreeNode("Node 0", root);
TreeNode node1 = new DefaultTreeNode("Node 1", root);
TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);
TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
node1.getChildren().add(new DefaultTreeNode("Node 1.1"));
node00.getChildren().add(new DefaultTreeNode("Node 0.0.0"));
node00.getChildren().add(new DefaultTreeNode("Node 0.0.1"));
node01.getChildren().add(new DefaultTreeNode("Node 0.1.0"));
node10.getChildren().add(new DefaultTreeNode("Node 1.0.0"));
root.getChildren().add(new DefaultTreeNode("Node 2"));
}
public void addNewNode(ActionEvent event) {
findNode(root);
selectedNode.getParent().getChildren().add(position, new DefaultTreeNode(newNodeName));
}
private void findNode(TreeNode node){
int pos = 0;
List<TreeNode> subChild = node.getChildren();
for (TreeNode treeNode : subChild) {
pos = pos + 1;
if(treeNode.getData().equals(selectedNodeName)){
position = pos;
selectedNode = treeNode;
}
findNode(treeNode);
}
}
}
点击添加孩子后