0

Unable to change the bind object value of selected combo item

asked 2016-10-28 10:29:15 +0800

nirmalmandhata gravatar image nirmalmandhata flag of India
2 3

updated 2016-10-28 10:33:43 +0800

We need to select the combo item through index and on saving it should save in the database. I am getting the combo box object (using id and executing javascript query by passing an java class object) then selecting combo item by using setSelectedIndex(index) in the java class method. It selects the combo item in the ui but but the binded value doesn't change. It calls the setValue(SelectOption<t> value) method of SelectOptionBinder class but here the value object is null and thats why it is unable to set value.

Can you please suggest how it can be done.

We have codes like this < combobox width="100%" autodrop="true" id="peopleeditPeopleType_component" model="@load(vm.peopleCode)" selectedItem="@bind(vm.selectedPeopleCode.value)" itemRenderer="com.csdcsystems.amanda.common.SelectOptionRenderer" tooltiptext="select people type" placeholder="select type" />

---------------------------------------------------------peopleCode--------------

List < SelectOption< Integer>> peopleCode;

------------------------------------------------------selectedPeopleCode ----------

selectedPeopleCode = new SelectOptionBinder< >(peopleRecord, People.PEOPLE.PEOPLE_CODE, peopleCode); ---------------------------------------------------SelectOptionRenderer 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()); } if(option.getStyle() != null){ item.setStyle(option.getStyle()); }

}
public void render(Listitem item, Object data, int itemIndex) throws Exception {
    SelectOption<?> option = (SelectOption<?>) data;
    item.setValue(option.getValue());
    item.setLabel(option.getLabel());
}

}

---------------------------------------------------------SelectOptionBinder class--

public class SelectOptionBinder<t> implements Serializable {

private static final long serialVersionUID = 1L;
private UpdatableRecord<?> record;
private Field<T> column;
private Collection<SelectOption<T>> options;
    public SelectOptionBinder(UpdatableRecord<?> record, Field<T> column, Collection<SelectOption<T>> options) {
    this.record = record;
    this.column = column;
    this.options = options;
}

public SelectOption<T> getValue() {
    return getOption(record.getValue(column));
}

public void setValue(SelectOption<T> value) {
    if (value != null) {
        record.setValue(column, value.getValue());
    } else {
        record.setValue(column, null);
    }
}

protected SelectOption<T> getOption(T value) {
    SelectOption<T> returnValue = null;
        for (SelectOption<T> option : options) {
            if (ObjectUtils.equals(value, option.getValue())) {
                returnValue = option;
                break;
            }
        }
    return returnValue;
}

}

--------------------------------------- selectOption class -----

public class SelectOption<t> implements Serializable, Comparable<selectoption<t>> { private static final long serialVersionUID = 1L; private T value; private String label; private String description; private String style; 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 String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public int compareTo(SelectOption<t> o) { if (UserSubject.current() == null || UserSubject.current().isEnglish()) { return this.label.compareToIgnoreCase(o.label); } else { return Collator.getInstance(Language.LANGUAGE_2.getLocale()).compare(this.label, o.label); } } public String toString() { return getLabel(); } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { if (value == null) { return super.hashCode(); } return value.hashCode(); }

/**
 * 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;
    } else if (obj == null) {
        return false;
    } else if (getClass() != obj.getClass()) {
        return false;
    } else {
        if (obj instanceof SelectOption<?>) {
            SelectOption<?> otherObj = (SelectOption<?>) obj;
            if (value == null) {
                if (otherObj.value != null) {
                    return false;
                }
            } else if (!value.equals(otherObj.value)) {
                return false;
            }   
            return true;
        }
        return false;
    }
}
public boolean isNotEmpty() {
    if(value!=null)
    return StringUtils.isNotBlank(value.toString());
    else
        return false;
}

}

delete flag offensive retag edit

Comments

Is there any work around for this?

nirmalmandhata ( 2016-10-31 06:49:10 +0800 )edit
Be the first one to answer this question!
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: 2016-10-28 10:29:15 +0800

Seen: 34 times

Last updated: Oct 28 '16

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