2012-09-04 25 views
1

Primefaces TreeTable節點似乎以相反的方式工作。從數據庫將內容加載到TreeTable中

您創建一個父級,然後創建一個孩子,而不是告訴孩子它屬於哪個父級。父母應該瞭解自己的孩子,而不是其他方式。

所以...我的問題是:

我需要從我的數據庫(祖父母,父母,子女及孫子女)加載數據的不確定ammount的。

如何將它們加載到TreeTable中?我想不出有什麼辦法可以創建TreeNodes並將其設置爲父母。

任何人都可以給我和想法?

+0

將引用存儲到父代而不是子集的引用是更優化的。這樣你只有一個引用而不是一個引用集合。維護關係比較容易,在數據庫中這會很相似。也許這解釋了它背後的一些邏輯;-) – siebz0r

回答

1

我還沒有測試過這個解決方案,但我認爲它應該可以工作。 primefaces展示的例子都是硬編碼的。爲了使其動態化,您必須使用陣列。
因此,例如,下面的硬編碼的一段代碼:

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

可以替換爲:

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

我將它們添加到地圖的原因是,如果你想引用它們,您可以使用該鍵作爲參考。 現在如果你想節點添加到圖片節點例如

//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")); 
} 

使用這種方法,您可以遞歸通過儘可能多層次地循環。我還沒有能夠測試這個代碼(只是記事本++),因爲我目前在辦公室。但試試看,如果你仍然陷入困境,我可以在幾個小時後回家時做更詳細的事情。

+0

好吧!現在感謝!我會測試它! – MBarni

+0

我被困在遞交TreeTable所需的遞歸中...... – MBarni