Combobox"
Jimmyshiau (talk | contribs) |
|||
Line 35: | Line 35: | ||
</zk> | </zk> | ||
</source> | </source> | ||
+ | |||
+ | = Autocomplete = | ||
+ | |||
+ | == Autocomplete in a Brute-force Way == | ||
+ | |||
+ | The straightforward way to implement the autocomplete feature is to listen the onChanging event. For example, | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <combobox> | ||
+ | <attribute name="onChaging"><![CDATA[ | ||
+ | self.getChildren().clear(); //remove all children | ||
+ | for (String value: getMatched(event.getValue()) | ||
+ | self.appendChild(new Comboitem(value)); | ||
+ | ]]></attribute> | ||
+ | </combobox> | ||
+ | </source> | ||
+ | |||
+ | where we assume getMatched() is an application-specific method that returns a collection of matched values. | ||
+ | |||
+ | == Autocomplete by ListSubModel == | ||
+ | |||
+ | To separate the data from the view (combobox) better, we could implement a list model with <javadoc type="interface">org.zkoss.zul.ListSubModel</javadoc>. ZK provides several implementations that we could use directly, such as <javadoc>org.zkoss.zul.SimpleListModel</javadoc> (3.0.2 or later) and <javadoc>org.zkoss.zul.ListModelList</javadoc> (5.0.4 or later). For example, | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <combobox id="combo" apply="MyAutoComplete"> | ||
+ | </source> | ||
+ | |||
+ | then in <tt>MyAutoComplete.java</tt>, you could have | ||
+ | |||
+ | <source lang="java"> | ||
+ | public class MyAutoComplete extends GenericForwardComposer { | ||
+ | Combobox combo; | ||
+ | public void afterCompose() { | ||
+ | super.afterCompose(); | ||
+ | combo.setModel(new ListModelList(getAllItems()); | ||
+ | } | ||
+ | List getAllItems() { | ||
+ | //return all items | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | By default, it shows the first 15 items that matches the value entered by the user. If you want to have a different value, you can override <javadoc method="getMaxNumberInSubModel(int)">org.zkoss.zul.SimpleListModel</javadoc> as follows. | ||
+ | |||
+ | <source lang="java"> | ||
+ | combo.setModel(new ListModelList(getAllItems()) { | ||
+ | public int getMaxNumberInSubModel(int nItems) { | ||
+ | return nRows < 0 ? 20: nRows; | ||
+ | } | ||
+ | }); | ||
+ | </srouce> | ||
=Supported events= | =Supported events= |
Revision as of 05:04, 24 August 2010
Combobox
- Demonstration: Comboboxes
- Java API: Combobox
- JavaScript API: Combobox
Employment/Purpose
Components: combobox and comboitem.
A combobox is a special text box that embeds a drop-down list. With comboboxes, users are allowed to select from a drop-down list, in addition to entering the text manually.
Example
<combobox>
<comboitem label="Simple and Rich"/>
<comboitem label="Cool!"/>
<comboitem label="Ajax and RIA"/>
</combobox>
<zk>
<zscript><![CDATA[
ListModelList lm = new ListModelList(Arrays.asList(new String[] { "David",
"Thomas", "Steven" }));
]]></zscript>
<combobox model="${lm}" onAfterRender="self.setSelectedIndex(2)"/>
</zk>
Autocomplete
Autocomplete in a Brute-force Way
The straightforward way to implement the autocomplete feature is to listen the onChanging event. For example,
<combobox>
<attribute name="onChaging"><![CDATA[
self.getChildren().clear(); //remove all children
for (String value: getMatched(event.getValue())
self.appendChild(new Comboitem(value));
]]></attribute>
</combobox>
where we assume getMatched() is an application-specific method that returns a collection of matched values.
Autocomplete by ListSubModel
To separate the data from the view (combobox) better, we could implement a list model with ListSubModel. ZK provides several implementations that we could use directly, such as SimpleListModel (3.0.2 or later) and ListModelList (5.0.4 or later). For example,
<combobox id="combo" apply="MyAutoComplete">
then in MyAutoComplete.java, you could have
public class MyAutoComplete extends GenericForwardComposer {
Combobox combo;
public void afterCompose() {
super.afterCompose();
combo.setModel(new ListModelList(getAllItems());
}
List getAllItems() {
//return all items
}
}
By default, it shows the first 15 items that matches the value entered by the user. If you want to have a different value, you can override SimpleListModel.getMaxNumberInSubModel(int) as follows.
<source lang="java"> combo.setModel(new ListModelList(getAllItems()) {
public int getMaxNumberInSubModel(int nItems) { return nRows < 0 ? 20: nRows; }
}); </srouce>
Supported events
Event: InputEvent
Denotes the content of an input component has been modified by the user.
| |
Event: InputEvent
Denotes that user is changing the content of an input component. Notice that the component's content (at the server) won't be changed until onChange is received. Thus, you have to invoke the getValue method in the InputEvent class to retrieve the temporary value. | |
Event: SelectEvent
Represents an event cause by user's the list selection is changed at the client. | |
Event: SelectionEvent
Denotes that user is selecting a portion of the text of an input component. You can retrieve the start and end position of the selected text by use of the getStart and getEnd methods. | |
Event: OpenEvent
Denotes user has opened or closed a component. Note: unlike onClose, this event is only a notification. The client sends this event after opening or closing the component. It is useful to implement load-on-demand by listening to the onOpen event, and creating components when the first time the component is opened.
| |
Event: Event
Denotes when a component gets the focus. | |
Event: Event
Denotes when a component loses the focus. | |
Event: CreateEvent
It's used to make the combobox do some action after it has been created. For example you can make it 'ReadOnly' ( onCreate="self.setReadonly(true);" ), 'Disabled' ( onCreate="self.setDisabled(true);" ) or even 'Select' a value from the combobox ( onCreate="self.setSelectedIndex(0);" ) . | |
onAfterRender
Description: Notifies one that the model's data has been rendered. |
Supported Children
Use cases
Version | Description | Example Location |
---|---|---|
Version History
Version | Date | Content |
---|---|---|
5.0.4 | July, 2010 | Combobox supports Selectable if it is implemented with the specified ListModel. |
5.0.4 | July 2010 | Support onAfterRender event |