Revision history [back]

click to hide/show revision 1
initial version

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

nirmalmandhata gravatar image nirmalmandhata flag of India

Unable to change the bind object value of selected combo item

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;
}

}

Unable to change the bind object value of selected combo item

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;
}

}

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