show and modify List in grid ZK -
i have list dynamically generated view. when button clicked, new row added, value entered , saved.
.zul
<zk> <window border="normal" title="hello" viewmodel="@id('vm') @init('gemalto.createserviceversion')" apply="org.zkoss.bind.bindcomposer"> <grid id="demogrid" model="@load(vm.profilelist) @template((each.editingstatus) ? 'editable' : 'noneditable')"> <columns sizable="true"> <column width="160px" >value</column> <column width="160px" ></column> </columns> <rows> <template name="editable"> <row> <textbox id="valuetextbox" value="@load(each.serviceprofile.valuevariable) @save(each.serviceprofile.valuevariable, before='confirm')" /> <button image="/img/save.png" label="save" onclick="@command('confirm', currentvariable=each)"/> </row> </template> <template name="noneditable"> <row> <label value="@load(each.serviceprofile.valuevariable)" /> </row> </template> </rows> </grid> <div align="center"> <button label="add" image="/img/create.png" onclick="@command('onaddnew')" /> </div> </window> </zk>
view model
public class createserviceversion extends selectorcomposer<component> { private boolean isediting = false; private boolean displayedit = true; private boolean isaddnew = false; private list<serviceprofilestatus> profilelist = new arraylist<serviceprofilestatus>(); public list<serviceprofilestatus> getprofilelist() { return profilelist; } @aftercompose public void aftercompose() { profilelist.add(new serviceprofilestatus(new serviceprofile("value1"), false)); profilelist.add(new serviceprofilestatus(new serviceprofile("value2"), false)); } @command public void crudserviceversion() { executions.sendredirect("crudserviceversion.zul"); } @command @notifychange({"profilelist"}) public void onaddnew() { if (!isediting) { serviceprofilestatus sps = new serviceprofilestatus(new serviceprofile(""), displayedit); profilelist.add(0, sps); isaddnew = true; isediting = true; } } @command public void confirm(@bindingparam("currentvariable") serviceprofilestatus sps) { isediting = false; isaddnew = false; sps.seteditingstatus(isediting); bindutils.postnotifychange(null,null,sps,"*"); } }
the problem add new item, value copied others items.
i put images see more happening.
- imgur.com/7u7okpg
- imgur.com/mf8puyi
- imgur.com/ajpnoxm
i tested code , works me.
changed code little bit :
- removed
extends selectorcomposer
viewmodel cause not needed mvvm. - changed 2 templates 1 template.
- usage of boolean in viewmodel in zul.
zul :
<?xml version="1.0" encoding="utf-8"?> <zk> <window border="normal" title="hello" viewmodel="@id('vm') @init('be.chillworld.createserviceversion')" apply="org.zkoss.bind.bindcomposer"> <grid id="demogrid" model="@load(vm.profilelist)"> <columns sizable="true"> <column width="160px" >value</column> <column width="160px" ></column> </columns> <rows> <template name="model"> <row> <textbox value="@load(each.serviceprofile.valuevariable) @save(each.serviceprofile.valuevariable, before='confirm')" /> <button disabled="@load(not each.editingstatus)" visible="@load(each.editingstatus)" image="/img/save.png" label="save" onclick="@command('confirm', currentvariable=each)"/> </row> </template> </rows> </grid> <div align="center"> <button disabled="@load(vm.maycreatenew)" label="add" image="/img/create.png" onclick="@command('onaddnew')" /> </div> </window> </zk>
viewmodel :
package be.chillworld; import java.util.arraylist; import java.util.list; import org.zkoss.bind.bindutils; import org.zkoss.bind.annotation.aftercompose; import org.zkoss.bind.annotation.bindingparam; import org.zkoss.bind.annotation.command; import org.zkoss.bind.annotation.notifychange; import org.zkoss.zk.ui.executions; /** * * @author chillworld */ public class createserviceversion { private boolean isediting = false; private list<serviceprofilestatus> profilelist = new arraylist<serviceprofilestatus>(); public list<serviceprofilestatus> getprofilelist() { return profilelist; } @aftercompose public void aftercompose() { profilelist.add(new serviceprofilestatus(new serviceprofile("value1"), false)); profilelist.add(new serviceprofilestatus(new serviceprofile("value2"), false)); } @command public void crudserviceversion() { executions.sendredirect("crudserviceversion.zul"); } @command @notifychange({"profilelist","maycreatenew"}) public void onaddnew() { isediting = true; serviceprofilestatus sps = new serviceprofilestatus(new serviceprofile(""), true); profilelist.add(0, sps); } @command @notifychange("maycreatenew") public void confirm(@bindingparam("currentvariable") serviceprofilestatus sps) { isediting = false; sps.seteditingstatus(isediting); bindutils.postnotifychange(null, null, sps, "*"); } public boolean getmaycreatenew() { return isediting; } }
serviceprofilestatus.java :
package be.chillworld; /** * * @author chillworld */ public class serviceprofilestatus { private serviceprofile serviceprofile; private boolean editingstatus; public serviceprofilestatus(serviceprofile serviceprofile, boolean editingstatus) { this.serviceprofile = serviceprofile; this.editingstatus = editingstatus; } public boolean iseditingstatus() { return editingstatus; } public void seteditingstatus(boolean editingstatus) { this.editingstatus = editingstatus; } public serviceprofile getserviceprofile() { return serviceprofile; } public void setserviceprofile(serviceprofile serviceprofile) { this.serviceprofile = serviceprofile; } public serviceprofilestatus(serviceprofile serviceprofile) { this.serviceprofile = serviceprofile; } }
serviceprofile.java :
package be.chillworld; /** * * @author chillworld */ public class serviceprofile { private string valuevariable; public serviceprofile(string valuevariable) { this.valuevariable = valuevariable; } public string getvaluevariable() { return valuevariable; } public void setvaluevariable(string valuevariable) { this.valuevariable = valuevariable; } }
Comments
Post a Comment