TreeGrid add Nodes manualy

Hi!

is there a way to add nodes to a treegrid manually? I dont get it to work with the examples from the documentation. I need to setup the tree manually

TreeGrid gets its data from a DataProvider. When you add a new item to the DataProvider, and call dataProvider.refreshAll(), the TreeGrid gets a new node.

Okay thank you, that sounds easy. Do you have any example? Is that the HyrachicalDataProvider?

I think the last aswer hear sound like what i need
https://stackoverflow.com/questions/60490142/vaadin-10-tree-grid-hierarchies-and-how-they-work

Here’s a simple example:

public class DepartmentView extends Div {

    private Department selected = null;

    public DepartmentView() {
        DepartmentData departmentData = new DepartmentData();
        TreeGrid<Department> treeGrid = new TreeGrid<>();
        List<Department> rootDepartments = departmentData.getRootDepartments();
        TreeData<Department> treeData = new TreeData<>();
        treeData.addItems(rootDepartments, departmentData::getChildDepartments);
        TreeDataProvider<Department> dataProvider = new TreeDataProvider(treeData);
        treeGrid.setDataProvider(dataProvider);
        treeGrid.addHierarchyColumn(Department::getName)
                .setHeader("Department Name");
        treeGrid.addSelectionListener(e -> {
            selected = e.getFirstSelectedItem().orElse(null);
        });
        Button button = new Button("Add child to selected", e -> {
            Department department = new Department("New department", selected);
            treeData.addItem(selected, department);
            departmentData.addDepartment(department);
            dataProvider.refreshAll();
        });

        add(treeGrid);
        add(button);
    }

}

Thank you very very much!

My Plan is following in the next step to show Categories (like in a webshop). Without using the HyrachicalDataProvider:

  1. get from DB the root Category (exists always)
  2. Select all Subcategories for root and add them to the root

now a tree is shown


root
    subcat1
    subcat2
  1. selectListeners on all Subcategories, when selected it should have a look if there are subsubcategories for the selected subcategorie and show them

That should be possible or? I dont want to use the Lazy Hyrachical DataProvider because i dont have enough experience at the moment to maintain it if there occur an error.

Is there a way to show the “expand icon” always even if there is no sucategory already? Showing this icon show the user, that he can click and expand the treenode (if no subnode is available, nothing will be shown. Or a trennode named “there is nothing else”)

I guess that should work, although you’re sort of reinventing a lazy hierarchical dataprovider there (without the benefit of being actually lazy loading).

As for the expand icon, I think its presence is determined by whether a node has children or not.

Got it to run with the Lazy Interface:)

Do you have an idea how to archive, that there appear a horizontal scrollbar?
https://ibb.co/djXKzBL

public ProductView() {
        setId("products-view");
        MainView.setCurrentDocumentionName(DocumentationEnum.CATEGORIES_VIEW);
        SplitLayout layout = new SplitLayout();
        layout.setOrientation(SplitLayout.Orientation.HORIZONTAL);
        layout.setSplitterPosition(25);

        layout.addToPrimary(createCategoriesTree());
        layout.addToSecondary(createCategoriesTree());
        add(layout);
    }

    private Component createCategoriesTree() {
        TreeGrid<ProductCategory> grid = new TreeGrid<>();
        grid.addHierarchyColumn(ProductCategory::getCategoryName).setHeader("Name");
        ProductCategory root = productCategoriesService.getRootCategory();
        HierarchicalDataProvider dataProvider = new AbstractBackEndHierarchicalDataProvider<ProductCategory, Void>() {

            @Override
			....