-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi
I just want to know how to set value and label for Combo box in MVVM ?
Say for example, i have state table which contains statecode and statename. In the combo box, as label i want to show statecode + "-" + statename. But while saving into the database, i want to save only statecode.
How we can do this ?
Then you have create a class something like SelectOption which will contain these variable..
import java.io.Serializable;
public class SelectOption<T> implements Serializable,
Comparable<SelectOption<T>> {
private static final long serialVersionUID = 1L;
private T value;
private String label;
private String description;
public SelectOption() {
}
public SelectOption(T value) {
this.value = value;
}
public SelectOption(T value, String label) {
this(value);
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int compareTo(SelectOption<T> o) {
return this.label.compareToIgnoreCase(o.label);
}
public String toString () {
return getLabel() ;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 1;
return result;
}
/**
* Equals method to compare the value of the drop down. This is used by data
* binder for selected item.The value being used for comparing the elements
* since it is unique.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
SelectOption otherObj = (SelectOption) obj;
if (value == null) {
if (otherObj.value != null) {
return false;
}
} else if (!value.equals(otherObj.value)) {
return false;
}
return true;
}
}
And ZUL COde...
ListModelList usersList = new ListModelList<SelectOption<String>>;//Get,Set method
private SelectOption<String> selectedUser = new SelectOption<String>();//Get,set Method
<combobox model="@load(vm.usersList)" autodrop="true" mold="rounded"
selectedItem="@bind(vm.selectedUser)" buttonVisible="true">
<template name="model" var="userobj">
<comboitem label="@load(userobj.label)" value="@load(userobj.value)" />
</template>
</combobox>
Thanks joshi. But i have my own component for Combo box and using many places. Here is the full code.
public class EnumDrpDown extends Combobox {
private static final long serialVersionUID = 1L;
private String dropDownType;
private String enumType;
private List<Enumeration> AllReordsInDB = null;
@WireVariable
private CRUDService CRUDService;
public String getDropDownType() {
return dropDownType;
}
public void setDropDownType(String dropDownType) {
this.dropDownType = dropDownType;
}
public String getEnumType() {
return enumType;
}
public void setEnumType(String enumType) {
this.enumType = enumType;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public EnumDrpDown() {
setMold("rounded");
CRUDService = (CRUDService) SpringUtil.getBean("CRUDService");
setItemRenderer(new ComboitemRenderer() {
public void render(Comboitem item, Object data, int index)
throws Exception {
Enumeration e1 = (Enumeration) data;
item.setLabel(e1.getDescription());
item.setValue(e1.getCode());
}
});
this.addEventListener(Events.ON_CREATE, new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
AllReordsInDB = new ArrayList<Enumeration>();
AllReordsInDB = CRUDService.GetListByNamedQuery(
"Enumeration.getListByType", new Object[] { enumType });
setModel(new BindingListModelList(AllReordsInDB, true));
}
});
this.addEventListener("onAfterRender", new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
if (dropDownType.equals("2")) {
setReadonly(true);
}
}
});
}
}
<component>
<component-name>EnumDrpDown</component-name>
<component-class>com.product.webapp.component.EnumDrpDown
</component-class>
<extends>combobox</extends>
</component>
ZUL File
<EnumDrpDown width="98%"
dropDownType="2" id="insprogram" EnumType="InsProgram"
disabled="@load(vm.makeAsReadOnly)"
value="@bind(fx.insProgram)" />
Every thing works fine. But after saving into the database, instead of storing the code, it stores the description.
What is wrong with the above code.
Then i think you can use something like this...
<combobox model="@load(vm.myStateList)" tooltiptext="My List"
selectedItem="@bind(vm.selectedState)" itemRenderer="com.common.SelectOptionRenderer"
readonly="true" width="130px" />
And the SelectOptionRenderer.java class
public class SelectOptionRenderer implements ComboitemRenderer, Serializable,
ListitemRenderer {
private static final long serialVersionUID = 1L;
public void render(Comboitem item, Object data, int itemIndex)
throws Exception {
SelectOption<?> option = (SelectOption<?>) data;
item.setValue(option.getValue());
item.setLabel(option.getLabel());
if (option.getDescription() != null) {
item.setDescription(option.getDescription());
}
}
public void render(Listitem item, Object data, int itemIndex)
throws Exception {
SelectOption<?> option = (SelectOption<?>) data;
item.setValue(option.getValue());
item.setLabel(option.getLabel());
}
}
Yes. If you see my above code, i have already set the value and label to different property. But still it is not storing the code, it is storing the description.
Senthilchettyin ( 2013-05-17 14:53:27 +0800 )editIn my combobox, I had to call setValue as sjoshi mentioned.
for example,
setSelectedItem(yourItemObject) calls setValue(stringValue).
The following is excerpted from Combobox.java
public void setSelectedItem(Comboitem item) {
if (item != null && item.getParent() != this)
throw new UiException("Not a child: "+item);
if (item != _selItem) {
_selItem = item;
if (item != null) {
setValue(item.getLabel());
} else {
//Bug#2919037: don't call setRawValue(), or the error message will be cleared
if (_value != null && !"".equals(_value)) {
_value = "";
smartUpdate("value", coerceToString(_value));
}
}
_lastCkVal = getValue();
}
}
Let me
This is my example. I set code into value in my comboitem. I planned to create custom combobox like your example near future. It would be appreciated if you can post your solution if this example helps.
<combobox id="attributeDataType" width="150px" readonly="true"
model="@load(vm.dataType)" selectedItem="@load(vm.dataTypeCode) @save(vm.dataTypeCode)"
onAfterRender="@command('openCode', combo=attributeDataType, code=vm.element.dataType)">
<template name="model">
<comboitem label="@load(each.label)" value="@load(each.code)"/>
</template>
</combobox>
Asked: 2013-05-17 10:51:03 +0800
Seen: 158 times
Last updated: May 17 '13
Databinding and auto-complete on combobox
Composite component and bind in ZK 6
How to detach / reattach MVVM windows?
Is there a way to resolve view model properties as input to client side javascripts?
How can I synchronize data in a ListBox in MVVM ? [closed]
MVVM Validator: class not found ? [closed]
How to Call Child ViewModel Method from Parent Window? [closed]