Hi,
before 14.3.8 a scrollTo call in the initial rendering (afterNavigation) would scroll the items in Grid and TreeGrid. Now I can’t find a way to scroll initially.
Expected: Items expand and scroll
Actual: No scroll happens (14.3.8)
Workaournd: Couldn’t find one (beforeClientResponse etc.)
Is that behaviour already known? Are there any possible workarounds? Sample Code:
public class GridScrollUi extends HorizontalLayout implements AfterNavigationObserver {
private final List<Double> values;
public GridScrollUi() {
super();
setDefaultVerticalComponentAlignment(Alignment.STRETCH);
values = DoubleStream.generate(Math::random)
.filter(item -> item != Double.MIN_VALUE)
.limit(10000)
.boxed()
.collect(Collectors.toList());
}
@Override
public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
addGrid();
addTreeGrid();
}
private void addGrid() {
VerticalLayout layout = new VerticalLayout();
Grid<Double> grid = new Grid<>();
for (int i = 0; i < 10; i++) {
grid.addColumn(String::valueOf);
}
grid.setItems(values);
layout.add(grid);
layout.add(new Button("Scroll to first", event1 -> {
scrollTo(grid, values, 0);
}));
layout.add(new Button("Scroll to last", event -> {
scrollTo(grid, values, values.size() - 1);
}));
add(layout);
// Initial scroll to bottom
scrollTo(grid, values, values.size() - 1);
}
private void addTreeGrid() {
VerticalLayout layout = new VerticalLayout();
List<Double> roots = new ArrayList<>();
roots.add(Double.MIN_VALUE);
TreeGrid<Double> treeGrid = new TreeGrid<>();
treeGrid.addHierarchyColumn(item -> item);
treeGrid.setItems(roots, value -> {
if (value == null) {
return roots;
} else if (value == Double.MIN_VALUE) {
return values;
} else {
return new ArrayList<>();
}
});
layout.add(treeGrid);
layout.add(new Button("Scroll to first", event1 -> {
scrollTo(treeGrid, values, 0);
}));
layout.add(new Button("Scroll to last", event -> {
scrollTo(treeGrid, values, values.size() - 1);
}));
add(layout);
// Initial scroll to bottom
scrollTo(treeGrid, values, values.size() - 1);
}
private void scrollTo(TreeGrid<Double> treeGrid, List<Double> list, int i) {
treeGrid.expand(Double.MIN_VALUE);
treeGrid.select(list.get(i));
treeGrid.scrollToIndex(i);
}
private void scrollTo(Grid<Double> grid, List<Double> list, int i) {
grid.select(list.get(i));
grid.scrollToIndex(i);
}
}
Thank you