如何获取组合框的显示文本而不是别名? [英] How to get display text of combobox and not the alias?

查看:28
本文介绍了如何获取组合框的显示文本而不是别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些显示值和别名的组合框,当我这样做时在 SSJS 中

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

getComponent("comboboxName").getValue()

它返回别名值,这很好.但是现在我想要的是combobox的显示文本而不是别名值,有什么办法可以得到吗?

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?

推荐答案

定义一个 SSJS 函数 getComponentLabel():

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;
}

它在SelectItemsSelectItem 定义中搜索组件的当前值并返回相应的显示文本(=label).如果没有标签,则返回值.

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.

现在,你得到标签

getComponentLabel("comboboxName")

此代码适用于 XPage 控件:

This code works for XPages controls:

  • 列表框
  • 组合框
  • 单选按钮组
  • Dojo 过滤选择

您可以将 getComponentLabel() 函数保存在服务器 JavaScript 脚本库(例如 Utils.jss)中,并将其作为资源集成到 XPage 中.

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

这是我以前对类似问题的回答的更新版本.

这篇关于如何获取组合框的显示文本而不是别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