Skip to content

Instantly share code, notes, and snippets.

@johannest
Created February 20, 2020 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannest/1dd2faa2570935afe6ba175747741abd to your computer and use it in GitHub Desktop.
Save johannest/1dd2faa2570935afe6ba175747741abd to your computer and use it in GitHub Desktop.
package com.example.application;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.Push;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.theme.lumo.Lumo;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
@Route("scrollTo")
@Theme(Lumo.class)
@Push
public class GridScrollToTest extends VerticalLayout {
private Grid<Dto> grid = null;
private ListDataProvider<Dto> dataProvider;
public GridScrollToTest() {
setSizeFull();
ScheduledExecutorService scheduler = newSingleThreadScheduledExecutor();
Button addItem = new Button("Add");
addItem.addClickListener(buttonClickEvent -> {
dataProvider.getItems().add(getNewMockDto());
dataProvider.refreshAll();
Optional<UI> optionalUI = addItem.getUI();
if (optionalUI.isPresent()) {
UI ui = optionalUI.get();
scheduler.schedule(() -> {
ui.access(() -> {
grid.scrollToIndex(dataProvider.getItems().size());
});
}, 100, TimeUnit.MILLISECONDS);
}
});
grid = new Grid<>(Dto.class);
List<Dto> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add(getNewMockDto());
}
dataProvider = new ListDataProvider<>(data);
grid.setDataProvider(dataProvider);
add(addItem, grid);
}
private Dto getNewMockDto() {
return new Dto(UUID.randomUUID().toString());
}
public static class Dto {
private String uuid;
public Dto(String uuid) {
super();
this.uuid = uuid;
}
public String getUuid() {
return uuid;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment