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

房东的猫 提交于 2019-12-11 04:52:42

问题


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.



来源:https://stackoverflow.com/questions/23386111/how-to-get-display-text-of-combobox-and-not-the-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!