我在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;
}
}
}
在控制台中,我获得了正确的当前状态和数组中过滤后的允许选项。但表单字段仍然显示所有选项,而不是预期的允许选项。
按照建议这里,我已经改变了
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);
}
}
并且效果完美。