I would like to ask how to use the computed field to display listbox value?
My idea is there is a listbox and a computed field. In the listbox, I use partial update in the onchange event and the partial update id is computed field
I may post the code about the listbbox and the computed field here:
For the listbox:
<xp:listBox id="listBox2">
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:viewScope.get("vs1");
}]]></xp:this.value>
</xp:selectItems>
<xp:eventHandler event="onchange" submit="true"
refreshMode="partial" refreshId="computedField1">
</xp:eventHandler></xp:listBox>
For the computed field: In this part I have read this post: How to get display text of combobox and not the alias? to learn how display the listbox value.
function getComponentLabel(componentId) {
var select = getComponent(componentId);
var value = select.getValue();
try {
var list = select.getChildren();
if (value) {
for (var i = 0; i < list.length; i++) {
if ((typeof list[i]).indexOf("SelectItems") > -1) {
items = list[i].getValue();
for (var k = 0; k < items.length; k++) {
if (items[k].getValue() === value) {
return items[k].getLabel();
}
}
}
else if((typeof list[i]).indexOf("SelectItem") > -1) {
if (list[i].getItemValue() === value) {
return list[i].getItemLabel();
}
}
}
}
} catch (e) {
}
return value;
}
var dspValue = getComponentLabel("listBox1");//yes, it's listbox 1 not listbox2, if type listbox2, the computed field will show null (not sure why)
return "The listbox selected value(s) is: " + dspValue;
So far when I run the code at the first time, I put the value in the listbox, the computed field can display the selected value from the list box. But when I put multiple values in the listbox, the computed field can only show the latest value that I've put.
I have tried to use for loop in the computed field to get the selected values in list box:
var text="";
for (var i = 0; i < dspValue.length; i++)
{
text += dspValue[i];
}
return "you select value is: " + text;
Then I run the code, I get the error message said the dspValue is null. I also try to display the dspValue.length without the for loop and get the same error.
Instead of use for loop, I don't have the idea to get all values from the listbox and show in the computed field.
How can I use the computed field to display all the selected values in the listbox? I appreciate for any advice please.