use of com.vaadin.flow.component.virtuallist.VirtualList in project flow-components by vaadin.
the class VirtualListViewPage method createRankedListWithEventHandling.
private void createRankedListWithEventHandling() {
VirtualList<String> list = new VirtualList<>();
list.setHeight("400px");
list.getStyle().set("border", "1px solid lightgray");
List<String> items = getLordOfTheRingsCharacters();
list.setItems(items);
/*
* The name of the event handlers defined at 'on-click' are used inside
* the 'withEventHandler' calls.
*/
list.setRenderer(TemplateRenderer.<String>of("<div style='display:flex; justify-content:space-between; padding:10px;'>" + "<div style='flex-grow:1'>#[[item.rank]]: [[item.name]]</div>" + "<div><button on-click='up' hidden='[[item.upHidden]]'>↑</button>" + "<button on-click='down' hidden='[[item.downHidden]]'>↓</button>" + "<button on-click='remove' style='color:red'>X</button></div>" + "<div>").withProperty("name", ValueProvider.identity()).withProperty("rank", item -> items.indexOf(item) + 1).withProperty("upHidden", item -> items.indexOf(item) == 0).withProperty("downHidden", item -> items.indexOf(item) == items.size() - 1).withEventHandler("up", item -> {
int previousRank = items.indexOf(item);
if (previousRank == 0) {
return;
}
String previousItem = items.set(previousRank - 1, item);
items.set(previousRank, previousItem);
list.getDataCommunicator().reset();
}).withEventHandler("down", item -> {
int previousRank = items.indexOf(item);
if (previousRank == items.size() - 1) {
return;
}
String previousItem = items.set(previousRank + 1, item);
items.set(previousRank, previousItem);
list.getDataCommunicator().reset();
}).withEventHandler("remove", item -> {
items.remove(item);
list.getDataCommunicator().reset();
}));
list.setId("using-events-with-templates");
add(new Div(new Text("Rank up/down your favorite Lord of the Rings characters")), list, new NativeButton("Reset", evt -> {
items.clear();
items.addAll(getLordOfTheRingsCharacters());
list.getDataCommunicator().reset();
}));
}
use of com.vaadin.flow.component.virtuallist.VirtualList in project flow-components by vaadin.
the class VirtualListViewPage method createPeopleListWithDataProviderAndComponentRenderer.
private void createPeopleListWithDataProviderAndComponentRenderer() {
/* VirtualList that uses the component above */
VirtualList<Person> list = new VirtualList<>();
list.setHeight("400px");
list.getStyle().set("border", "1px solid lightgray");
DataProvider<Person, ?> dataProvider = DataProvider.ofCollection(createListOfPeople());
list.setDataProvider(dataProvider);
// Uses the constructor of the PersonCard for each item in the list
list.setRenderer(new ComponentRenderer<>(PersonCard::new));
// For a smooth scrolling experience use a placeholder item
Person placeholder = new Person();
placeholder.setFirstName("-----");
list.setPlaceholderItem(placeholder);
NativeButton switchEnabled = new NativeButton("Switch enabled state", event -> list.setEnabled(!list.isEnabled()));
list.setId("list-of-people-with-dataprovider-and-component-renderer");
switchEnabled.setId("switch-enabled-people-list");
add(new Div(new Text("List of people with grid layout")), list, switchEnabled);
}
use of com.vaadin.flow.component.virtuallist.VirtualList in project flow-components by vaadin.
the class VirtualListViewPage method createDisabledStringsList.
private void createDisabledStringsList() {
VirtualList<String> list = new VirtualList<>();
list.setHeight("400px");
list.getStyle().set("border", "1px solid lightgray");
Div removalResult = new Div();
removalResult.setId("disabled-removal-result");
DataProvider<String, ?> dataProvider = DataProvider.fromCallbacks(query -> queryStringsFromDatabase(query), query -> countStringsFromDatabase(query));
list.setDataProvider(dataProvider);
// Disable the list so that scrolling still works but events are not
// handled
list.setEnabled(false);
/*
* The name of the event handlers defined at 'on-click' are used inside
* the 'withEventHandler' calls.
*/
list.setRenderer(TemplateRenderer.<String>of("<div style='display:flex; justify-content:space-between; padding:10px;'>" + "<div style='flex-grow:1'>[[item.name]]</div>" + "<div><button on-click='removeItem' style='color:red'>X</button></div>" + "<div>").withProperty("name", ValueProvider.identity()).withEventHandler("removeItem", item -> {
removalResult.setText(item);
}));
NativeButton switchEnabled = new NativeButton("Switch enabled state", event -> list.setEnabled(!list.isEnabled()));
list.setId("disabled-list-with-templates");
switchEnabled.setId("switch-enabled-state-string-list");
add(new Div(new Text("Rank up/down your favorite Lord of the Rings characters")), list, removalResult, switchEnabled);
}
use of com.vaadin.flow.component.virtuallist.VirtualList in project flow-components by vaadin.
the class VirtualListPage method createTemplateFromRendererWithPeople.
private void createTemplateFromRendererWithPeople() {
VirtualList<Person> list = new VirtualList<>();
list.setHeight("100px");
List<Person> people = createPeople(3);
DataProvider<Person, ?> dataProvider = DataProvider.ofCollection(people);
list.setDataProvider(dataProvider);
list.setRenderer(TemplateRenderer.<Person>of("[[item.name]] - [[item.age]] - [[item.user]]").withProperty("name", Person::getName).withProperty("age", Person::getAge).withProperty("user", person -> person.getName().toLowerCase().replace(" ", "_")));
NativeButton update = new NativeButton("Update item 1", evt -> {
Person item = people.get(0);
item.setName(item.getName() + " Updated");
list.getDataProvider().refreshItem(item);
});
list.setId("template-renderer-with-people");
update.setId("template-renderer-with-people-update-item");
add(list, update);
}
use of com.vaadin.flow.component.virtuallist.VirtualList in project flow-components by vaadin.
the class VirtualListPage method createTemplateWithEventHandlers.
private void createTemplateWithEventHandlers() {
VirtualList<String> list = new VirtualList<>();
list.setHeight("100px");
Div message = new Div();
List<String> items = new ArrayList<>(Arrays.asList("Clickable item 1", "Clickable item 2", "Clickable item 3"));
list.setRenderer(TemplateRenderer.<String>of("<div on-click='remove' id='template-events-item-[[index]]'>[[item.label]]</div>").withProperty("label", ValueProvider.identity()).withEventHandler("remove", item -> {
items.remove(item);
list.getDataCommunicator().reset();
message.setText(item + " removed");
}));
list.setItems(items);
list.setId("template-events");
message.setId("template-events-message");
add(list, message);
}
Aggregations