You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datamodel-selection.asciidoc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ---
  2. title: Selecting items
  3. order: 5
  4. layout: page
  5. ---
  6. [[datamodel.selection]]
  7. = Selecting Items
  8. Listing components displaying data also support allowing the user to select items. Depending on component, it the user can select either one or several items at a time.
  9. How selection is handled in listings is split into three categories:
  10. * Single selection: components that only allow a single item to be selected at a time, for example `RadioButtonGroup` belongs in this category.
  11. More generally, all components that implement the `SingleSelect` interface belong in this category.
  12. * Multi selection: components that allow for selecting any number of the displayed items, for example `CheckBoxGroup`.
  13. All components that implement the `MultiSelect` interface belong in this category.
  14. * Listing components whose selection can be configured through the usage of the `SelectionModel` interface.
  15. The `Grid` component is an example of this type of listing and it currently has built in implementations for both the single selection and multi selection cases, as well as disabling selection altogether.
  16. == Single and Multi Selection
  17. Single and multi selection components implement the `HasValue` interface, where the current selection represents the value that is currently held by the component.
  18. In practice this means that it is possible to get, set and listen to selection changes the same way you would with value changes in Vaadin field components.
  19. In the case of single select components `HasValue` is further extended with `SingleSelect`, and correspondingly with `MultiSelect` in the case of multi select components, giving further control over the current selection.
  20. An example of basic single selection with the ComboBox component:
  21. [source, java]
  22. ----
  23. ComboBox<Availability> comboBox = new ComboBox<>();
  24. // Populate the combo box with items
  25. comboBox.setItems(EnumSet.allOf(Availability.class));
  26. // Set the current selection
  27. comboBox.setValue(Availability.DISCONTINUED);
  28. // Get the current selection
  29. Availability availability = comboBox.getValue();
  30. // Add a value change listener, a ValueChangeEvent<Availability> will be fired
  31. // any time a change to the selection is made.
  32. comboBox.addValueChangeListener(event -> Notification.show(event.getValue()));
  33. ----
  34. A similar example for the multi select listing `CheckBoxGroup` follows.
  35. A difference to note in this example is the parameter type of `setValue` and the return type of `getValue` being `Set<Category>`, the members of which represent the selection contents.
  36. [source, java]
  37. ----
  38. CheckBoxGroup<Category> checkBoxGroup = new CheckBoxGroup<>();
  39. checkBoxGroup.setItems(EnumSet.allOf(Category.class));
  40. checkBoxGroup.setValue(EnumSet.allOf(Category.class));
  41. Set<Category> categories = checkBoxGroup.getValue();
  42. checkBoxGroup.addValueChangeListener(event -> {
  43. Notification.show("Number of selected items: " + event.getValue().size());
  44. });
  45. ----
  46. Additionally, `MultiSelect` provides numerous utility functions for simpler programmatic handling of selections, such as:
  47. [source, java]
  48. ----
  49. checkBoxGroup.select(Category.DVD, Category.BOOK);
  50. checkBoxGroup.isSelected(Category.BOOK); // true
  51. checkBoxGroup.deselectAll();
  52. checkBoxGroup.getSelectedItems(); // now returns an empty set of Categories
  53. ----
  54. == Selection Models
  55. `Grid` component can hold either multi- or single- selection. Since grid can not be both `SingleSelect<SomePojo>` and
  56. `MultiSelect<SomePojo>` in the same time, grid itself is not a select component, but it delegates the selection to a subclass of `SelectionModel` class.
  57. By default, `Grid` is in single selection mode, and we can obtain selection object using `asSingleSelect` method.
  58. [source, java]
  59. ----
  60. Grid<Person> grid = new Grid<>();
  61. SingleSelect<Person> selection = grid.asSingleSelect();
  62. //...
  63. Notification.show(selection.getValue().getName() + " was selected");
  64. ----
  65. If selection of multiple rows is required, then `Grid` needs to be switched into multiselection mode, and multiple item
  66. selection object can be obtained using `asMultiSelect` method.
  67. [source, java]
  68. ----
  69. Grid<Person> grid = new Grid<>();
  70. grid.setSelectionMode(Grid.SelectionMode.MULTI);
  71. MultiSelect<Person> selection = grid.asMultiSelect();
  72. //...
  73. Notification.show(
  74. selection.getValue().stream().map(Person::getName).collect(Collectors.joining(", "))
  75. + " were selected");
  76. ----
  77. [[datamodel.selection.binder]]
  78. === Selected Items
  79. Selection models (subclasses of `SelectionModel`) allow retrieving a `HasValue` object corresponding to the selection with the `asSingleSelect` and `asMultiSelect` methods, and thus can be used bound to data using a `Binder`.
  80. This way, conversions and validation can be used for selections.
  81. [source, java]
  82. ----
  83. public static class Company {
  84. private Person boss;
  85. private Set<Person> managers;
  86. public Person getBoss() {
  87. return boss;
  88. }
  89. public void setBoss(Person boss) {
  90. this.boss = boss;
  91. }
  92. public Set<Person> getManagers() {
  93. return managers;
  94. }
  95. public void setManagers(Set<Person> managers) {
  96. this.managers = managers;
  97. }
  98. }
  99. Binder<Company> companyBinder = new Binder<>();
  100. //Setup single selection binding
  101. Grid<Person> bossGrid = new Grid<>();
  102. SingleSelect<Person> bossSelection = bossGrid.asSingleSelect();
  103. companyBinder.forField(bossSelection).bind(Company::getBoss, Company::setBoss);
  104. //Setup multi selection binding
  105. Grid<Person> managersGrid = new Grid<>();
  106. managersGrid.setSelectionMode(Grid.SelectionMode.MULTI);
  107. MultiSelect<Person> managersSelection = managersGrid.asMultiSelect();
  108. companyBinder.forField(managersSelection).bind(Company::getManagers, Company::setManagers);
  109. ----
  110. [[datamodel.selection.events]]
  111. === Selection Events
  112. `SelectionModel` implementations allow retrieving a `HasValue` object corresponding to the selection with the `asSingleSelect` and `asMultiSelect` methods. The `HasValue` implementations returned by those methods support the standard `addValueChangeListener` method and
  113. all added listeners are notified about any selection change. In addition, selections support their own, selection-specific listeners,
  114. `SelectionListener`, `SingleSelectionListener`, and `MultiSelectionListener`. To add those listeners, we need to explicitly cast a selection to
  115. `SingleSelectionModel`, or `MultiSelectionModel` respectively.