How to get display text of combobox and not the al

2019-07-31 17:12发布

问题:

I have a combobox with some display values and alias, In SSJS when I do

getComponent("comboboxName").getValue()

it returns alias value, which is fine. But now I want the display text of the combobox and not the alias value, is there any way to get it?

回答1:

Define a SSJS function getComponentLabel():

function getComponentLabel(componentId) {       
    var select = getComponent(componentId); 
    var value = select.getValue();
    if (value) {
        try {
            var list = select.getChildren();
            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;
}

It searches for component's current value in SelectItems and SelectItem definitions and returns the corresponding display text (=label). In case there is no label it returns the value.

Now, you get the label with

getComponentLabel("comboboxName")

This code works for XPages controls:

  • List Box
  • Combo Box
  • Radio Button Group
  • Dojo Filtering Select

You can save the getComponentLabel() function in a Server JavaScript Scriptlibrary (e.g. Utils.jss) and integrate it in your XPages as resource.

This is a renewed version of my former answer to a similar question.