Primefaces tree from database

前端 未结 1 1088
长发绾君心
长发绾君心 2021-01-15 21:37

I have the following entity class :

@Entity
@Table(name = \"THE_TREE\", catalog = \"\", schema = \"dbo\")
public class TheTree implements Serializable {
priv         


        
相关标签:
1条回答
  • 2021-01-15 22:00

    You have to create a recursive function to make the tree. This is how I would do it:

    @ManagedBean
    @ViewScoped
    public class TreeMBean {
    
        private TreeNode rootNode;
    
         @PostConstruct
         public void init() {
             TheTree root = new TheTree(); // instead get root object from database 
             rootNode = newNodeWithChildren(root, null);
         }
    
         /**
          *  recursive function that returns a new node with its children
         */
         public TreeNode newNodeWithChildren(TheTree ttParent, TreeNode parent){
              TreeNode newNode= new DefaultTreeNode(ttParent, parent);
              for (TheTree tt : ttParent.getChildren()){
                   TreeNode newNode2= newNodeWithChildren(tt, newNode);
              }
              return newNode;
         }
    
         public TreeNode getRootNode() {
             return rootNode;
         }
    
         public void setRootNode(TreeNode node) {
             rootNode = node;
         }
    
     }
    
    0 讨论(0)
提交回复
热议问题