TreeGrid and AbstractBackEndHierarchicalDataProvider

I want to use the TreeGrid with DataProvider.

I follow the example of https://vaadin.com/components/vaadin-tree-grid/java-examples:

TreeGrid<Account> grid = new TreeGrid<>();
grid.addHierarchyColumn(Account::toString).setHeader("Account Title");
grid.addColumn(Account::getCode).setHeader("Code");

HierarchicalDataProvider dataProvider =
        new AbstractBackEndHierarchicalDataProvider<Account, Void>() {

    @Override
    public int getChildCount(HierarchicalQuery<Account, Void> query) {
        return (int) accountService.getChildCount(query.getParent());
    }

    @Override
    public boolean hasChildren(Account item) {
        return accountService.hasChildren(item);
    }

    @Override
    protected Stream<Account> fetchChildrenFromBackEnd(
            HierarchicalQuery<Account, Void> query) {
        return accountService.fetchChildren(query.getParent()).stream();
    }
};

grid.setDataProvider(dataProvider);
add(grid);

But it does’nt work for my Example. Is there anything missing in the code-snippet like setItems? Since, the dataProvider methods depends on query.getParent() but what about the root-Items? Where I have to fetch the root-items?

I hope you can help me.

I added some Code like

@Override
public int getChildCount(HierarchicalQuery<Category, Void> query) {

                        CatalogProductDTO parent = query.getParent();

                        if(parent==null){
                              return service.getCategories().size();
}

 @Override
  protected Stream<Category> fetchChildrenFromBackEnd(HierarchicalQuery<Category, Void> query) {
 if(parent == null){
   return service.getCategories.stream();
}

and it works :slight_smile: