How to get display text of combobox and not the alias?

前端 未结 1 629
自闭症患者
自闭症患者 2021-01-24 07:47

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

getComponent(\"comboboxName\").getValue()

it returns alias value, whic

相关标签:
1条回答
  • 2021-01-24 08:37

    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.

    0 讨论(0)
提交回复
热议问题