Selectbox"

From Documentation
Line 35: Line 35:
 
</source>
 
</source>
  
== Databinding (deprecated) ==
+
 
Here is the example for Databinding.
+
== Data binding ==
 +
 
 +
Here is the MVVM way:
 
<source lang="xml" >
 
<source lang="xml" >
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
+
    <zscript><![CDATA[
<zk>
+
    public class MyUserBean {
<zscript><![CDATA[
+
        private String[] userList = { "Tony", "Ryan", "Jumper", "Wing", "Sam" };
public class MyUserBean {
+
        private int index = 0;
private String[] userList = { "Tony", "Ryan", "Jumper", "Wing", "Sam" };
 
private int index = 0;
 
  
public ListModelList getUserList() {
+
        public ListModelList getUserList() {
return new ListModelList(Arrays.asList(userList));
+
            return new ListModelList(Arrays.asList(userList));
}
+
        }
  
public void setUserList() {
+
        public void setUserList() {
}
+
        }
  
public void setIndex(int ind) {
+
        public void setIndex(int ind) {
index = ind;
+
            index = ind;
}
+
        }
  
public int getIndex() {
+
        public int getIndex() {
return index;
+
            return index;
}
+
        }
  
public String getCurrentName() {
+
    }
return userList[index];
+
    MyUserBean mybean = new MyUserBean();
}
 
}
 
MyUserBean mybean = new MyUserBean();
 
 
/**  Implements ItemRenderer without using template
 
/**  Implements ItemRenderer without using template
 
       org.zkoss.zul.ItemRenderer render = new org.zkoss.zul.ItemRenderer() {
 
       org.zkoss.zul.ItemRenderer render = new org.zkoss.zul.ItemRenderer() {
public String render(Component owner, Object data, int index) throws Exception {
+
        public String render(Component owner, Object data, int index) throws Exception {
return data.toString();
+
            return data.toString();
}
+
        }
};
+
    };
 
*/
 
*/
]]></zscript>
+
    ]]></zscript>
<div>
+
    <div apply="org.zkoss.bind.BindComposer">
Select User:
+
        Select User:
<selectbox id="box" model="@{mybean.userList}"
+
        <selectbox id="box" model="@init(mybean.userList)"
selectedIndex="@{mybean.index}">
+
                  selectedIndex="@bind(mybean.index)">
<template name="model">${each}</template>
+
            <template name="model">${each}</template>
</selectbox>
+
        </selectbox>
 +
 
 +
    Selected:
 +
    <label id="val" value="@load(mybean.index)" />
  
</div>
 
Selected:
 
<label id="val" value="@{mybean.currentName}" />
 
</zk>
 
 
</source>
 
</source>
 +
  
 
=Supported Events=
 
=Supported Events=

Revision as of 03:15, 4 June 2020

Selectbox

Employment/Purpose

Selectbox is a lightweight dropdown list and it can support ListModel, Renderer, and Databinding as well. The benefit of it is not to create child widgets for each data, so the memory usage is much lower at the server.

Example

Selectbox-Example1.png

<zk>
	<zscript>
		<![CDATA[
		String[] userName = { "Tony", "Ryan", "Jumper", "Wing", "Sam" };
		ListModelList model = new ListModelList(userName);
	]]></zscript>
	<selectbox model="${model}" onSelect='alert(model.get(event.getData()));'>
		<template name="model">
			Name is ${each}
		</template>
	</selectbox>
</zk>

To give the selectbox an initial value, for example, Tony, add the following code after the model is created:

model.addToSelection ("Tony");


Data binding

Here is the MVVM way:

    <zscript><![CDATA[
    public class MyUserBean {
        private String[] userList = { "Tony", "Ryan", "Jumper", "Wing", "Sam" };
        private int index = 0;

        public ListModelList getUserList() {
            return new ListModelList(Arrays.asList(userList));
        }

        public void setUserList() {
        }

        public void setIndex(int ind) {
            index = ind;
        }

        public int getIndex() {
            return index;
        }

    }
    MyUserBean mybean = new MyUserBean();
/**   Implements ItemRenderer without using template
       org.zkoss.zul.ItemRenderer render = new org.zkoss.zul.ItemRenderer() {
        public String render(Component owner, Object data, int index) throws Exception {
            return data.toString();
        }
    };
*/
    ]]></zscript>
    <div apply="org.zkoss.bind.BindComposer">
        Select User:
        <selectbox id="box" model="@init(mybean.userList)"
                   selectedIndex="@bind(mybean.index)">
            <template name="model">${each}</template>
        </selectbox>

    Selected:
    <label id="val" value="@load(mybean.index)" />


Supported Events

Name
Event Type
onSelect
Event: SelectEvent

Notifies one that the user has selected a new item in the selectbox.

Supported Children

*NONE

Use Cases

Version Description Example Location
     

Version History

Last Update : 2020/06/04


Version Date Content
6.0.0 October 4, 2011 Add the new Selectbox component
6.0.0-RC2 December 6, 2011 Rename OptionRenderer to ItemRenderer



Last Update : 2020/06/04

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.