0

Databinding with grid and checkboxes

asked 2009-02-18 10:15:23 +0800

airstrike gravatar image airstrike
42

Hi,
I'm trying to use the ZK binding manager to associate a grid with checkboxes columns to a Java data source, but I encountered some problems. The Java data source CommonData contains a list (procList) of elements of the Java bean class Procedure (which contains some data fields with setter and getter methods). The method loadData() populates the list.
The binding manager correctly populates the grid, but the problems is with the checkboxes: if I set the association between the checkboxes labels and the data source, the method checkUpdate() in the zscript doesn't work. But if I remove that association (that is removing label="@{proc.sendTime,save-when etc..), then the method works. It seems like I cannot manually update the checkboxes labels if they are associated to the binding manager. Do you have any suggestions?
Moreover, is there a way to access the list element currently elaborated while the binding manager builds the grid?
And finally, is there a way to access and manipulate an element of the row (for example a Label component) from another element of the row (for example a checkbox with the onChecked attribute). I mean, how can I use the id attribute when I use the binding manager to build the grid?
Thanks in advance for your help!

Here is my grid code:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?page title="Monitor" contentType="text/html;charset=UTF-8"?>
<zk>

<window border="normal" title="Title">
<zscript>
CommonData c = new CommonData();
c.loadData();
</zscript>

<grid fixedLayout="true" model="@{c.procList}">
<auxhead>
<auxheader label="Aux1" style="padding-left:25%;font-weight:900" colspan="3"/>
<auxheader label="Aux2" style="padding-left:8%" colspan="1"/>
</auxhead>
<columns sizable="true">
<column label="C1"></column>
<column label="C2"></column>
<column label="C3"></column>
<column label="C4"></column>
</columns>

<rows>
<zscript><![CDATA[
void checkUpdate(Checkbox chbx)
{
java.util.Date now = new Date();
String timestamp = java.text.DateFormat.getDateTimeInstance().format(now);
chbx.label = timestamp;
}
]]></zscript>
<row self="@{each='proc'}">
<label style="padding-left:15px;color:green" value="@{proc.name}" />
<label value="@{proc.owner}" />
<checkbox checked="@{proc.sent, save-when='self.onCheck', load-when='buttonUpdate.onClick'}" label="@{proc.sendTime, save-when='self.onCheck', load-when='buttonUpdate.onClick'}" onCheck="checkUpdate(self)"/>
<checkbox checked="@{proc.loaded, save-when='self.onCheck', load-when='buttonUpdate.onClick'}" />
</row>
</rows>
</grid>

<grid fixedLayout="true">
<columns sizable="true">
<column label="" width="100px"></column>
<column label="" width="100px"></column>
</columns>
<rows>
<row>
<button id="buttonUpdate" label="Prova" width="100px">
<attribute name="onClick">
if (c.procList.get(0).getSent())
self.label = "positivo";
</attribute>
</button>
<label id="labelUpdateTime" value="Aggiornato a: "/>
</row>
<row>
<button id="buttonReset" width="100px" label="Reset">
<attribute name="onClick">
c.reset();
</attribute>
</button>
</row>
</rows>

</grid>
</window>
</zk>

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-02-19 10:36:20 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

