数据库中的Primefaces树 [英] Primefaces tree from database

查看:56
本文介绍了数据库中的Primefaces树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体类:

@Entity
@Table(name = "THE_TREE", catalog = "", schema = "dbo")
public class TheTree implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "NODE_NAME")
private String name;

@Column(name = "LEVEL")
private int level;

@OneToMany    
@JoinColumn(name="PARENTID")    
public List<TheTree > children = new LinkedList<TheTree >();

我想用素数树来表示,但是我做对了. primefaces 网站中给出的示例具有静态节点,该节点具有预定义的深度,其中我需要深度未知的节点,并需要从数据库中填充节点.我在这里看到过各种帖子,但我不清楚.在此帖子中,作者似乎提出了相同的问题,但答案是与问题无关.任何解决方案将不胜感激.

I would like to represent this into primefaces tree, but I cant get it right. The example given in primefaces website has static nodes with predefined depth, where I need nodes with unknown depth and to be filled from database. I have seen various posts here but nothing is clear to me. In this post it seems the author has asked the same question but the answer is not relative to the question somehow. Any solution would be appreciated.

推荐答案

您必须创建一个递归函数来制作树.这就是我的方法:

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

 }

这篇关于数据库中的Primefaces树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