-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Combobox seems to not work properly with data binding. The attribute selectedItem seems to only work one way: when the user select an item, the value is updated for the form and the VM. When I pass the bottle in parameter and I update the selectedBottle in afterCompose method, the combobox cbSelectedProduct is empty. But, the value fx.product is correct if I put a label to display it. I tried without form and the behiavoir is the same. I use hibernate and Bottle is ManyToOne relation.
What did I wrong?
Thanks a lot
My view
<zk>
<window id="bottleForm" apply="org.zkoss.bind.BindComposer"
title="Add bottle"
viewModel="@id('vm')@init('net.maximeroussel.vetDrugsInventory.vm.BottleFormVM')"
form="@id('fx') @load(vm.selectedBottle) @save(vm.selectedBottle, before='save')"
validationMessages="@id('vmsgs')" width="350px" closable="true">
<grid>
<columns>
<column hflex="min"/>
<column hflex="2"/>
</columns>
<rows>
<row>
<label value="Product"></label>
<combobox id="cbSelectedProduct" model="@load(vm.availableProducts)"
selectedItem="@bind(fx.product)" readonly="true" focus="true">
<template name="model">
<comboitem label="@load(each.name)" value="@bind(each)"/>
</template>
</combobox>
</row>
<row>
<label value="Capacity"></label>
<doublebox value="@bind(fx.capacity)" constraint="no empty"/>
</row>
<row>
<label value="Expiration date"></label>
<datebox value="@bind(fx.expirationDate)" constraint="no past"/>
</row>
<row>
<label value="Purchase date"></label>
<datebox value="@bind(fx.purchaseDate)" constraint="no empty,no future"/>
</row>
</rows>
</grid>
<separator height="25px" />
<button label="Save" onClick="@command('save')"></button>
</window>
</zk>
VM
package net.maximeroussel.vetDrugsInventory.vm;
import java.util.List;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
import net.maximeroussel.vetDrugs.RepositoriesHolder;
import net.maximeroussel.vetDrugsInventory.entities.Bottle;
import net.maximeroussel.vetDrugsInventory.entities.Product;
public class BottleFormVM {
Bottle selectedBottle;
@Wire
Window bottleForm;
@Wire
Combobox cbSelectedProduct;
Object parentVM;
boolean editMode;
@AfterCompose
public void afterCompose( @ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("bottle") Bottle bottle,
@ExecutionArgParam("parentVM") Object parentVM){
Selectors.wireComponents(view, this, false);
this.parentVM = parentVM;
if (bottle != null) {
selectedBottle = bottle;
bottleForm.setTitle("Edit bottle");
editMode = true;
} else {
selectedBottle = new Bottle();
editMode = false;
}
}
@SuppressWarnings("unchecked")
@Command
public void save() {
RepositoriesHolder.getInstance().getBottlesRepository().save(selectedBottle);
if (editMode) {
BottleFormVM.this.bottleForm.detach();
} else {
Messagebox.show("Success! The bottle Id is " + selectedBottle.getId(),
"Message dialog", Messagebox.OK, Messagebox.INFORMATION,
new org.zkoss.zk.ui.event.EventListener() {
public void onEvent(Event evt) throws Exception {
BottleFormVM.this.bottleForm.detach();
BindUtils.postNotifyChange(null, null, parentVM, "activeBottles");
}
});
}
}
public List<Product> getAvailableProducts() {
return RepositoriesHolder.getInstance().getProductsRepository().findActiveOrderByName();
}
public Bottle getSelectedBottle() {
return selectedBottle;
}
public void setSelectedBottle(Bottle selectedBottle) {
this.selectedBottle = selectedBottle;
}
}
1-Why you are mixing MVC and MVVM architecture here i do not think here any need to use @Wire
as you already using MVVM Architecture.
2-And Selected item will only work when user will selected a item from the combobox otherwise it will be empty.
3-What you want to chive here Can you please clarify are you getting value from cmbobox in aftercompose()?
I fix my problem by adding the value attribute:
<combobox id="cbSelectedProduct" model="@load(vm.availableProducts)" selectedItem="@bind(fx.product)" readonly="true" focus="true" value="@load(fx.product.name)">
<template name="model">
<comboitem label="@load(each.name)" value="@bind(each)"/>
</template>
</combobox>
Thanks
hi maximeroussel, which zk library are you using for your project? also I have a similar problem that occurs using libraries 6.5.1. I think that the use of selectedItem should be the only, for both bindings that for the display of the values, as it is with the use of libraries 6.0. It could be a bug? I show a very simple example that raises failure using libraries 6.5.1, while working with libraries 6.0.
zul file
<?page title="Combo" contentType="text/html;charset=UTF-8"?><zk>
<window title="Combo" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('umvm') @init('viewModel.ComboManagementVM')">
New Content Here!
<hbox>
<combobox id="alarmCodeList"
model="@load(umvm.clearCombo.alarmCodeList)"
selectedItem="@bind(umvm.clearCombo.selectedAlarmCode) ">
<template name="model" var="code">
<comboitem label="@load(code)" />
</template>
</combobox>
<combobox
model="@load(umvm.clearCombo.alarmNotificationEventList)"
selectedItem="@bind(umvm.clearCombo.selectedAlarmNotificationEvent)">
<template name="model" var="notifyEvent">
<comboitem
label="@load(notifyEvent)" />
</template>
</combobox>
<combobox
model="@load(umvm.clearCombo.alarmNotificationModeList)"
selectedItem="@bind(umvm.clearCombo.selectedAlarmNotificationMode)">
<template name="model">
<comboitem label="@load(each)" />
</template>
</combobox>
<button label="CLEAR" onClick="@command('clear')"></button>
</hbox>
</window></zk>
ViewModel
package viewModel;import org.zkoss.bind.annotation.Command;import org.zkoss.bind.annotation.Init;import org.zkoss.bind.annotation.NotifyChange;public class ComboManagementVM{private ClearCombo clearCombo;public ComboManagementVM (){
}
@Init
public void init () {
try {
clearCombo = new ClearCombo();
listInit();
} catch (Throwable ex) {
}
}
private void listInit(){
/*alarm code*/
clearCombo.getAlarmCodeList();
/*alarm notification Event*/
clearCombo.getAlarmNotificationEventList();
/*alarm notification Mode*/
clearCombo.getAlarmNotificationModeList();
}
@Command
@NotifyChange ({"clearCombo"})
public void clear (){
clearCombo.setSelectedAlarmCode(null);
clearCombo.setSelectedAlarmNotificationEvent(null);
clearCombo.setSelectedAlarmNotificationMode(null);
}
public void setClearCombo(ClearCombo clearCombo) {
this.clearCombo = clearCombo;
}
public ClearCombo getClearCombo() {
return clearCombo;
} }
Model
package viewModel;import java.util.ArrayList;import java.util.List; public class ClearCombo {
private List <String> alarmCodeList;
private List <String> alarmNotificationEventList;
private List <String> alarmNotificationModeList;
private String selectedAlarmCode;
private String selectedAlarmNotificationEvent;
private String selectedAlarmNotificationMode;
ClearCombo(){
alarmCodeList = new ArrayList<String>();
alarmNotificationEventList =new ArrayList<String>();
alarmNotificationModeList =new ArrayList<String>();
alarmCodeList.add("a1");
alarmCodeList.add("a2");
alarmCodeList.add("a3");
alarmCodeList.add("a4");
alarmNotificationEventList.add("b1");
alarmNotificationEventList.add("b2");
alarmNotificationEventList.add("b3");
alarmNotificationEventList.add("b4");
alarmNotificationModeList.add("c1");
alarmNotificationModeList.add("c2");
alarmNotificationModeList.add("c3");
alarmNotificationModeList.add("c4");
}
public List<String> getAlarmCodeList() {
System.out.println("alarmCode size "+alarmCodeList.size() );
return alarmCodeList;
}
public void setAlarmCodeList(List<String> alarmCodeList) {
this.alarmCodeList = alarmCodeList;
}
public List<String> getAlarmNotificationEventList() {
return alarmNotificationEventList;
}
public void setAlarmNotificationEventList(
List<String> alarmNotificationEventList) {
this.alarmNotificationEventList = alarmNotificationEventList;
}
public List<String> getAlarmNotificationModeList() {
return alarmNotificationModeList;
}
public void setAlarmNotificationModeList(
List<String> alarmNotificationModeList) {
this.alarmNotificationModeList = alarmNotificationModeList;
}
public String getSelectedAlarmCode() {
return selectedAlarmCode;
}
public void setSelectedAlarmCode(String selectedAlarmCode) {
this.selectedAlarmCode = selectedAlarmCode;
}
public String getSelectedAlarmNotificationEvent() {
return selectedAlarmNotificationEvent;
}
public void setSelectedAlarmNotificationEvent(
String selectedAlarmNotificationEvent) {
this.selectedAlarmNotificationEvent = selectedAlarmNotificationEvent;
}
public String getSelectedAlarmNotificationMode() {
return selectedAlarmNotificationMode;
}
public void setSelectedAlarmNotificationMode(
String selectedAlarmNotificationMode) {
this.selectedAlarmNotificationMode = selectedAlarmNotificationMode;
}}
Asked: 2013-02-26 14:46:40 +0800
Seen: 386 times
Last updated: Mar 13 '13
Using databinding in menupopup
ZK Clustering, Session Replication Question [closed]
Problem binding values to a composite component
How can i load a constant in .zul
set selectedItem of list in viewmodel - cleaner way? [closed]
search button using validation
[Ugent please] connect Web service .net view in grid group ZK