Listbox
Listbox
- Demonstration: Listbox
- Java API: org.zkoss.zul.Listbox
- JavaScript API:
zul.sel.Listbox
Employment/Purpose
Components: listbox, listitem, listcell, listhead
and listheader
.
A list box is used to display a number of items in a list. The user may
select an item from the list. Although listhead
is optional, if it
exists, notice that the number of listheader
should equal the number
of listcell
, so that listbox
can display its content correctly. If
listhead
contains no listheader
, the listbox
will display nothing
in its content.
Example
<window title="listbox demo" border="normal" width="250px">
<listbox id="box">
<listhead sizable="true">
<listheader label="name" sort="auto" />
<listheader label="gender" sort="auto" />
</listhead>
<listitem>
<listcell label="Mary" />
<listcell label="FEMALE" />
</listitem>
<listitem>
<listcell label="John" />
<listcell label="MALE" />
</listitem>
<listitem>
<listcell label="Jane" />
<listcell label="FEMALE" />
</listitem>
<listitem>
<listcell label="Henry" />
<listcell label="MALE" />
</listitem>
<listfoot>
<listfooter>
<label value="This is footer1" />
</listfooter>
<listfooter>
<label value="This is footer2" />
</listfooter>
</listfoot>
</listbox>
</window>
Listboxes Contain Buttons
In theory, a list cell can contain any other components, as demonstrated below.
<zk>
<listbox width="250px">
<listhead>
<listheader label="Population"/>
<listheader label="Percentage"/>
</listhead>
<listitem value="A">
<listcell><textbox width="90%" value="A. Graduate"/></listcell>
<listcell label="20%"/>
</listitem>
<listitem value="B">
<listcell><checkbox label="B. College"/></listcell>
<listcell><button label="23%"/></listcell>
</listitem>
<listitem value="C">
<listcell label="C. High School"/>
<listcell><textbox cols="8" value="40%"/></listcell>
</listitem>
</listbox>
</zk>
Notes:
- Don’t use a list box, when a grid is a better choice. The appearances of list boxes and grids are similar, but the listbox should only be used to represent a list of selectable items.
- Users are usually confused if a listbox contains editable components
such as a
textbox
or acheckbox
. - Due to the limitation of the browsers, users cannot select a range of characters from text boxes.
Mold
The Listbox has two molds: default
and select
.
Select Mold
Suggest using Selectbox.
If the select
mold is used, Listbox renders an HTML <select>
tag
instead.
<listbox mold="select">
<listitem label="Matthew"/>
<listitem label="Macus"/>
<listitem label="Lucas"/>
<listitem label="John"/>
</listbox>
Note: if the mold
is “select
”, rows
is “1”, and none of the items
are marked as selected, the browser will display the listbox
as if the
first item is selected. Worst of all, if the user selects the first item
in this case, no onSelect
event is sent. To avoid this confusion,
developers should select at least one item when using mold="select"
and rows="1"
.
In addition to each item’s label, you can assign an application-specific
value to each item using the setValue
method.