01. Introduction
Listings are components that display one or more attributes from a list of items.DataProviders provide the list of items to the Listing components.
Direct memory loading or lazy loading are the mechanisms for loading the list into the listing components.
Callbacks are used in the listing components for defining how the attributes are displayed in the listing elements. Remember the grid.addColumn(Person:getName) method.
setItem() method enables to load data into the listing component
// Sets items as a collection comboBox.setItems(EnumSet.allOf(Status.class)); // Sets items using varargs grid.setItems( new Person("George Washington", 1732), new Person("John Adams", 1735), new Person("Thomas Jefferson", 1743), new Person("James Madison", 1751) );
ListDataProvider is a supplier of data that can be used simultaneously by several list components.
02. In-memory data
02.01. Sorting data directly on the grid (listing component)
We can supply a Comparator. Note this method does not work in lazy loading
grid.addColumn(Person::getName).setHeader("Name") // Override default natural sorting .setComparator(Comparator .comparing(person -> person.getName().toLowerCase()));
02.02. Sorting elements directly in the DataProvider
Note that when you sort the DataProvider directly, all the listing components attached to it will update themselves.ListDataProvider<Person> dataProvider = DataProvider.ofCollection(persons); dataProvider.setSortOrder(Person::getName, SortDirection.ASCENDING); Grid<Person> grid = new Grid<>(Person.class); // The grid shows the persons sorted by name grid.setDataProvider(dataProvider); // Makes the combo box show persons in descending order button.addClickListener(event -> { dataProvider.setSortOrder(Person::getName, SortDirection.DESCENDING); });
02.03 Filtering (in-memory data)
The methods addFilter and setFilter can be used to filter data in the DataProvider; addFilter can be stacked several times while setFilter can only be used once.In this example, the combo shows only persons from a selected department.
ListDataProvider<Person> dataProvider = DataProvider.ofCollection(persons); ComboBox<Person> comboBox = new ComboBox<>(); comboBox.setDataProvider(dataProvider); departmentSelect.addValueChangeListener(event -> { Department selectedDepartment = event.getValue(); if (selectedDepartment != null) { dataProvider.setFilterByValue(Person::getDepartment, selectedDepartment); } else { dataProvider.clearFilters(); } }); // Makes the combo box show persons in descending order button.addClickListener(event -> { dataProvider.setSortOrder(Person::getName, SortDirection.DESCENDING); });
The methods refreshAll() and refreshItems() enables to notify to listing components when any data has been changed.
ListDataProvider<Person> dataProvider = new ListDataProvider<>(persons); Button addPersonButton = new Button("Add person", clickEvent -> { persons.add(new Person("James Monroe", 1758)); dataProvider.refreshAll(); }); Button modifyPersonButton = new Button("Modify person", clickEvent -> { Person personToChange = persons.get(0); personToChange.setName("Changed person"); dataProvider.refreshItem(personToChange); });
03. Lazy Loading
This is somewhat complicated
No hay comentarios:
Publicar un comentario