Dynamics 365 选项集字段显示指定值(单选项集)

在项目中,经常会碰到,选项集字段显示值会根据其它字段控制联动。比如:未提交状态,只显示1,2;已提交状态,显示3,4...

1.调用示例

if (machinetype == 1) {
        ShowOption("new_returntype", [1, 2]);
    } else if (machinetype == 2) {
        ShowOption("new_returntype", [1, 2]);
    } else if (machinetype == 3) {
        ShowOption("new_returntype", [1]);
    } else if (machinetype == 4) {
        ShowOption("new_returntype", [2, 5]);
    } else {
        ShowOption("new_returntype", [2]);
    }

2. ShowOption方法如下

function ShowOption(field, filterArray) {
    /// <summary>选项集显示指定值(选项集值)</summary>
    /// <param name="filterArray">显示值:[1,2,3]</param>
    let optionsetControl = Xrm.Page.getControl(field);  // OptionSet 的选项对象
    if (optionsetControl != undefined && optionsetControl != null) {
        let options = optionsetControl.getAttribute().getOptions(); // 获取选项所有对象值
        let currentValue = Xrm.Page.getAttribute(field).getValue()
        optionsetControl.clearOptions();  // 从选项集控件清除所有选项
        options.forEach(function (item) {
            let isIn;
            if (this.isIExplorer()) {
                isIn = (filterArray.indexOf(item.value) != -1);
            } else {
                isIn = filterArray.includes(item.value);
            }
            if (isIn === true) {
                optionsetControl.addOption(item);
            }
        });

        //  默认值重新赋值
        if (currentValue) {
            Xrm.Page.getAttribute(field).setValue(currentValue);
        }
    }
}
View Code

 

posted @ 2020-06-23 17:53  30号少年  阅读(481)  评论(0编辑  收藏  举报