Chapter 4: Controlling Components"

From Documentation
Line 1: Line 1:
 
 
ZK's Components are not just only for constructing user interface, we even can control them. In this chapter, we continue to use the last chapter's example but we remove 3 item's hyper links in the sidebar and replace them with redirecting action. To achieve this, we should write codes in Java for each item to response a user's clicking and redirect the user to an external site.
 
ZK's Components are not just only for constructing user interface, we even can control them. In this chapter, we continue to use the last chapter's example but we remove 3 item's hyper links in the sidebar and replace them with redirecting action. To achieve this, we should write codes in Java for each item to response a user's clicking and redirect the user to an external site.
  
Line 11: Line 10:
  
 
'''Event listener redirect()'''
 
'''Event listener redirect()'''
<source lang='xml' high='11'>
+
<source lang='xml' high='11,14'>
 
<grid hflex="1" vflex="1" sclass="sidebar">
 
<grid hflex="1" vflex="1" sclass="sidebar">
 
<zscript><![CDATA[
 
<zscript><![CDATA[
Line 32: Line 31:
 
</source>
 
</source>
 
* Line 11: Define a event listener method like normal Java method and it redirects a browser according to passed key.
 
* Line 11: Define a event listener method like normal Java method and it redirects a browser according to passed key.
 +
* Line 14: [[ZUML_Reference/EL_Expressions/Implicit_Objects/execution execution]]
  
 
<source lang="xml" high='4,8,10'>
 
<source lang="xml" high='4,8,10'>
Line 48: Line 48:
 
</rows>
 
</rows>
 
</grid>
 
</grid>
 +
 +
</source>
  
 
= Control in Controller =
 
= Control in Controller =

Revision as of 09:44, 11 January 2013

ZK's Components are not just only for constructing user interface, we even can control them. In this chapter, we continue to use the last chapter's example but we remove 3 item's hyper links in the sidebar and replace them with redirecting action. To achieve this, we should write codes in Java for each item to response a user's clicking and redirect the user to an external site.

Control in Zscript

The simplest way to response a user's clicking is to write a event listener method and invoke it in onClick attribute. We could define a event listener in Java inside a <zscript> element and those codes will be interpreted when the ZUL is visited. This element also allows other script language like Javascript, Ruby, or Groovy.

Event listener redirect()

<grid hflex="1" vflex="1" sclass="sidebar">
	<zscript><![CDATA[
		//zscript code, it runs on server site, use it for fast prototyping
		java.util.Map sites = new java.util.HashMap();
		
		sites.put("zk","http://www.zkoss.org/");
		sites.put("demo","http://www.zkoss.org/zkdemo");
		sites.put("devref","http://books.zkoss.org/wiki/ZK_Developer's_Reference");
		
		
		void redirect(String name){
			String loc = sites.get(name);
			if(loc!=null){
				execution.sendRedirect(loc);
			}
		}
	]]></zscript>
...
<grid>
	...
	<rows>
		<row sclass="sidebar-fn" onClick='redirect("zk")'>
			<image src="/imgs/site.png"/> ZK
		</row>
		<row sclass="sidebar-fn" onClick='redirect("demo")'>
			<image src="/imgs/demo.png"/> ZK Demo
		</row>
		<row sclass="sidebar-fn" onClick='redirect("devref")'>
			<image src="/imgs/doc.png"/> ZK Developer Reference
		</row>
	</rows>
</grid>

Control in Controller

  • what is composer
  • how to apply it on a zul
  • initialize in doAftercomposer()
  • dynamic create components
  • event listener