【问题标题】:Primefaces Dynamic TreenodePrimefaces 动态树节点
【发布时间】:2014-02-03 07:15:51
【问题描述】:

我正在尝试设置一个动态树节点来完成像 Reddit 这样的评论系统。不熟悉的可以参考这个:http://www.reddit.com/r/videos/comments/1wv4tt/guy_runs_on_camera_during_super_bowl_post_game/

我现在在数据库中的是一个看起来像这样的评论表

id parent content
1 0       text1
2 0       text2
3 1       child of text 1
4 3       child to the child of text 1

因此评论面板将如下所示

- text1
  - child of text1
    - child to the child of text 1
- text2

我无法想象如何将此表格设计与 Primefaces Treenode Java 代码结合起来,如果有人能指出我正确的方向,那就太好了。我已经尝试在论坛和谷歌上搜索,但找不到我能理解的解决方案。

我可能应该提到,由于这是用户生成的内容,我的表中没有固定的结构,因此后端代码应该能够动态处理它

感谢您的帮助。

谢谢, 博恩

【问题讨论】:

  • 无论如何,我最终通过这样做解决了我自己的问题。 Map treeNodeMap = new HashMap(); for(Comment comment : cmets) { TreeNode node = null; //表示顶级注释 if(comment.getParent()==0) { node = new DefaultTreeNode(comment.getContent(), root); } else { node = new DefaultTreeNode(comment.getContent(), treeNodeMap.get(comment.getParent())); } treeNodeMap.put(comment.getId(), node); }
  • 将其发布为答案而不是评论。
  • 这个项目,treeDS4j,因为版本 2.1 旨在实现您在问题中描述的内容。

标签: dynamic primefaces treenode


【解决方案1】:

我是这样解决的,使用递归方法和上表结构:

public TreeNode createDocuments() {
    TreeNode rootNode = new DefaultTreeNode(new Document(), null);
    List<Document> documentRootNodeList = dao.getDocumentsRoot();
    for (Document doc : documentRootNodeList) {
        TreeNode node = new DefaultTreeNode(doc, rootNode);
        createSubNode(doc, node);
    }
    return rootNode;
}

public void createSubNode(Document doc, TreeNode node) {
    List<Document> documentList = dao.getDocumentsNode(doc);
    for (Document subDoc : documentList) {
        TreeNode subNode = new DefaultTreeNode(subDoc, node);
        createSubNode(subDoc, subNode);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多