Hi!
I used a ComboBox with integer values (on client side - as double) and string labels (PolymerTemplate).
In the template model, a getter was declared for combobox value (return Integer) with @Encoder for transform Integer<->Double.
For initial state: value=null - ok
For item selection: value=Double->Integer - ok
For clearing selection: value=String=‘’ (not null!) - error!
How to correctly implement this dual type case? Is the usage of string values the only way?
Bean for ComboBox items:
public class UIComboItem {
private Integer value;
private String label;
public UIComboItem(Integer value, String label) {
this.value = value;
this.label = label;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
Template model:
public interface UIModel extends TemplateModel {
void setFirms(List<UIComboItem> items);
@Encode(IntegerToDoubleEncoder.class)
Integer getIdFirm();
}
Value encoder:
public static class IntegerToDoubleEncoder implements ModelEncoder<Integer, Double> {
@Override
public Double encode(Integer modelValue) {
return modelValue == null ? null : new Double(modelValue);
}
@Override
public Integer decode(Double presentationValue) {
return presentationValue == null ? null : presentationValue.intValue();
}
}
Polymer template html:
<vaadin-combo-box items="[[firms]
] value="{{idFirm}}"></vaadin-combo-box>