0
votes

I have a problem with creating parent-child link in my TreeTable.

Setting of DataSource

table.setContainerDataSource(new TempPeopleContainer(((MyUI) UI.getCurrent()).peopleService.getItemList()));
table.setParent(1,0);

How can I set id of Object in this kind of DataSource setting? I've no explicit Id for elements of TreeTable.

Here is example from vaadin , where you can see "clearly" definition of Id's for each element (code not from my app):

 TreeTable ttable = new TreeTable("My TreeTable");
    ttable.addContainerProperty("Name", String.class, null);
    ttable.addContainerProperty("Number", Integer.class, null);
    ttable.setWidth("20em");

    // Create the tree nodes and set the hierarchy
    ttable.addItem(new Object[]{"Menu", null}, 0);
    ttable.addItem(new Object[]{"Beverages", null}, 1);
    ttable.setParent(1, 0);
    ttable.addItem(new Object[]{"Foods", null}, 2);
    ttable.setParent(2, 0);

it's my TempPeopleContainer class definition:

private class TempPeopleContainer extends FilterableListContainer<People> {
    public TempPeopleContainer(final Collection<People> collection) {
        super(collection);
    }

    // This is only temporarily overridden until issues with
    // BeanComparator get resolved.
    @Override
    public void sort(final Object[] propertyId, final boolean[] ascending) {
        final boolean sortAscending = ascending[0];
        final Object sortContainerPropertyId = propertyId[0];
        Collections.sort(getBackingList(), (o1, o2) -> {
            int result = 0;
            if ("lastname".equals(sortContainerPropertyId)) {
                result = o1.getLastname().compareTo(o2.getLastname());
            }
            if (!sortAscending) {
                result *= -1;
            }
            return result;
        });
    }
}

I hope my question is clear. Thanks.

1

1 Answers

0
votes

It depends on your ItemContainer. The common BeanItemContainer from vaadin uses the item as item id itself as documented here https://vaadin.com/api/com/vaadin/data/util/BeanItemContainer.html

You are using vitrin's org.vaadin.viritin.ListContainer acting like the BeanItemContainer

So you could use the items from your list as itemId/newParentId if you want to stick at your ItemContainer implementations.

Or you could go the long way and get the item ids by iterating over com.vaadin.data.Container.getItemIds() and check manually if this item id is the parent id you want to use.

edit:

Try something like this:

List myList = ((MyUI) UI.getCurrent()).peopleService.getItemList();
TempPeopleContainer container = new TempPeopleContainer(myList);
table.setContainerDataSource(container);
table.setParent(myList.get(1), myList.get(0));