如何根据 Dynamics 365 表单中的当前状态筛选“选项集”字段选项

问题描述 投票:0回答:1

我在dynamics 365表单中有一个选项集字段,我想动态控制表单中可用的选项集值,以便根据当前状态,可用选项应该改变。但它无法使用 Javascript 工作

我在表单加载事件中使用以下代码。

function setStatusoptions(executionContext) {
    var formContext = executionContext.getFormContext();
    var statusField = formContext.getAttribute("new_status");
    
    var currentStatus = statusField.getValue();
    var allowedOptionValues = [];
    console.log("currentstat="+currentStatus);

    if (currentStatus === 717800000) {
        allowedOptionValues = [717800001,717800002];
    } else if (currentStatus === 717800001) {
        allowedOptionValues = [717800002, 717800003];
    } 
    console.log(allowedOptionValues);
    
    // Loop through all the options in the status field
    var options = statusField.getOptions();
    for (var i = 0; i < options.length; i++) {
        var option = options[i];
        
       // Check if the option value is in the allowedOptionValues array
        if (allowedOptionValues.indexOf(option.value) === -1) {
            // Disable the option
            option.disabled = true;
        } else {
            // Enable the option
            option.disabled = false;
        }
    }
}

在控制台中,我获得了正确的当前状态和数组中过滤后的允许选项。但表单字段仍然显示所有选项,而不是预期的允许选项。

javascript dynamics-365
1个回答
0
投票

按照建议这里,我已经改变了

 var options = statusField.getOptions();
for (var i = 0; i < options.length; i++) {
    var option = options[i];
    
   // Check if the option value is in the allowedOptionValues array
    if (allowedOptionValues.indexOf(option.value) === -1) {
        // Disable the option
        option.disabled = true;
    } else {
        // Enable the option
        option.disabled = false;
    }
}

var statusFieldControl = formContext.getControl("new_status");

// Get the attribute options
var options = statusFieldControl.getAttribute().getOptions();

// Clear existing options
statusFieldControl.clearOptions();

// Add only allowed options back
for (var i = 0; i < options.length; i++) {
    var option = options[i];
    
    if (allowedOptionValues.indexOf(option.value) !== -1) {
        statusFieldControl.addOption(option);
    }
}

并且效果完美。

© www.soinside.com 2019 - 2024. All rights reserved.