Search in sources :

Example 6 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider 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);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Image(com.vaadin.flow.component.html.Image) ValueProvider(com.vaadin.flow.function.ValueProvider) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Random(java.util.Random) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) SecureRandom(java.security.SecureRandom) HashSet(java.util.HashSet) Faker(com.github.javafaker.Faker) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) Text(com.vaadin.flow.component.Text) Query(com.vaadin.flow.data.provider.Query) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Set(java.util.Set) Serializable(java.io.Serializable) List(java.util.List) Stream(java.util.stream.Stream) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) Collections(java.util.Collections) NativeButton(com.vaadin.flow.component.html.NativeButton) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) Text(com.vaadin.flow.component.Text)

Example 7 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider 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);
}
Also used : IntStream(java.util.stream.IntStream) Text(com.vaadin.flow.component.Text) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) NativeButton(com.vaadin.flow.component.html.NativeButton) VirtualList(com.vaadin.flow.component.virtuallist.VirtualList)

Example 8 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider 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);
}
Also used : ListBoxListDataView(com.vaadin.flow.component.listbox.dataview.ListBoxListDataView) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) AbstractDataProvider(com.vaadin.flow.data.provider.AbstractDataProvider) Set(java.util.Set) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) ListBox(com.vaadin.flow.component.listbox.ListBox) AtomicReference(java.util.concurrent.atomic.AtomicReference) ListBoxDataView(com.vaadin.flow.component.listbox.dataview.ListBoxDataView) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) DataProvider(com.vaadin.flow.data.provider.DataProvider) Span(com.vaadin.flow.component.html.Span) NativeButton(com.vaadin.flow.component.html.NativeButton) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) ListBox(com.vaadin.flow.component.listbox.ListBox)

Example 9 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider 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);
}
Also used : ListBoxListDataView(com.vaadin.flow.component.listbox.dataview.ListBoxListDataView) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) AbstractDataProvider(com.vaadin.flow.data.provider.AbstractDataProvider) Set(java.util.Set) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) ListBox(com.vaadin.flow.component.listbox.ListBox) AtomicReference(java.util.concurrent.atomic.AtomicReference) ListBoxDataView(com.vaadin.flow.component.listbox.dataview.ListBoxDataView) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) DataProvider(com.vaadin.flow.data.provider.DataProvider) Span(com.vaadin.flow.component.html.Span) NativeButton(com.vaadin.flow.component.html.NativeButton) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) ListBox(com.vaadin.flow.component.listbox.ListBox)

Example 10 with DataProvider

use of com.vaadin.flow.data.provider.DataProvider in project flow-components by vaadin.

the class IronListTestPage method createTemplateFromRendererWithPeople.

private void createTemplateFromRendererWithPeople() {
    IronList<Person> list = new IronList<>();
    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);
}
Also used : IntStream(java.util.stream.IntStream) LocalDateRenderer(com.vaadin.flow.data.renderer.LocalDateRenderer) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) LocalDateTime(java.time.LocalDateTime) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) NumberRenderer(com.vaadin.flow.data.renderer.NumberRenderer) LocalDateTimeRenderer(com.vaadin.flow.data.renderer.LocalDateTimeRenderer) List(java.util.List) NativeButtonRenderer(com.vaadin.flow.data.renderer.NativeButtonRenderer) Locale(java.util.Locale) DataProvider(com.vaadin.flow.data.provider.DataProvider) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) LocalDate(java.time.LocalDate) IronList(com.vaadin.flow.component.ironlist.IronList) HasComponents(com.vaadin.flow.component.HasComponents) Renderer(com.vaadin.flow.data.renderer.Renderer) NativeButton(com.vaadin.flow.component.html.NativeButton) IronList(com.vaadin.flow.component.ironlist.IronList)

Aggregations

DataProvider (com.vaadin.flow.data.provider.DataProvider)11 Query (com.vaadin.flow.data.provider.Query)9 Div (com.vaadin.flow.component.html.Div)8 NativeButton (com.vaadin.flow.component.html.NativeButton)8 Route (com.vaadin.flow.router.Route)8 List (java.util.List)8 Stream (java.util.stream.Stream)8 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)7 Arrays (java.util.Arrays)7 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Component (com.vaadin.flow.component.Component)4 Serializable (java.io.Serializable)4 Collections (java.util.Collections)4 ComponentUtil (com.vaadin.flow.component.ComponentUtil)3 HasHelper (com.vaadin.flow.component.HasHelper)3 HasLabel (com.vaadin.flow.component.HasLabel)3