0

dynamically create components/event handler

asked 2008-06-05 03:48:25 +0800

admin gravatar image admin
18691 1 10 129
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=5008914

By: ramisyuanbo

What is the elegant way to dynamically create components and setting up their event handler?

My situation is like this:
I need to show a table on my page, with data retrieved from database. I use "<grid>" tag for this table.
For each row, one of the columns is a Toolbarbutton, by clicking which some details will be shown about that row.

I do it in this way.
1. there's a <grid> in the page
2. setup the grid's Model and RowRenderer in script 3. in the RowRenderer's render method, make up a button and add to the row
Toolbarbutton button = new Toolbarbutton((String)rowData[0]);
org.zkoss.zk.ui.metainfo.ZScript zscript = new org.zkoss.zk.ui.metainfo.ZScript(null, "java",
"showLoginInfo(\""+((String)rowData[0])+"\");", null);
org.zkoss.zk.ui.metainfo.EventHandler eventHandler = new org.zkoss.zk.ui.metainfo.EventHandler( zscript, null);
button.addEventHandler("onClick", eventHandler);
row.appendChild( button );
where showLoginInfo is a script function written in the page.


This kind of cross-over reference looks very ugly.

What's the best way to do this kinda stuff?

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2008-06-05 06:49:47 +0800

admin gravatar image admin
18691 1 10 129
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=5009138

By: oberinspector

i often use the addForward function for this... i have a class which creates my gui:

...
public class MyGUI extends Window{
...
public MyGui{
...
Textbox tb = new Textbox();
tb.addForward("onChange", this, "onTbChanged");
this.appendChild(tb);
...
}

public void onTbChanged(){
//handle the input
}
}

when you do things in java directly there is normaly no need to use zscript, since it is also executed at server side...

/thomas

link publish delete flag offensive edit

answered 2008-12-16 00:16:25 +0800

judacoor gravatar image judacoor
280 1 3 8

updated 2008-12-16 00:16:52 +0800

oberinspector, I've tryed what you said and it works perfectly fine!

I'm using toolbarbuttons inside a tree, trying to give the user the capability to delete records from that tree. So when the user clicks the toolbarbutton the record is erased.

The problem I have is that I don't understand how to read a value from the onClick event in the toolbarbutton, for I would need to know which record to erase...

I've got this so far:

public class PacSearch extends Window implements AfterCompose {

public void afterCompose() {
Components.addForwards(this, this);
Components.wireVariables(this, this);
renderTree();
}


//And inside renderTree:

Toolbarbutton b = new Toolbarbutton("Hi");
b.addForward("onClick", this, "onTBClicked");
Treecell cellasd = new Treecell();
cellasd.appendChild(b);
row5.appendChild(cellasd);

it does it for a bunch of times so I can get about 100 records in the tree (I'm just testing)


public void onTBClicked(){
String x = pacTree.getSelectedItem().getLabel();
System.out.println("Button was pressed!!! "+x);
}


As you can see I tryed getting the selectedItem from the tree (pacTree) but it's no use, because when you click the tbButton you're not selecting an item from the tree, so you get a nullpointer exception.

Could you please help me?
All I need is some way to obtain an attribute like "recordId" from the tbButton when it's pressed, but I don't know if there's a way to get the Event or something..

Please help me, I would appreciate your help very much, since I'm new to java and ZK

Thanks,
judacoor

link publish delete flag offensive edit

answered 2008-12-18 09:54:33 +0800

PeterKuo gravatar image PeterKuo
481 2

You can search these two api in http://www.zkoss.org/doc/devguide-single/index.html
for hints:
setAttribute
event.getTarget()

You can set recordId in each toolbarbutton by setAttribute.
When it clicked, event.getTarget() will point to the clicking target.
Therefore, you can retrieve back recordId.

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: 2008-06-05 03:48:25 +0800

Seen: 1,326 times

Last updated: Dec 18 '08

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