1.The binding manager correctly populates the grid, but the problems is with the checkboxes: 
if I set the association between the checkboxes labels and the data source, the method checkUpdate() in the zscript doesn't work. 
But if I remove that association (that is removing label="@{proc.sendTime,save-when etc..), then the method works. 
It seems like I cannot manually update the checkboxes labels if they are associated to the binding manager. 
Do you have any suggestions?

if you update the model(CommonData.procList) or the component manually(by code) then you should invoke databinder to reload data to component or save data to model.
Moreover, is there a way to access the list element currently elaborated while the binding manager builds the grid?

Don't understand what do you want. colud you split the question one by one?
And finally, is there a way to access and manipulate an element of the row (for example a Label component) from another element of the row (for example a checkbox with the onChecked attribute). I mean, how can I use the id attribute when I use the binding manager to build the grid?
Thanks in advance for your help!

You could use getParent, getChildren of a component to navigate through components, or you can get a Component with the id if you have assigned to it.

link publish delete flag offensive edit

answered 2009-02-19 18:03:27 +0800

airstrike gravatar image airstrike
42

Hi Dennis and thank you for your answer.

> if you update the model(CommonData.procList) or the component manually(by code) then you should invoke databinder to > reload data to component or save data to model.

I'd like to have the checkboxes labels associated to the model(CommonData.procList) whith an automatic save when the labels are modified by the user and an automatic load when the user push the update button. The labels should be modified by the users when they check the checkboxes (triggering the checkUpdate method). Unfortunately the checkUpdate method is not triggered if the checkboxes labels are associated to the binding manager


> Don't understand what do you want. colud you split the question one by one?

Since I can't have the checkUpdate method triggered when the users check the checkboxes (see above), I would like to try, as you suggest, to directly modify the data source and let the binding manager do the loading of the modified data. But how can I access the right list element in the method triggered when the users check the checkboxes?


> You could use getParent, getChildren of a component to navigate through components, or you can get a Component with > the id if you have assigned to it

I tried to add a label in the row but, as with the checkboxes labels, if I associate them to the binder manager with label-id.value="@{proc.sendTime}" then I cannot update label-id.value in the checkUpdate method (It doesn't work)

Do you have any suggestions?

link publish delete flag offensive edit

answered 2009-02-24 10:34:01 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

Ok, I try to simulate your code and have some result.

Cause of some timeing issue and load-on-save feature, you set a component's label(value) in a onXX event is no effect to update the bean and databadin load the bean data back to component.
so the result of sendtime will not been changed;

I rewrite some code to accomplish your case. (since you didn't provide what is CommonData , so I created fake one)

link publish delete flag offensive edit

answered 2009-02-24 10:34:08 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

				<zscript><![CDATA[
void checkUpdate(Checkbox chbx,test.CommonData.Data data){
java.util.Date now = new Date();
String timestamp = java.text.DateFormat.getDateTimeInstance().format(now);
data.setSendTime(timestamp);
}
]]></zscript>
<rows>

				<row self="@{each='proc'}" value="@{proc,save-when=none,load-when=none}">
					<label style="padding-left:15px;color:green"
						value="@{proc.name}" />
					<label value="@{proc.owner}" />
					<checkbox 
						checked="@{proc.sent, save-when='self.onCheck', load-when='buttonUpdate.onClick'}"
						label="@{proc.sendTime, save-when='none', load-when='buttonUpdate.onClick'}"
						onCheck="checkUpdate(self,self.parent.value)" />
					<checkbox
						checked="@{proc.loaded, save-when='self.onCheck', load-when='buttonUpdate.onClick'}" />
				</row>
			</rows>

link publish delete flag offensive edit

answered 2009-02-24 10:34:16 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-02-24 10:35:28 +0800

it binds the proc to row whitout any save/load

<row self="@{each='proc'}" value="@{proc,save-when=none,load-when=none}">

and label no need to save-when since no user opeation can change it, and use code to change it cause timing issue with databainder.

label="@{proc.sendTime, save-when='none', load-when='buttonUpdate.onClick'}"

finally get the 'proc' back with self.parent.value

onCheck="checkUpdate(self,self.parent.value)" />

link publish delete flag offensive edit

answered 2009-02-24 14:04:38 +0800

airstrike gravatar image airstrike
42

YEAH!!!!!!!!!!
Thank you so much my friend!

I exactly needed that smart tip! I was trying to change the labels by code and let the binding manager do the savings. Instead, the right way is to associate the row "value" attribute with its list element "proc" and then modify the list element using "self.parent.value" from the checkbox, letting the binding manager do the loadings.

I really appreciated your help Dennis! I owe you a beer!

link publish delete flag offensive edit

answered 2009-02-24 15:21:08 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

no problem. :)

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: 2009-02-18 10:15:23 +0800

Seen: 957 times

Last updated: Feb 24 '09

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