vaadin / vaadin-grid-flow

Vaadin Flow Java API for vaadin/vaadin-grid Web Component
https://vaadin.com/components/vaadin-grid
Other
14 stars 16 forks source link

TreeGrid - strange behaviour with >50 children #1093

Closed pellmann closed 4 years ago

pellmann commented 4 years ago

I use the code from https://github.com/vaadin/vaadin-grid-flow/issues/1087 and add this to generate many children:

        TestNode third = root.addChild(new TestNode(root, "third"));
        for (int i = 0; i < 60; i++) {
            third.addChild(new TestNode(third, "third.sub." + i));
        }

The strange effect is, that the items will be like following (with increments of i for the ellipses):

.third.sub.0.
.third.sub.1.
(...)
.third.sub.49.
.third.sub.0.
(...)
.third.sub.9.

So after 50 something very strange happens.

jcgueriaud1 commented 4 years ago

Hi,

fetchChildren is fetching only one page (50 elements). In the example code you have this:

        @Override
        public Stream<TestNode> fetchChildren(HierarchicalQuery<TestNode, String> query) {
            TestNode parent = query.getParent();
            if (parent == null) {
                return Stream.of(root);
            }
            return parent.getChildren().stream();
        }

query contains an offset, so the correct fetchChildren is something like that:

@Override
        public Stream<TestNode> fetchChildren(HierarchicalQuery<TestNode, String> query) {
            TestNode parent = query.getParent();
            if (parent == null) {
                return Stream.of(root);
            }
            return parent.getChildren().subList(query.getOffset(), Integer.min(query.getOffset() + query.getLimit(), parent.getChildren().size() ) ).stream();
        }

Unfortunately the demos of the tree grid doesn't contain the offset.

pellmann commented 4 years ago

Thank you @jcgueriaud1! That makes a lot of sense and fixes my problems.