summaryrefslogtreecommitdiffstats
path: root/documentation/components/components-combobox.asciidoc
blob: ccd75a3dc5d3e1559d6522a99578891657b9cfc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: ComboBox
order: 16
layout: page
---

[[components.combobox]]
= [classname]#ComboBox#

*_This section has not yet been updated for Vaadin Framework 8_*

ifdef::web[]
[.sampler]
image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/data-input/multiple-value/combo-box"]
endif::web[]

[classname]#ComboBox# is a selection component allows selecting an item from a
drop-down list. The component also has a text field area, which allows entering
search text by which the items shown in the drop-down list are filtered. Common
selection component features are described in
<<dummy/../../../framework/components/components-selection#components.selection,"Selection
Components">>.

.The [classname]#ComboBox# Component
image::img/combobox-basic.png[width=35%, scaledwidth=50%]

[[components.combobox.filtering]]
== Filtered Selection

[classname]#ComboBox# allows filtering the items available for selection in the
drop-down list by the text entered in the input box.

[[figure.components.combobox.filter]]
.Filtered Selection in [classname]#ComboBox#
image::img/combobox-filtering.png[width=35%, scaledwidth=50%]

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
drop-down list is paged and clicking on the scroll buttons will change to the
next or previous page. The list selection can also be done with the arrow keys
on the keyboard. The shown items are loaded from the server as needed, so the
number of items held in the component can be quite large. The number of matching
items is displayed by the drop-down list.

Filtering is enabled by setting a __filtering mode__ with
[methodname]#setFilteringMode()#.


[source, java]
----
cb.setFilteringMode(FilteringMode.CONTAINS);
----
See the http://demo.vaadin.com/book-examples-vaadin7/book#component.select.combobox.filtering[on-line example, window="_blank"].

The modes defined in the [classname]#FilteringMode# enum are as follows:

[parameter]#CONTAINS#:: Matches any item that contains the string given in the text field part of the
component.

[parameter]#STARTSWITH#:: Matches only items that begin with the given string.

[parameter]#OFF# (default):: Filtering is by default off and all items are shown all the time.



The above example uses the containment filter that matches to all items
containing the input string. As shown in <<figure.components.combobox.filter>>
below, when we type some text in the input area, the drop-down list will show
all the matching items.

[[components.combobox.newitems]]
== Allowing Adding New Items

[classname]#ComboBox# allows the user to add new items, when the user types
in a value and presses kbd:[Enter]. You need to enable this with
[methodname]#setNewItemHandler()#.

Adding new items is not possible if the selection component is read-only. An
attempt to do so may result in an exception.


=== Handling New Items

Adding new items is handled by a [interfacename]#NewItemHandler#, which gets the
item caption string as parameter for the [methodname]#accept(String)# method.


[source, java]
----
// List of planets
List<Planet> planets = new ArrayList<>();
planets.add(new Planet(1, "Mercury"));
planets.add(new Planet(2, "Venus"));
planets.add(new Planet(3, "Earth"));
planets.add(new Planet(4, "Mars"));

ComboBox<Planet> select =
    new ComboBox<>("Select or Add a Planet");
select.setItems(planets);

// Use the name property for item captions
select.setItemCaptionGenerator(Planet::getName);

// Allow adding new items and add
// handling for new items
select.setNewItemHandler(inputString -> {

    // Create a new bean - can't set all properties
    Planet newPlanet = new Planet(planets.size(), inputString);
    planets.add(newPlanet);

    // Update combobox content
    select.setItems(planets);

    // Remember to set the selection to the new item
    select.select(newPlanet);

    Notification.show("Added new planet called " +
                      inputString);
});
----

[[components.combobox.css]]
== CSS Style Rules


[source, css]
----
.v-filterselect { }
  .v-filterselect-input { }
  .v-filterselect-button { }

// Under v-overlay-container
.v-filterselect-suggestpopup { }
  .popupContent { }
    .v-filterselect-prevpage,
      .v-filterselect-prevpage-off { }
    .v-filterselect-suggestmenu { }
      .gwt-MenuItem { }
    .v-filterselect-nextpage,
      .v-filterselect-nextpage-off { }
    .v-filterselect-status { }
----

In its default state, only the input field of the [classname]#ComboBox#
component is visible. The entire component is enclosed in
[literal]#++v-filterselect++# style (a legacy remnant), the input field has
[literal]#++v-filterselect-input++# style and the button in the right end that
opens and closes the drop-down result list has
[literal]#++v-filterselect-button++# style.

The drop-down result list has an overall
[literal]#++v-filterselect-suggestpopup++# style. It contains the list of
suggestions with [literal]#++v-filterselect-suggestmenu++# style. When there are
more items that fit in the menu, navigation buttons with
[literal]#++v-filterselect-prevpage++# and
[literal]#++v-filterselect-nextpage++# styles are shown. When they are not
shown, the elements have [literal]#++-off++# suffix. The status bar in the
bottom that shows the paging status has [literal]#++v-filterselect-status++#
style.