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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ---
  2. title: ComboBox
  3. order: 16
  4. layout: page
  5. ---
  6. [[components.combobox]]
  7. = ComboBox
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/combo-box"]
  11. endif::web[]
  12. [classname]#ComboBox# is a selection component allows selecting an item from a
  13. drop-down list. The component also has a text field area, which allows entering
  14. search text by which the items shown in the drop-down list are filtered. Common
  15. selection component features are described in
  16. <<dummy/../../../framework/components/components-selection#components.selection,"Selection
  17. Components">>.
  18. .The [classname]#ComboBox# Component
  19. image::img/combobox-basic.png[width=35%, scaledwidth=50%]
  20. [[components.combobox.filtering]]
  21. == Filtered Selection
  22. [classname]#ComboBox# allows filtering the items available for selection in the
  23. drop-down list by the text entered in the input box. To apply custom filtering, the [methodname]#setItems(CaptionFilter, Collection)# can be used. When using a [classname]#DataProvider#, the filtering is delegated to it.
  24. [[figure.components.combobox.filter]]
  25. .Filtered Selection in [classname]#ComboBox#
  26. image::img/combobox-filtering.png[width=35%, scaledwidth=50%]
  27. 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
  28. drop-down list is paged and clicking on the scroll buttons will change to the
  29. next or previous page. The list selection can also be done with the arrow keys
  30. on the keyboard. The shown items are loaded from the server as needed, so the
  31. number of items held in the component can be quite large. The number of matching
  32. items is displayed by the drop-down list.
  33. [[components.combobox.newitems]]
  34. == Allowing Adding New Items
  35. [classname]#ComboBox# allows the user to add new items, when the user types
  36. in a value and presses kbd:[Enter]. You need to enable this with
  37. [methodname]#setNewItemHandler()#.
  38. Adding new items is not possible if the selection component is read-only. An
  39. attempt to do so may result in an exception.
  40. === Handling New Items
  41. Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
  42. item caption string as parameter for the [methodname]#accept(String)# method.
  43. [source, java]
  44. ----
  45. // List of planets
  46. List<Planet> planets = new ArrayList<>();
  47. planets.add(new Planet(1, "Mercury"));
  48. planets.add(new Planet(2, "Venus"));
  49. planets.add(new Planet(3, "Earth"));
  50. ComboBox<Planet> select =
  51. new ComboBox<>("Select or Add a Planet");
  52. select.setItems(planets);
  53. // Use the name property for item captions
  54. select.setItemCaptionGenerator(Planet::getName);
  55. // Allow adding new items and add
  56. // handling for new items
  57. select.setNewItemHandler(inputString -> {
  58. Planet newPlanet = new Planet(planets.size(), inputString);
  59. planets.add(newPlanet);
  60. // Update combobox content
  61. select.setItems(planets);
  62. // Remember to set the selection to the new item
  63. select.select(newPlanet);
  64. });
  65. ----
  66. [[components.combobox.css]]
  67. == CSS Style Rules
  68. [source, css]
  69. ----
  70. .v-filterselect { }
  71. .v-filterselect-input { }
  72. .v-filterselect-button { }
  73. // Under v-overlay-container
  74. .v-filterselect-suggestpopup { }
  75. .popupContent { }
  76. .v-filterselect-prevpage,
  77. .v-filterselect-prevpage-off { }
  78. .v-filterselect-suggestmenu { }
  79. .gwt-MenuItem { }
  80. .v-filterselect-nextpage,
  81. .v-filterselect-nextpage-off { }
  82. .v-filterselect-status { }
  83. ----
  84. In its default state, only the input field of the [classname]#ComboBox#
  85. component is visible. The entire component is enclosed in
  86. [literal]#++v-filterselect++# style (a legacy remnant), the input field has
  87. [literal]#++v-filterselect-input++# style and the button in the right end that
  88. opens and closes the drop-down result list has
  89. [literal]#++v-filterselect-button++# style.
  90. The drop-down result list has an overall
  91. [literal]#++v-filterselect-suggestpopup++# style. It contains the list of
  92. suggestions with [literal]#++v-filterselect-suggestmenu++# style. When there are
  93. more items that fit in the menu, navigation buttons with
  94. [literal]#++v-filterselect-prevpage++# and
  95. [literal]#++v-filterselect-nextpage++# styles are shown. When they are not
  96. shown, the elements have [literal]#++-off++# suffix. The status bar in the
  97. bottom that shows the paging status has [literal]#++v-filterselect-status++#
  98. style.