Associate UI Components with a Collection"

From Documentation
m (Created page with '{{ZKDevelopersGuidePageHeader}} It can be very useful to associate a collection with a UI components, and Data Binding Manager will convert the collection into UI components acc…')
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
+
{{ZKDevelopersReferencePageHeader}}
  
It can be very useful to associate a collection with a UI components, and Data Binding Manager will convert the collection into UI components accordingly.  
+
It can be very useful to associate a collection with a UI component and the Data Binding Manager will convert the collection into UI components accordingly.  
  
# Prepare the data source of Collection
+
# Prepare the data source of collection
# Associate the collection with <tt>model</tt> attribute of those supported UI components, ex. <tt>Listbox</tt>, <tt>Grid</tt>, and <tt>Tree</tt>.
+
# Associate the collection with <tt>model</tt> attribute of those supported UI components, ex. <tt>Listbox</tt> and <tt>Grid</tt>.
# Define a template of UI component
+
# If the UI component is a selection type one such as <tt>Listbox</tt> or <tt>Combobox</tt>, you can also associate a variable with <tt>selectedItem</tt> attribute of the UI component. The Data Binding Manager will hold in the variable the currently selected collection item for you.
## '''Define a variable''', whatever you want, to represent each instance in the Collection with <tt>self</tt> attribute.<tt><component-name self="@{each='variable-name'}"/></tt> The '''variable-name''' could only be seen by component-name and its child components.
+
# Define a template of UI components
 +
## '''Define a variable''', whatever you want, to represent each instance in the Collection with <tt>self</tt> attribute.<tt><component-name self="@{each='variable-name'}"/></tt> The '''variable-name''' could only be seen by the component-name and its child components.
 
## Associate UI components with the variable<br/> <component-name attribute-name="@{variable-name.attribute-name}"/>
 
## Associate UI components with the variable<br/> <component-name attribute-name="@{variable-name.attribute-name}"/>
  
Line 19: Line 20:
 
int count = 30;
 
int count = 30;
 
List persons = new ArrayList();
 
List persons = new ArrayList();
for(int j= 0; j &lt; count; ++j) {
+
for(int j= 0; j < count; ++j) {
 
Person personx = new Person();
 
Person personx = new Person();
 
personx.setFirstName("Tom"+j);
 
personx.setFirstName("Tom"+j);
Line 25: Line 26:
 
persons.add(personx);
 
persons.add(personx);
 
}
 
}
 +
Person selected = persons.get(0);       
 
     ]]>
 
     ]]>
 
  </zscript>
 
  </zscript>
  
<listbox rows="4" model="@{persons}">
+
<listbox rows="4" model="@{persons}" selectedItem="@{selected}">
 
<listhead>
 
<listhead>
 
<listheader label="First Name" width="100px" />
 
<listheader label="First Name" width="100px" />
Line 48: Line 50:
 
</source>
 
</source>
  
{{ ZKDevelopersGuidePageFooter}}
+
 
 +
=Version History=
 +
{{LastUpdated}}
 +
{| border='1px' | width="100%"
 +
! Version !! Date !! Content
 +
|-
 +
| &nbsp;
 +
| &nbsp;
 +
| &nbsp;
 +
|}
 +
 
 +
{{ZKDevelopersReferencePageFooter}}

Revision as of 02:04, 25 July 2011


DocumentationZK Developer's ReferenceData BindingAssociate UI Components with a Collection
Associate UI Components with a Collection


It can be very useful to associate a collection with a UI component and the Data Binding Manager will convert the collection into UI components accordingly.

  1. Prepare the data source of collection
  2. Associate the collection with model attribute of those supported UI components, ex. Listbox and Grid.
  3. If the UI component is a selection type one such as Listbox or Combobox, you can also associate a variable with selectedItem attribute of the UI component. The Data Binding Manager will hold in the variable the currently selected collection item for you.
  4. Define a template of UI components
    1. Define a variable, whatever you want, to represent each instance in the Collection with self attribute.<component-name self="@{each='variable-name'}"/> The variable-name could only be seen by the component-name and its child components.
    2. Associate UI components with the variable
      <component-name attribute-name="@{variable-name.attribute-name}"/>

In the following example, we demonstrate how to associate a collection with Listbox to display a list of persons.

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

<window width="500px">
	<zscript><![CDATA[ 
     //prepare the example persons List
		int count = 30;
		List persons = new ArrayList();
		for(int j= 0; j < count; ++j) {
			Person personx = new Person();
			personx.setFirstName("Tom"+j);
			personx.setLastName("Hanks"+j);      
			persons.add(personx);
		}
		Person selected = persons.get(0);        
     ]]>
 	</zscript>

	<listbox rows="4" model="@{persons}" selectedItem="@{selected}">
		<listhead>
			<listheader label="First Name" width="100px" />
			<listheader label="Last Name" width="100px" />
			<listheader label="Full Name" width="100px" />
		</listhead>
		<!-- define variable person here-->
		<listitem self="@{each='person'}">
			<listcell>
				<textbox value="@{person.firstName}" />
			</listcell>
			<listcell>
				<textbox value="@{person.lastName}" />
			</listcell>
			<listcell label="@{person.fullName}" />
		</listitem>
	</listbox>
</window>


Version History

Last Update : 2011/07/25


Version Date Content
     



Last Update : 2011/07/25

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