0

How to add an empty value to a combobox?

asked 2015-06-07 23:26:01 +0800

WilliamB gravatar image WilliamB
1609 1 6

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

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-06-22 08:31:21 +0800

WilliamB gravatar image WilliamB
1609 1 6

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

link publish delete flag offensive edit
0

answered 2015-06-21 23:36:31 +0800

Darksu gravatar image Darksu
1991 1 4

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

link publish delete flag offensive edit
Your answer
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
1 follower

RSS

Stats

Asked: 2015-06-07 23:26:01 +0800

Seen: 91 times

Last updated: Jun 22 '15

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