我正在尝试设置一个动态树节点来实现像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代码结合在一起,如果有人能为我指明正确的方向,那就太好了。我试着在论坛和谷歌上搜索,但找不到一个我能理解的解决方案。
我可能应该提一下,因为这是用户生成的内容,所以我的表中没有固定的结构,因此后端代码应该能够动态处理它
我很感谢你的帮助。
谢谢,布恩
发布于 2015-05-13 07:06:41
我用递归方法和上面的表结构解决了这个问题:
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);
}
}https://stackoverflow.com/questions/21521676
复制相似问题