0

Using Javasript into zk

asked 2011-07-29 00:16:25 +0800

Sheik gravatar image Sheik
9

'How to pass javascript object value into zk' or 'how to access zk component into javascript'

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2011-07-31 20:04:11 +0800

samchuang gravatar image samchuang
4084 4

Hi

for client side, you can refer to ZK Client-side Reference

link publish delete flag offensive edit

answered 2011-08-03 08:28:07 +0800

creata87 gravatar image creata87
197 2

..hey, shelk, did you find your answer? how do you call javascript methods from java code?

link publish delete flag offensive edit

answered 2011-08-03 10:08:16 +0800

twiegand gravatar image twiegand
1807 3

updated 2011-08-03 10:10:44 +0800

@creata87,

Calling JavaScript methods from Java is quite simple - you just use Clients.evalJavaScript().  Here is an example:

<zk>
	<script>
		function sayHello(){
			alert("Hi there");
		}
	</script>
	<zscript>
		import org.zkoss.zk.ui.util.GenericForwardComposer;

		public class MyComposer extends GenericForwardComposer {
						
			public void doAfterCompose(Component comp) throws Exception {
				super.doAfterCompose(comp);
			}
			
			public void onClickButton(Event event) {
				String jsCommand = "sayHello()"; 
				Clients.evalJavaScript(jsCommand);
			}			
		}
	</zscript>
	
	<window apply="MyComposer">
		<button label="Say Hello in JavaScript" forward="onClick=onClickButton()"/>
	</window>
</zk>

In the future though, you might consider making a new thread to avoid confusion.

Regards,

Todd

link publish delete flag offensive edit

answered 2011-08-03 10:34:20 +0800

creata87 gravatar image creata87
197 2

..thank you for the quick reply :) yes, this is the easy way. unfortunately, it does not work for me as i have 3 files: .zul, .js and the .java controller. my guess is somehow the .js file has to be loaded in the controller..?

hello.zul

<zk>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>	

<script src="/scripts/hello.js" />

<div width="100%" height="100%" apply="${helloController}">	
	<button id="helloButton" label="Hello"/>
</div>	

</zk>	

hello.js

	function sayHello(){
		alert("Hello world");
	}

hello.java

import org.zkoss.zk.ui.util.GenericForwardComposer;

public class HelloController extends GenericForwardController {
			
	public void onClick$helloButton(Event event) {
		String jsCommand = "sayHello()"; 
	        Clients.evalJavaScript(jsCommand);
	}			

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}        
        
}

link publish delete flag offensive edit

answered 2011-08-03 11:22:17 +0800

twiegand gravatar image twiegand
1807 3

You can certainly have external JavaScript files and I do this all the time with various jQuery libraries.  I have never had a need to have the JavaScript loaded in a controller.  The way you have code set up above should work just fine.

If you want to see another example, click here.

Regards,

Todd

link publish delete flag offensive edit

answered 2011-08-03 17:25:08 +0800

TonyQ gravatar image TonyQ
642
https://www.masterbranch....

updated 2011-08-03 17:59:45 +0800

Hi creata87 ,

it works for me.

Please reference to this sample and check all the path is correct.


Hello.js
function sayHello(){
alert("Hello world");
}

HelloController.java
package j15d409$v1;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zul.*;

public class HelloController extends GenericForwardComposer{

public void onClick$helloButton(Event event) {
String jsCommand = "sayHello()";
Clients.evalJavaScript(jsCommand);
}

}


index.zul
<zk>
<script src="Hello.js" />

<div width="100%" height="100%" apply="j15d409$v1.HelloController">
<button id="helloButton" label="Hello"/>
</div>

</zk>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-07-29 00:16:25 +0800

Seen: 434 times

Last updated: Aug 03 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More