Button"

From Documentation
Line 112: Line 112:
 
  [Since 5.0.0]
 
  [Since 5.0.0]
  
To prevent multiple clicks of a button upon clicking a button and a process executing the button is now disabled. The button is then re-enabled after the processing has finished, this is done using the autodisable feature.
+
<javadoc method="setAutodisable(java.lang.String)">org.zkoss.zul.Button</javadoc> is used to disable a button automatically, when it is clicked. It is useful to prevent the user from clicking it twice (and firing redundant requests), which is common if the request takes long to serve.
 +
 
 +
The simplest use is to specify it with <tt>self</tt> as follows. Then, the button is disabled when it is clicked.
  
 
<source lang="xml">
 
<source lang="xml">
Line 118: Line 120:
 
</source>
 
</source>
  
The above code demonstrates autodisable functionality, it is possible to re-enable buttons using the following method.
+
If you'd like to disable several buttons, you could specify all of them in this property by separating with a comma. For example, the following disables both buttons, when one of them is clicked.
 +
 
 +
<source lang="xml">
 +
<button id="ok" label="OK" autodisable="ok,cancel" />
 +
<button id="canel" label="Cancel" autodisable="ok,cancel" />
 +
</source>
  
 +
The button will be enabled automatically, after the request has been served (i.e., the response has been sent back to the client). If you prefer to enable them manually (i.e., by calling <javadoc method="setDisabled(boolean)">org.zkoss.zul.Button</javadoc> explicitly), you could prefix the ID with a plus (<tt>+</tt>). For example,
  
 
<source lang="xml">
 
<source lang="xml">
<button label="enable all" onClick="ok.disabled = cancel.disabled =
+
<button id="ok" label="OK" autodisable="+self, +cancel" />
false"/></source>
+
</source>
 +
 
 +
Then, you could enable them manually under the situation depending on your application's requirement, such as
 +
 
 +
<source lang="java">
 +
if (something_happens) {
 +
  ok.setDisabled(false);
 +
  cancel.setDisabled(false);
 +
}
 +
</source>
  
 
== Href ==
 
== Href ==

Revision as of 08:30, 29 April 2011

Button

Employment/Purpose

You could assign a label and an image to a button by the label and image properties. If both are specified, the dir property control which is displayed up front, and the orient property controls whether the layout is horizontal or vertical.

Within ZK 5, the file upload has been redesigned so it can be integrated with any widget. For example, The button can now be used to upload a file. In addition to this, the display of the upload status has been enhanced and can be customized easily.

Example

ZKComRef Button.jpg

	<button label="Left" image="/img/network.gif" width="125px"/>
	<button label="Right" image="/img/network.gif" dir="reverse" width="125px"/>
	<button label="Above" image="/img/network.gif" orient="vertical" width="125px"/>
	<button label="Below" image="/img/network.gif" orient="vertical" dir="reverse" width="125px"/>

In addition to employing URLs to specify images, you can dynamically assign a generated image to a button using the setImageContent method. Refer to the following section for details.

Tip: The setImageContent method is supplied by all components that have an image property. Simply put, setImageContent is used for dynamically generated images, while image is used for images identifiable by a URL.

File Upload

Any button[1] can be used to upload files. All you need to do is:

  1. Specify the upload attribute with true
  2. Handles the onUpload event.
<button upload="true" label="Fileupload" onUpload="myProcessUpload(event.getMedia())"/>

When the file is uploaded, an instance of UploadEvent is sent to the button. Then, the event listener can retrieve the uploaded content by examining the return value of UploadEvent.getMedia().


  1. Any Toolbarbutton can be used to upload files too.

Custom Error Message When Fileupload Over Maxsize

1. Write your own AuLoader

package test;  

import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException; 
import org.zkoss.zk.au.http.AuUploader;   

public class MyUploader extends AuUploader { 	

   protected String handleError(Throwable ex) { 		 		
      if(ex instanceof SizeLimitExceededException){ 			
         SizeLimitExceededException e = (SizeLimitExceededException) ex; 			
         return e.getActualSize() + " is over our limit"; 		
      } 		
      return super.handleError(ex); 	
   } 
}

2. Apply it in the web.xml

<servlet> 	
   <description>The asynchronous update engine for ZK</description> 	
   <servlet-name>auEngine</servlet-name> 	
   <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class> 	
   <init-param> 		
      <param-name>extension0</param-name> 		
      <param-value>/upload=test.MyUploader</param-value> 	
   </init-param> 
</servlet>

Limitation of the Default Mold

The default mold of a button uses HTML BUTTON tag to represent it visually. It is efficient, but it has some limitations:

  1. The look might be different from one browser to another.
  2. It doesn't support the file upload. In fact, it will become the trendy mold automatically if upload is specified.

If it is an issue, you could use the trendy mold instead.

<button label="OK" mold="trendy"/>

Configure to Use the Trendy Mold as Default

If you prefer to use the trendy mold as default, you could configure ZK by adding the following to /WEB-INF/zk.xml

