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.

components-combobox.asciidoc 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ---
  2. title: ComboBox
  3. order: 16
  4. layout: page
  5. ---
  6. [[components.combobox]]
  7. = [classname]#ComboBox#
  8. *_This section has not yet been updated for Vaadin Framework 8_*
  9. ifdef::web[]
  10. [.sampler]
  11. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/combo-box"]
  12. endif::web[]
  13. [classname]#ComboBox# is a selection component allows selecting an item from a
  14. drop-down list. The component also has a text field area, which allows entering
  15. search text by which the items shown in the drop-down list are filtered. Common
  16. selection component features are described in
  17. <<dummy/../../../framework/components/components-selection#components.selection,"Selection
  18. Components">>.
  19. .The [classname]#ComboBox# Component
  20. image::img/combobox-basic.png[width=35%, scaledwidth=50%]
  21. [[components.combobox.filtering]]
  22. == Filtered Selection
  23. [classname]#ComboBox# allows filtering the items available for selection in the
  24. drop-down list by the text entered in the input box.
  25. [[figure.components.combobox.filter]]
  26. .Filtered Selection in [classname]#ComboBox#
  27. image::img/combobox-filtering.png[width=35%, scaledwidth=50%]
  28. Pressing kbd:[Enter] will complete the item in the input box. Pressing kbd:[Up] and kbd:[Down] arrow keys can be used for selecting an item from the drop-down list. The
  29. drop-down list is paged and clicking on the scroll buttons will change to the
  30. next or previous page. The list selection can also be done with the arrow keys
  31. on the keyboard. The shown items are loaded from the server as needed, so the
  32. number of items held in the component can be quite large. The number of matching
  33. items is displayed by the drop-down list.
  34. Filtering is enabled by setting a __filtering mode__ with
  35. [methodname]#setFilteringMode()#.
  36. [source, java]
  37. ----
  38. cb.setFilteringMode(FilteringMode.CONTAINS);
  39. ----
  40. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.select.combobox.filtering[on-line example, window="_blank"].
  41. The modes defined in the [classname]#FilteringMode# enum are as follows:
  42. [parameter]#CONTAINS#:: Matches any item that contains the string given in the text field part of the
  43. component.
  44. [parameter]#STARTSWITH#:: Matches only items that begin with the given string.
  45. [parameter]#OFF# (default):: Filtering is by default off and all items are shown all the time.
  46. The above example uses the containment filter that matches to all items
  47. containing the input string. As shown in <<figure.components.combobox.filter>>
  48. below, when we type some text in the input area, the drop-down list will show
  49. all the matching items.
  50. [[components.combobox.newitems]]
  51. == Allowing Adding New Items
  52. [classname]#ComboBox# allows the user to add new items, when the user types
  53. in a value and presses kbd:[Enter]. You need to enable this with
  54. [methodname]#setNewItemHandler()#.
  55. Adding new items is not possible if the selection component is read-only. An
  56. attempt to do so may result in an exception.
  57. === Handling New Items
  58. Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
  59. item caption string as parameter for the [methodname]#accept(String)# method.
  60. [source, java]
  61. ----
  62. // List of planets
  63. List<Planet> planets = new ArrayList<>();
  64. planets.add(new Planet(1, "Mercury"));
  65. planets.add(new Planet(2, "Venus"));
  66. planets.add(new Planet(3, "Earth"));
  67. planets.add(new Planet(4, "Mars"));
  68. ComboBox<Planet> select =
  69. new ComboBox<>("Select or Add a Planet");
  70. select.setItems(planets);
  71. // Use the name property for item captions
  72. select.setItemCaptionGenerator(Planet::getName);
  73. // Allow adding new items and add
  74. // handling for new items
  75. select.setNewItemHandler(inputString -> {
  76. // Create a new bean - can't set all properties
  77. Planet newPlanet = new Planet(planets.size(), inputString);
  78. planets.add(newPlanet);
  79. // Update combobox content
  80. select.setItems(planets);
  81. // Remember to set the selection to the new item
  82. select.select(newPlanet);
  83. Notification.show("Added new planet called " +
  84. inputString);
  85. });
  86. ----
  87. [[components.combobox.css]]
  88. == CSS Style Rules
  89. [source, css]
  90. ----
  91. .v-filterselect { }
  92. .v-filterselect-input { }
  93. .v-filterselect-button { }
  94. // Under v-overlay-container
  95. .v-filterselect-suggestpopup { }
  96. .popupContent { }
  97. .v-filterselect-prevpage,
  98. .v-filterselect-prevpage-off { }
  99. .v-filterselect-suggestmenu { }
  100. .gwt-MenuItem { }
  101. .v-filterselect-nextpage,
  102. .v-filterselect-nextpage-off { }
  103. .v-filterselect-status { }
  104. ----
  105. In its default state, only the input field of the [classname]#ComboBox#
  106. component is visible. The entire component is enclosed in
  107. [literal]#++v-filterselect++# style (a legacy remnant), the input field has
  108. [literal]#++v-filterselect-input++# style and the button in the right end that
  109. opens and closes the drop-down result list has
  110. [literal]#++v-filterselect-button++# style.
  111. The drop-down result list has an overall
  112. [literal]#++v-filterselect-suggestpopup++# style. It contains the list of
  113. suggestions with [literal]#++v-filterselect-suggestmenu++# style. When there are
  114. more items that fit in the menu, navigation buttons with
  115. [literal]#++v-filterselect-prevpage++# and
  116. [literal]#++v-filterselect-nextpage++# styles are shown. When they are not
  117. shown, the elements have [literal]#++-off++# suffix. The status bar in the
  118. bottom that shows the paging status has [literal]#++v-filterselect-status++#
  119. style.