Description
Implements a component that manages an HTML <select> form element. The
Select component can take the form of a drop down list (default) or a list
selection box if the "multiple" parameter is set to true. The Select component
must wrap around Option components within a
Form.
Unless multiple selections are required it is generally easier to use the
PropertySelection
component.
This examples shows how to setup a standard Select and Option component.
Note that Select and Option are typically only used like this when a
PropertySelection
component is unable to fulfil the required need. Normally, most users will want to use
the PropertySelection, since it requires less work, and can express similar semantics
with only a single component (instead of three, as will be shown below).
Select a color
<form jwcid="@Form" listener="ognl:listeners.formSubmit">
Select a color
<select jwcid="@Select" multiple="ognl:true">
<span jwcid="@Foreach" source="ognl:visit.colorList" value="ognl:visit.currentColor">
<option jwcid="@Option" selected="ognl:visit.currentColor.selected" label="ognl:visit.currentColor.label"/>
</span>
</select>
</form>
This example uses a pre-existing collection as the source of the Option list. The
Foreach is used to iterate through the list, rendering multiple HTML option
elements on the way.
When the form is submitted, Tapestry will take care of setting the
selected attribute on any item that has been selected by
the user. For this example, that means that a Color object will
have it's selected property set to true (see below)
In this example, the page object (the .java file) is not important, since it contains nothing.
The example has purposely tied both the option list, and the currently selected value (the
Foreach component's value field) to a property of
the Visit object.
>
The Visit object is defined as follows:
public class Visit implements Serializable {
private transient List colorList;
private Color currentColor;
public Collection getColorList() {
if (colorList == null) {
colorList = new ArrayList();
colorList.add(new Color("Green"));
colorList.add(new Color("Blue"));
colorList.add(new Color("Red"));
colorList.add(new Color("White"));
colorList.add(new Color("Purple"));
colorList.add(new Color("Dark Black"));
colorList.add(new Color("Fiji Blue"));
}
return colorList;
}
public Color getCurrentColor() { return currentColor; }
public void setCurrentColor(Color value) {
currentColor = value;
}
}
public class Color implements Serializable {
private String label;
private boolean selected;
public Color(String label) {
setLabel(label);
}
public String getLabel() { return label; }
public void setLabel(String value) {
label = value;
}
public boolean getSelected() { return selected; }
public void setSelected(boolean value) {
selected = value;
}
}