<library-property>
    <name>org.zkoss.zul.Button.mold</name>
    <value>trendy</value>
</library-property>

Properties

Autodisable

[Since 5.0.0]

Button.setAutodisable(String) is used to disable a button automatically, when it is clicked. It is useful to prevent the user from clicking it twice (and firing redundant requests), which is common if the request takes long to serve.

The simplest use is to specify it with self as follows. Then, the button is disabled when it is clicked.

<button id="ok" label="OK" autodisable="self" />

If you'd like to disable several buttons, you could specify all of them in this property by separating with a comma. For example, the following disables both buttons, when one of them is clicked.

<button id="ok" label="OK" autodisable="ok,cancel" />
<button id="canel" label="Cancel" autodisable="ok,cancel" />

The button will be enabled automatically, after the request has been served (i.e., the response has been sent back to the client). If you prefer to enable them manually (i.e., by calling Button.setDisabled(boolean) explicitly), you could prefix the ID with a plus (+). For example,

<button id="ok" label="OK" autodisable="+self, +cancel" />

Then, you could enable them manually under the situation depending on your application's requirement, such as

if (something_happens) {
   ok.setDisabled(false);
   cancel.setDisabled(false);
}

Href

If a button shall jump to another URL when the user clicks, you could specify the URL in the href property (Button.setHref(String)).

Notice that the end user could hold the Contro key and click on the button to follow the link in a new browser window (like a HTML A tag does).

Href and the onClick Event

There are two ways to add behavior to a button and toolbarbutton. Firstly, you can specify a listener for the onClick event. Secondly, you could specify a URL for the href property (Button.setHref(String)). If both are specified, the href property has the higher priority, i.e., the onClick event won't be sent.

<zk>
	<window title="example">
		<button label="click me" onClick="do_something_in_Java()"/>
		<button label="don't click that one, click me" href="/another_page.zul"/>
	</window>
</zk>

Href and SendRedirect

The href property is processed at the client. In other words, the browser will jump to the URL specified in the href property, so your application running at the server has no chance to process it.

If you have to process it at the server or you have to decide whether to jump to another URL based on certain condition, you could listen to the onClick event, process it, and then invoke Executions.sendRedirect(String) if it shall jump to another URL.

For end users, there is no difference between the use of Button.setHref(String) and Executions.sendRedirect(String).

<zk>
	<window>		
		<button label="redirect" onClick="Executions.sendRedirect(&quot;another.zul&quot;)"/>
		<button label="href" href="another.zul"/>
	</window>
</zk>

Since the onClick event is sent to the server for processing, you are able to perform additional tasks before invoking Executions.sendRedirect(String), such as redirecting to another page only if certain conditions are satisfied.

On the other hand, the href property is processed at the client side. Your application won't be notified when users click the button.

Type

[Since 5.0.4]

Button.setType(String) sets the button's type. It is designed to work with the HTML FORM tag. You rarely need it unless you want to work with HTML FORM and Servlets. For example,

<n:form action="/foo/my_handler" xmlns:n="native">
  <textbox/>
  <button type="submit" label="Submit"/>
  <button type="reset" label="Reset"/>
</n:form>

Upload

By specifying the upload property (Button.setUpload(String)), you could make a button used for uploading files. For example,

<button label="Upload" upload="true" onUpload="handle(event.media)"/>

Once the file(s) are uploaded, the onUpload event will be sent with an instance of UploadEvent. And, you could retrieve the uploaded files from UploadEvent.getMedia() and UploadEvent.getMedias()

If you want to customize the handling of the file upload at the client, you can specify a JavaScript class when calling this method:

<button upload="foo.Upload"/> <!-- assume you implement a JavaScript class: foo.Upload -->

Another options for the upload can be specified as follows:

<button label="Upload" upload="true,maxsize=-1,native"/>

where

  • maxsize: the maximal allowed upload size of the component, in kilobytes, or a negative value if no limit.
  • native: treating the uploaded file(s) as binary, i.e., not to convert it to image, audio or text files.

Supported Events

Name
Event Type
onFocus
Event: Event

Denotes when a component gets the focus.

onBlur
Event: Event

Denotes when a component loses the focus.

onUpload
Event: UploadEvent

Denotes user has uploaded a file to the component.

Supported Molds

Available molds of a component are defined in lang.xml embedded in zul.jar.

Name
Snapshot
default
Button mold default.png
trendy
Button mold trendy.png
os
Button mold os.png

Supported Children

*NONE

Use Cases

Version Description Example Location
3.6 Get dynamically generated Button reference in onClick Event http://www.zkoss.org/forum/listComment/8780
3.6 How to fire onClick Event on a Button http://www.zkoss.org/forum/listComment/1716

Version History

Last Update : 2011/04/29


Version Date Content
5.0.4 August 2010 Button.setType(String) was introduced to allow a button able to submit or reset a form.
<n:form action="a_uri" xmlns:n="native">
  <button type="submit" label="Submit"/>
  <button type="reset" label="Reset"/>
</n:form>



Last Update : 2011/04/29

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