简体   繁体   English

从数据库将内容加载到TreeTable中

[英]Loading content into TreeTable from database

Primefaces TreeTable nodes seems to work in a reverse way. Primefaces TreeTable节点似乎以相反的方式工作。

You create a parent, then you create a child, than you tell the child what parent it belongs to. 创建一个父母,然后创建一个孩子,然后告诉孩子该孩子属于哪个父母。 The parents should know their children, not the other way. 父母应该了解自己的孩子,而不是其他。

So... my problem is: 所以...我的问题是:

I need to load an undefined ammount of data from my database(grandparents, parents, children and grand children). 我需要从数据库(祖父母,父母,子女和孙子女)中加载未定义的数据量。

How to dinamically load them into the TreeTable? 如何将它们动态地加载到TreeTable中? I can't think of a way inside my for-each to create TreeNodes and set it`s parents. 我想不出在for-each内部创建TreeNodes并设置其父级的方法。

Anyone can give me and idea? 任何人都可以给我和想法吗?

I haven't tested out this solution yet, but I think it should work. 我尚未测试过该解决方案,但我认为它应该可以工作。 the examples on the primefaces showcase are all hard coded. primefaces展示柜上的示例均采用硬编码。 To make it dynamic, You'll have to work with Arrays. 为了使其动态,您必须使用数组。
So for example the section of hardcoded code below: 因此,例如下面的硬编码部分:

 TreeNode documents = new DefaultTreeNode(new Document("Documents", "-", "Folder"), root);  
 TreeNode pictures = new DefaultTreeNode(new Document("Pictures", "-", "Folder"), root);  
 TreeNode music = new DefaultTreeNode(new Document("Music", "-", "Folder"), root);

Can be replaced with: 可以替换为:

 Map<String, TreeNode> rootNodes = new HashMap<String, TreeNode>();

 //Retrieve the list of root Nodes. eg Tables in the database.
 for(Table table : databaseTables){

    //table.getTableName() will be the name of the node, it could be something like "music" or "documents"
    rootNodes.add(table.getTableName(), new DefaultTreeNode(table.getTableName(), new Document......, root);
 }

The reason I add them to the map is so that if you want to refer to them, you can use the key as a reference. 我将它们添加到地图的原因是,如果要引用它们,则可以使用该键作为参考。 now If you want to add nodes to the pictures node for example 现在,例如,如果您要将节点添加到图片节点

 //Create a new map
 Map<String, TreeNode> pictureNodes = new HashMap<String, TreeNode>();

 //now loop through your picture objects
for(picture : pictures){
    pictureNode.add(new DefaultTreeNode(picture.getName(), ......., root.get("pictures"));
}

Using this method, you can recursively loop through as many hierarchies as possible. 使用此方法,您可以递归地循环遍历尽可能多的层次结构。 I haven't been able to test this code (just notepad++) because I'm currently at the office. 我无法测试此代码(仅记事本++),因为我目前在办公室。 but try it out, if you're still stuck I can do something more detailed when I get home in a few hours. 但是请尝试一下,如果您仍然遇到问题,我几个小时后回家可以做些更详细的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM