Tree component Load Data programmatic

While Working with Tree component ,

I want to create Tree component programmatic and loading data programmatic inside controller

While following the below link

Tree Component

which is using the legacy version and tried to simulate the same on cuba 7 base

TreeEntity is Meta Data not persistence entity

below snippet is my screen

Injecting treeDc inside my screen as well tree component filling treeDc
with list of TreeEntity

below is snippet of my controller

i’m not able to load treeDc as it doesn’t have loader

Just i need only way to make tree appear on the screen with it’s data populated to it .

Hello!

As your Tree has already bounded with dataContainer it is enough just to add items to your CollectionContainer and entities will be shown.

For instance, we have the following screen desciptor:

<data>
    <collection id="treeDc" class="com.company.ftree.entity.TreeEntity"/>
</data>
<layout>
    <tree id="tree"
          width="100%"
          height="100%"
          dataContainer="treeDc"
          hierarchyProperty="parent"/>
</layout>

And in the controller we can populate our container:

@Inject
private CollectionContainer<TreeEntity> treeDc;
@Inject
private Metadata metadata;

@Subscribe
public void onInit(InitEvent event) {
    TreeEntity parent1 = metadata.create(TreeEntity.class);
    parent1.setName("parent1");
    treeDc.getMutableItems().add(parent1);

    TreeEntity parent2 = metadata.create(TreeEntity.class);
    parent2.setName("parent2");
    parent2.setParent(parent1);
    treeDc.getMutableItems().add(parent2);
}

Demo project: tree.zip (77.5 KB)

1 Like