use of com.vaadin.flow.component.listbox.dataview.ListBoxListDataView in project flow-components by vaadin.
the class ListBoxViewDemoPage method addItemRenderer.
private void addItemRenderer() {
ListBox<Item> listBox = new ListBox<>();
ListBoxListDataView<Item> listDataView = listBox.setItems(getItems());
listBox.setRenderer(new ComponentRenderer<>(item -> {
Label name = new Label("Item: " + item.getName());
Label stock = new Label("In stock: " + item.getStock());
NativeButton button = new NativeButton("Buy", event -> {
item.setStock(item.getStock() - 1);
listDataView.refreshItem(item);
});
Div labels = new Div(name, stock);
Div layout = new Div(labels, button);
labels.getStyle().set("display", "flex").set("flexDirection", "column").set("marginRight", "10px");
layout.getStyle().set("display", "flex").set("alignItems", "center");
return layout;
}));
listBox.setItemEnabledProvider(item -> item.getStock() > 0);
addCard("Using item renderer and disabling items", listBox).setId("list-box-with-renderer");
}
use of com.vaadin.flow.component.listbox.dataview.ListBoxListDataView in project flow-components by vaadin.
the class ListBoxDataViewPage method createSetSortComparatorDataView.
private void createSetSortComparatorDataView() {
Item first = new Item(1L, FIRST);
Item second = new Item(2L, SECOND);
Item third = new Item(3L, THIRD);
ListBox<Item> listBoxForSortDataView = new ListBox<>();
listBoxForSortDataView.setId(LIST_BOX_FOR_SORT_DATA_VIEW);
ListBox<Item> otherListBox = new ListBox<>();
otherListBox.setId(OTHER_LIST_BOX_FOR_SORT_DATA_VIEW);
final ListDataProvider<Item> dataProvider = DataProvider.ofItems(third, first, second);
ListBoxListDataView<Item> dataView = listBoxForSortDataView.setItems(dataProvider);
otherListBox.setItems(dataProvider);
NativeButton dataViewSortButton = new NativeButton("Sort", click -> dataView.setSortComparator((item1, item2) -> item1.getValue().compareToIgnoreCase(item2.getValue())));
dataViewSortButton.setId(LIST_DATA_VIEW_SORT_BUTTON);
add(listBoxForSortDataView, otherListBox, dataViewSortButton);
}
use of com.vaadin.flow.component.listbox.dataview.ListBoxListDataView in project flow-components by vaadin.
the class ListBoxDataViewPage method createFilterItemsByDataView.
private void createFilterItemsByDataView() {
ListBox<Integer> numbers = new ListBox<>();
numbers.setId(LIST_BOX_FOR_FILTER_DATA_VIEW);
ListBox<Integer> otherNumbers = new ListBox<>();
otherNumbers.setId(OTHER_LIST_BOX_FOR_FILTER_DATA_VIEW);
final ListDataProvider<Integer> dataProvider = DataProvider.ofItems(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
ListBoxListDataView<Integer> numbersDataView = numbers.setItems(dataProvider);
otherNumbers.setItems(dataProvider);
NativeButton filterOdds = new NativeButton("Filter Odds", click -> numbersDataView.setFilter(num -> num % 2 == 0));
filterOdds.setId(LIST_DATA_VIEW_SET_FILTER_BUTTON);
NativeButton filterMultiplesOfThree = new NativeButton("Filter Multiples of 3", click -> numbersDataView.addFilter(num -> num % 3 != 0));
filterMultiplesOfThree.setId(LIST_DATA_VIEW_ADD_FILTER_BUTTON);
NativeButton noFilter = new NativeButton("No Filter", click -> numbersDataView.removeFilters());
noFilter.setId(LIST_DATA_VIEW_REMOVE_FILTER_BUTTON);
add(numbers, otherNumbers, filterOdds, filterMultiplesOfThree, noFilter);
}
use of com.vaadin.flow.component.listbox.dataview.ListBoxListDataView in project flow-components by vaadin.
the class ListBoxDataViewPage method createIdentifierProviderForMultiSelectListBox.
private void createIdentifierProviderForMultiSelectListBox() {
CustomItem first = new CustomItem(1L, "First");
CustomItem second = new CustomItem(2L, "Second");
CustomItem third = new CustomItem(3L, "Third");
CustomItem fourth = new CustomItem(4L, "Fourth");
List<CustomItem> items = new ArrayList<>(Arrays.asList(first, second, third, fourth));
MultiSelectListBox<CustomItem> multiSelectListBox = new MultiSelectListBox<>();
ListBoxListDataView<CustomItem> listDataView = multiSelectListBox.setItems(items);
// Setting the following Identifier Provider makes the component
// independent from the CustomItem's equals method implementation:
listDataView.setIdentifierProvider(CustomItem::getId);
Set<CustomItem> selected = new HashSet<>(Arrays.asList(new CustomItem(1L), third));
multiSelectListBox.setValue(selected);
Span selectedIdsSpan = new Span();
selectedIdsSpan.setId(MULTI_SELECT_LIST_BOX_SELECTED_IDS_SPAN);
multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
Button updateAndSelectByIdButton = new Button("Update & Select by Id", click -> {
// Make the names of unselected items similar to the name of
// selected
// one to mess with the <equals> implementation in
// CustomItem:
second.setName("First");
listDataView.refreshItem(second);
fourth.setName("Third");
listDataView.refreshItem(fourth);
// Select the items not only with the reference of existing
// items,
// but also the Id to verify <equals> is not in use and the
// selection is happening only based on identifier:
Set<CustomItem> newSelected = new HashSet<>(Arrays.asList(second, new CustomItem(4L)));
multiSelectListBox.setValue(newSelected);
multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
});
updateAndSelectByIdButton.setId(MULTI_SELECT_LIST_BOX_SELECTION_UPDATE_BUTTON);
Button selectByIdAndNameButton = new Button("Select by Id and Name", click -> {
// Select the items not only with the reference of existing
// items,
// but also the Id and a challenging name to verify <equals>
// is not
// in use and the selection is happening only based on
// identifier:
Set<CustomItem> newSelected = new HashSet<>(Arrays.asList(first, new CustomItem(3L, "Third")));
multiSelectListBox.setValue(newSelected);
multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
});
selectByIdAndNameButton.setId(MULTI_SELECT_LIST_BOX_SELECTION_BY_ID_AND_NAME_BUTTON);
add(multiSelectListBox, selectedIdsSpan, updateAndSelectByIdButton, selectByIdAndNameButton);
}
Aggregations