0

ZK MVVM Combox box Data binding

asked 2013-05-17 10:51:03 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

updated 2013-05-17 10:51:54 +0800

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 ?

delete flag offensive retag edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2013-05-17 11:16:21 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

updated 2013-05-17 11:19:13 +0800

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>
link publish delete flag offensive edit

Comments

This is way which work with JSF ,Struts etc.

sjoshi ( 2013-05-17 11:20:29 +0800 )edit
0

answered 2013-05-17 11:29:20 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

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.

link publish delete flag offensive edit
0

answered 2013-05-17 12:48:42 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

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());

    }
}
link publish delete flag offensive edit

Comments

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 )edit

SelectOption<?> option = (SelectOption<?>) data; item.setValue(option.getValue()) see this line of code m setting value from value while you adding description may be this creating issue

sjoshi ( 2013-05-17 17:32:17 +0800 )edit
0

answered 2013-05-17 18:29:52 +0800

phdsong gravatar image phdsong
13 3

In 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

link publish delete flag offensive edit
0

answered 2013-05-17 18:33:51 +0800

phdsong gravatar image phdsong
13 3

updated 2013-05-17 20:25:40 +0800

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>
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
2 followers

RSS

Stats

Asked: 2013-05-17 10:51:03 +0800

Seen: 158 times

Last updated: May 17 '13

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