-
FEATURED COMPONENTS
First time here? Check out the FAQ!
<combobox
model="@load(vm.patientModelList)" hflex="1"
width="85px" mold="rounded"
value="@bind(vm.patientCode)"
autodrop="true" autocomplete="true">
<template
name="model">
<comboitem
label="@load(each.formattedName)"
value="@load(each.code)" />
</template>
</combobox>
In the above code, i am expecting vm.patientcode should contain the code, but after selecting, it actually contains the formattedName.
If i change label = @load(each.code) then it works fine. But i want to display the name and i want the underlined code after selecting
what is the way to achieve that ?
Combobox will take the label of selected Comboitem as its value, you can use a custom comboitem to render what you want as below:
test.zul
<zk>
<window apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('test.TestVM')">
<combobox model="@load(vm.patientModelList)"
value="@bind(vm.patientCode)"
width="85px" mold="rounded"
autodrop="true" autocomplete="true">
<template name="model">
<comboitem use="test.CustomComboitem" label="@load(each.code)"
displayLabel="@load(each.formattedName)" />
</template>
</combobox>
</window>
</zk>
TestVM.java
package test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.zkoss.zul.ListModelList;
public class TestVM implements Serializable {
ListModelList _patientModelList;
private String _patientCode = "";
public ListModelList getPatientModelList () {
if (_patientModelList == null) {
List<Patient> patientList = new ArrayList<Patient>();
for (int i = 0; i < 10; i++) {
patientList.add(new Patient("Name_" + (i+1), i+1 + ""));
}
_patientModelList = new ListModelList(patientList);
}
return _patientModelList;
}
public String getPatientCode () {
return _patientCode;
}
public void setPatientCode (String patientCode) {
_patientCode = patientCode;
}
}
Patient.java
package test;
public class Patient {
private String _name;
private String _code;
public Patient (String name, String code) {
_name = name;
_code = code;
}
public String getName () {
return _name;
}
public String getCode () {
return _code;
}
public String getFormattedName () {
return "Formatted " + getName();
}
}
CustomComboitem.java
package test;
import org.zkoss.zul.Comboitem;
public class CustomComboitem extends Comboitem {
private String _displayLabel;
public CustomComboitem () {
setWidgetOverride ("domLabel_", "function () {\n"
+ " return zUtl.encodeXML(this._displayLabel || this.getLabel(), {pre: 1});\n"
+ " }\n");
setWidgetOverride ("setDisplayLabel", "function (v) {\n"
+ " this._displayLabel = v;\n"
+ " }\n");
}
public void setDisplayLabel (String displayLabel) {
if (displayLabel == null) {
displayLabel = "";
}
if (!displayLabel.equals(_displayLabel)) {
_displayLabel = displayLabel;
smartUpdate("displayLabel", _displayLabel);
}
}
// override
protected void renderProperties(org.zkoss.zk.ui.sys.ContentRenderer renderer)
throws java.io.IOException {
super.renderProperties(renderer);
render(renderer, "displayLabel", _displayLabel);
}
}
Hi Mr.Senthil, Try This.
In ZUL..
<combobox id="cmbx_patient">
<comboitem label="Patient1Name" value="patient1id">
<comboitem label="Patient2Name" value="patient2id">
</combobox>
In Java Class..
cmbx_patient.addEventListener(Events.ON_SELECT, new EventListener() {
public void onEvent(Event evt)throws Exception
{
cmbx_patient.setValue(cmbx_patient.getSelectedItem().getValue()+"");
}
});
Greetings...
M Varun.
Asked: 2014-02-05 19:42:49 +0800
Seen: 100 times
Last updated: Feb 11 '14