2016-02-04 64 views
0

我在Vaadin頗爲新穎,並面臨下一個問題。我試圖用一些屬性對樹進行分組來表示我的實體列表。一切都很好,直到第一級的項目。我做了什麼:從實體列表中的Vaadin樹

MyEntity myEntity1 = new MyEntity(1l, "prop1", "sub_prop0") 
MyEntity myEntity2 = new MyEntity(2l, "prop1", "sub_prop1") 
MyEntity myEntity3 = new MyEntity(3l, "prop2", "sub_prop2") 

BeanContainer<Long, MyEntity > entityContainer = new BeanContainer<>(MyEntity .class); 
marketContainer.setBeanIdProperty("prop"); // prop is property name for the second value in constructor 
entityContainer.addBean(myEntity1); 
entityContainer.addBean(myEntity2); 
entityContainer.addBean(myEntity3) 

Tree markets = new Tree("Markets"); 
markets.setContainerDataSource(entityContainer); 

因此我得到樹與2項目:prop1和prop2但沒有別的。實際上,我唯一需要補充的是將具有值的sup元素作爲子項目形成另一個屬性。

在此先感謝。

回答

1

看看文檔和發現的例子here

簡而言之:

設置的子對象

// Set it to be a child. 
tree.setParent(l2, l1); 

如果沒有允許兒童的父母,告訴它這樣

/* Make the moons look like leaves. */ 
tree.setChildrenAllowed(l2, false); 

如果你想在定義Hierachie容器,然後使用其中一個容器執行HierarchicalContainer。 您還可以使用setParent(...)方法來指定關係。

+0

感謝您的回答。這是否意味着containerDataSource,在我的情況下是BeanContainer,並且樹中的關係必須以不同的方式配置?我的意思是我有提供我的數據源,迭代它並配置父/子關係? – Marian

+0

如果你想在容器級別定義它,那麼你需要使用'HierarchicalContainer'這裏記錄https://vaadin.com/api/com/vaadin/data/util/HierarchicalContainer.html。在那裏,你還必須爲孩子定義setParent(....)(你可以在Tree組件中) –

+0

再次感謝,看起來像是用父母和孩子構建一棵Tree,只有HierarchicalContainer是合適的數據源,但不是BeanContainer,不是嗎? – Marian