Get the Index of the Node in a Primefaces Tree

后端 未结 2 1979
天涯浪人
天涯浪人 2021-01-07 13:01

I am using a Primefaces Tree which has a following structure :

Node 1

Node 1.1

Node 1.2

Node 1.3

Now, I want to add a node in betwee

相关标签:
2条回答
  • 2021-01-07 13:35

    This is another example which present a easy way to add now node below specific node.

    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);
            }
        }
    }
    

    Figure 1

    After click add child

    Figure 2

    0 讨论(0)
  • 2021-01-07 13:43

    This is my example which present a easy way to add now node inside specific node.

    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);
            }
        }
    }
    

    Figure 1

    After click add child Figure 3

    You can apply with this example by using treeNode.getChildren().add(i, name) and so on.

    0 讨论(0)
提交回复
热议问题