|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.control.AbstractControl org.apache.click.control.Field org.apache.click.control.Select
public class Select
Provides a Select control: <select></select>.
Select |
multiple
item property is false.
If a Select is required, an item after the first in the list must be selected
for the Field to be valid. This forces the user to make an active selection.
An example of a single item Select is provided below along with the
rendered HTML.
public class GenderPage extends Page { public Form form = new Form(); private Select genderSelect = new Select("Gender"); public GenderPage() { genderSelect.setRequired(true); genderSelect.add(new Option("U", ""); genderSelect.add(new Option("M", "Male")); genderSelect.add(new Option("F", "Female")); form.add(genderSelect); form.add(new Submit("ok", " OK ")); } public void onPost() { if (form.isValid()) { String gender = genderSelect.getValue(); .. } } }Rendered HTML:
|
Option
items are added to the Select. In this
example the "U" option will not be a valid selection, as it is the first
item in the option list.
multiple
item property is false, and must be
enabled for multiple item selects.
If multiple item Select is required, the user must select an item(s) in
the list for the Field to be valid. A valid selection can include any item
including the first item.
An example of a single item Select is provided below along with the
rendered HTML.
public class LocationPage extends Page { public Form form = new Form(); private Select locationSelect = new Select("location"); public LocationPage() { locationSelect.setMutliple(true); locationSelect.setRequired(true); locationSelect.setSize(7); locationSelect.add("QLD"); locationSelect.add("NSW"); locationSelect.add("NT"); locationSelect.add("SA"); locationSelect.add("TAS"); locationSelect.add("VIC"); locationSelect.add("WA"); form.add(locationSelect); form.add(new Submit("ok", " OK ")); } public void onPost() { if (form.isValid()) { String location = locationSelect.getValue(); .. } } }Rendered HTML:
|
add(String)
method is used to add an Option
item to the Select.
public MyPage extends Page { .. private Select mySelect; public MyPage() { mySelect = new Select("mySelect"); mySelect.setRequired(true); .. } public void onInit() { mySelect.add(Option.EMPTY_OPTION); List<Customer> customerList = customerDao.getCustomerList(); mySelect.addAll(customerList, "id", "name"); } .. }Unless you use a DataProvider, remember to always populate the Select option list before it is processed. Do not populate the option list in a Page's onRender() method.
optionList
in. To alleviate
this problem you can set a
dataProvider
which allows the Select to fetch data when needed. This is
particularly useful if retrieveing Select data is expensive e.g. loading
from a database.
Below is a simple example:
public class GenderPage extends Page { public Form form = new Form(); private Select genderSelect = new Select("Gender"); public GenderPage() { // Set the Select default "non-selection" option genderSelect.setDefaultOption(new Option("U", ""); // Set a DataProvider which "getData" method will be called to populate the // optionList. The "getData" method is only called when the optionList // data is needed genderSelect.setDataProvider(new DataProvider() { public List getData() { List options = new ArrayList(); options.add(new Option("M", "Male")); options.add(new Option("F", "Female")); return options; } }); form.add(genderSelect); form.add(new Submit("ok", " OK ")); } public void onPost() { if (form.isValid()) { String gender = genderSelect.getValue(); .. } } }
value
to the
option value you want to select:
public MyPage extends Page { private Select mySelect; public MyPage() { mySelect = new Select("mySelect"); mySelect.add("YES"); mySelect.add("NO"); // If you want NO to be selected by default, set the value to "NO" mySelect.setValue("NO"); } }See also the W3C HTML reference: SELECT
Option
,
OptionGroup
,
Serialized FormField Summary | |
---|---|
protected DataProvider |
dataProvider
The select data provider. |
protected Option |
defaultOption
The default option will be the first option added to the Select. |
protected boolean |
multiple
The multiple options selectable flag. |
protected List |
optionList
The Select Option/OptionGroup list. |
protected List<String> |
selectedValues
The multiple selected values. |
protected int |
size
The Select display size in rows. |
protected static String |
VALIDATE_SELECT_FUNCTION
The field validation JavaScript function template. |
Fields inherited from class org.apache.click.control.Field |
---|
disabled, error, focus, form, help, label, labelStyle, labelStyleClass, parentStyleClassHint, parentStyleHint, readonly, required, tabindex, title, trim, validate, value |
Fields inherited from class org.apache.click.control.AbstractControl |
---|
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles |
Fields inherited from interface org.apache.click.Control |
---|
CONTROL_MESSAGES |
Constructor Summary | |
---|---|
Select()
Create a Select field with no name defined. |
|
Select(String name)
Create a Select field with the given name. |
|
Select(String name,
boolean required)
Create a Select field with the given name and required status. |
|
Select(String name,
String label)
Create a Select field with the given name and label. |
|
Select(String name,
String label,
boolean required)
Create a Select field with the given name, label and required status. |
Method Summary | |
---|---|
void |
add(Object option)
Add the given Option/OptionGroup/String/Number/Boolean to the Select. |
void |
add(Option option)
Add the given Option to the Select. |
void |
add(OptionGroup optionGroup)
Add the given OptionGroup to the Select. |
void |
add(String value)
Add the given option value to the Select. |
void |
addAll(Collection<?> options)
Add the given Option/OptionGroup/String/Number/Boolean collection to the Select. |
void |
addAll(Collection objects,
String optionValueProperty,
String optionLabelProperty)
Add the given collection of objects to the Select, creating new Option instances based on the object properties specified by optionValueProperty and optionLabelProperty. |
void |
addAll(Map options)
Add the given Map of option values and labels to the Select. |
void |
addAll(String[] options)
Add the given array of string options to the Select option list. |
void |
bindRequestValue()
Bind the request submission, setting the Field.value or
selectedValues property if defined in the request. |
int |
getControlSizeEst()
Return the estimated rendered control size in characters. |
DataProvider |
getDataProvider()
Return the select option list DataProvider. |
Option |
getDefaultOption()
Return the Select default option or null if no default option is set. |
protected String |
getDefaultOptionValue()
Return the Select defaultOption value, or
null if no defaultOption is set. |
List |
getMultipleValues()
Deprecated. use getSelectedValues() instead, this method will
be removed in subsequent releases |
List |
getOptionList()
Return the Option list. |
List |
getSelectedValues()
Return the list of selected values. |
int |
getSize()
Return the number of Select display rows. |
Object |
getState()
Return the Select state. |
String |
getTag()
Return the select's html tag: select. |
String |
getValidationJavaScript()
Return the Select JavaScript client side validation function. |
boolean |
isMultiple()
Return true if multiple options can be selected. |
void |
onRender()
This method invokes getOptionList() to ensure exceptions thrown
while retrieving options will be handled by the error page. |
void |
render(HtmlStringBuffer buffer)
Render the HTML representation of the Select. |
void |
setDataProvider(DataProvider dataProvider)
Set the select option list DataProvider. |
void |
setDefaultOption(Option option)
Set the Select default option. |
protected void |
setInitialValue()
Set the initial select option value. |
void |
setMultiple(boolean value)
Set the multiple options can be selected flag. |
void |
setMultipleValues(List<String> multipleValues)
Deprecated. use getSelectedValues() instead, this method will
be removed in subsequent releases |
void |
setOptionList(List options)
Set the Option list. |
void |
setSelectedValues(List<String> multipleValues)
Set the list of selected values. |
void |
setSize(int rows)
Set the number of the Select display rows. |
void |
setState(Object state)
Set the Select state. |
void |
validate()
Validate the Select request submission. |
Methods inherited from class org.apache.click.control.Field |
---|
getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getLabelStyle, getLabelStyleClass, getParentStyleClassHint, getParentStyleHint, getRequestValue, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isTrim, isValid, onProcess, removeState, renderTagBegin, restoreState, saveState, setDisabled, setError, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setLabelStyle, setLabelStyleClass, setListener, setParent, setParentStyleClassHint, setParentStyleHint, setReadonly, setRequired, setTabIndex, setTextAlign, setTitle, setTrim, setValidate, setValue, setValueObject, setWidth |
Methods inherited from class org.apache.click.control.AbstractControl |
---|
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHeadElements, getHtmlImports, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, onDestroy, onInit, removeBehavior, removeStyleClass, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final String VALIDATE_SELECT_FUNCTION
protected boolean multiple
protected List optionList
protected int size
protected List<String> selectedValues
multiple
is true.
protected DataProvider dataProvider
protected Option defaultOption
setDataProvider(org.apache.click.dataprovider.DataProvider)
, where
the DataProvider does not return a sensible default option e.g. an
empty ("") option.
Constructor Detail |
---|
public Select(String name)
name
- the name of the fieldpublic Select(String name, String label)
name
- the name of the fieldlabel
- the label of the fieldpublic Select(String name, boolean required)
name
- the name of the fieldrequired
- the field required statuspublic Select(String name, String label, boolean required)
name
- the name of the fieldlabel
- the label of the fieldrequired
- the field required statuspublic Select()
Method Detail |
---|
public String getTag()
getTag
in class AbstractControl
AbstractControl.getTag()
public void add(Option option)
option
- the Option value to add
IllegalArgumentException
- if option is nullpublic void add(OptionGroup optionGroup)
optionGroup
- the OptionGroup value to add
IllegalArgumentException
- if optionGroup is nullpublic void add(String value)
Option
with the given value and add it to the
Select. The new Option display label will be the same as its value.
value
- the option value to add
IllegalArgumentException
- if the value is nullpublic void add(Object option)
option
- one of either Option/OptionGroup/String/Number/Boolean
to add
IllegalArgumentException
- if option is null, or the option
is an unsupported classpublic void addAll(Collection<?> options)
options
- the collection of Option/OptionGroup/String/Number/Boolean
objects to add
IllegalArgumentException
- if options is null, or the collection
contains an unsupported classpublic void addAll(Map options)
options
- the Map of option values and labels to add
IllegalArgumentException
- if options is nullpublic void addAll(String[] options)
Option.value
and Option.label
.
options
- the array of option values to add
IllegalArgumentException
- if options is nullpublic void addAll(Collection objects, String optionValueProperty, String optionLabelProperty)
Map
instances.
Example usage:
Select select = new Select("type", "Type:"); select.addAll(getCustomerService().getCustomerTypes(), "id", "name"); form.add(select);This method will iterate over all customerTypes and for each customerType create a new Option, setting the option value to the customerType "id", and the option label to the customerType "name".
objects
- the collection of objects to render as optionsoptionValueProperty
- the name of the object property to render as
the Option valueoptionLabelProperty
- the name of the object property to render as
the Option label
IllegalArgumentException
- if objects or optionValueProperty
parameter is nullpublic DataProvider getDataProvider()
public void setDataProvider(DataProvider dataProvider)
Select select = new Select("name", "Name"); // Set the Select default "non-selection" option select.setDefaultOption(new Option("U", "")); select.setDataProvider(new DataProvider() { public List getData() { List options = new ArrayList(); options.add(new Option("M", "Male")); options.add(new Option("F", "Female")); return options; } });
dataProvider
- the select option list DataProviderpublic int getSize()
public void setSize(int rows)
rows
- the Select display size in rows.public boolean isMultiple()
public void setMultiple(boolean value)
value
- the multiple options can be selected flagpublic List getMultipleValues()
getSelectedValues()
instead, this method will
be removed in subsequent releases
public List getSelectedValues()
public void setMultipleValues(List<String> multipleValues)
getSelectedValues()
instead, this method will
be removed in subsequent releases
multipleValues
- the list of selected valuespublic void setSelectedValues(List<String> multipleValues)
multipleValues
- the list of selected valuespublic void setDefaultOption(Option option)
optionList
.
Please note: this property is used in conjunction with the Select
dataProvider
,
where the DataProvider does not return a sensible default, non-selecting
option. For example if the DataProvider returns a list of Authors from the
database, the list won't include a default empty ("") option to choose
from. By setting the defaultOption property, the Select will add this
Option as the first option of the Select optionList
.
In addition, if the Select is required
,
the defaultOption is used to check whether the Select is valid or not.
In other words, if the user's selected value equals the defaultOption value,
the Select won't be valid since no selection was made by the user.
Example usage:
public void onInit() { authorSelect.setDefaultOption(Option.EMPTY_OPTION); authorSelect.setDataProvider(new DataProvider() { public List getData() { List options = new ArrayList(); Listauthors = getAuthorDao().getAuthors(); for (Author author : authors) { options.add(new Option(author.getId(), author.getName())); } return options; } }); form.add(authorSelect); }
option
- the Select default optionpublic Option getDefaultOption()
setDefaultOption(org.apache.click.control.Option)
public List getOptionList()
public void setOptionList(List options)
options
- the Option listpublic String getValidationJavaScript()
getValidationJavaScript
in class Field
public void bindRequestValue()
Field.value
or
selectedValues
property if defined in the request.
bindRequestValue
in class Field
public Object getState()
isMultiple()
is true or false:
Field.getValue()
if isMultiple()
is falsegetSelectedValues()
if isMultiple()
is true
getState
in interface Stateful
getState
in class Field
public void setState(Object state)
setState
in interface Stateful
setState
in class Field
state
- the Select state to setpublic void onRender()
getOptionList()
to ensure exceptions thrown
while retrieving options will be handled by the error page.
onRender
in interface Control
onRender
in class AbstractControl
Control.onRender()
public int getControlSizeEst()
AbstractControl
getControlSizeEst
in class AbstractControl
AbstractControl.getControlSizeEst()
public void render(HtmlStringBuffer buffer)
render
in interface Control
render
in class AbstractControl
buffer
- the specified buffer to render the control's output toAbstractControl.toString()
public void validate()
Field.required
then the user must select a value
other than the first value is the list, otherwise the Select will
have a validation error. If the Select is not required then no
validation errors will occur.
A field error message is displayed if a validation error occurs.
These messages are defined in the resource bundle: Error message bundle key names include:/click-control.properties
- select-error
validate
in class Field
protected String getDefaultOptionValue()
defaultOption
value, or
null if no defaultOption is set.
getDefaultOption()
,
setDefaultOption(org.apache.click.control.Option)
protected void setInitialValue()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |