-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Is there any clean way to add an empty (null) value to a combobox on top of the ListModel defined as Model?
I'm using MVVM, and the ListModel is actualy an Enum . values().
If the user pick a choice, he cannot go back to blank at the moment ...
Thanks for your help
Hello Darksu thanks for your reply. I had the enum display part working fine.
My issue was about how to add an empty item in my combobox without affecting the model, as I did not want to create an "Empty" value in my enum.
I got it working by overriding the combobox component
public class ComboWithEmptyValue extends Combobox {
private static final long serialVersionUID = 1L;
private boolean withEmptyOption = false;
public void setWithEmptyOption(final boolean pWithEmptyOption) {
withEmptyOption = pWithEmptyOption;
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setModel(final ListModel<?> pModel) {
// If one wants to add an empty value
if (withEmptyOption) {
// we recreate the list
ListModelList list = new ListModelList(pModel.getSize() + 1);
// And add an empty value at the start of the list
list.add(0, null);
// Then the original list value
list.addAll((Collection) pModel);
super.setModel(list);
} else {
// Otherwise we do nothing special
super.setModel(pModel);
}
}
}
And to override the original combobox, i added to lang-addon.xml :
<component>
<component-name>combobox</component-name>
<extends>combobox</extends>
<component-class>com.mywebapp.component.combobox.ComboWithEmptyValue</component-class>
</component>
Exemple with the following enum as model :
public enum TypeEntityEnum {
VALUE1("com.value1.label"),
VALUE2("com.value1.label"),
VALUE3("com.value1.label"),
VALUE4("com.value1.label");
private TypeEntityEnum(final String pLabelKey) {
labelKey = pLabelKey;
}
private String labelKey ;
}
And in the zul, after i populate the vm.listTypeEntity ViewModel variable with TypeEntityEnum.values()
<combobox model="@init(vm.listTypeEntity)" readonly="true" withEmptyOption="true"
selectedItem="@save(vm.myEnumSelectedValue)">
<template name="model" var="typeEntity">
<comboitem label="${c:l(typeEntity.label)}"/>
</template>
</combobox>
There is currently a little issue with the way ZK displays an empty value in the combobox : http://tracker.zkoss.org/browse/ZK-2783?filter=14300 The CSS to fix it can be found in the jira.
Hope this will help someone.
Regards
Hello WilliamB,
You can use the following fiddle:
http://zkfiddle.org/sample/19j7r1i/1-Using-Enum-class-as-Model-in-Combobox#source-1
At the example you can go again to blank by pressing the backspace button.
Best Regards,
Darksu
Asked: 2015-06-07 23:26:01 +0800
Seen: 91 times
Last updated: Jun 22 '15
Combobox issue :Taking time to load
Combobox with multiple items taking longer time to render
Zk Combobox onOpen Event issue
Combo box OnDouble Click Event and SelectedItem
Combobox model='xxx' whcih is not ListModel
Combobbox selectedItem issues. Unsupported child for Combobox: <Listitem null>