javascript 为 Crm 表单中某个部分的所有字段设置禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24707837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:16:26  来源:igfitidea点击:

setDisable for all fields of a Section in a Crm Form

javascriptdynamics-crm-2011dynamics-crmdynamics-crm-2013disabled-input

提问by MauricioJuanes

I need to disable a section if a value from other field is true, normally I would do:

如果来自其他字段的值为真,我需要禁用一个部分,通常我会这样做:

function disableSection1(disabledStatus){
    Xrm.Page.getControl("section1field1").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field2").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field3").setDisabled(disabledStatus);
    Xrm.Page.getControl("section1field4").setDisabled(disabledStatus);
}

but i have to do this for many sections, so I am looking for a function like this:

但是我必须为许多部分执行此操作,因此我正在寻找这样的功能:

function sectionSetDisabled(tabNumber, sectionNumber, disabledStatus){
    //some code..
}

回答by MauricioJuanes

Most answers I have seen you have use the use the sectionLable and do the following comparison: controlIHave.getParent().getLabel()=="name of the section

我见过的大多数答案都使用了 sectionLable 并进行了以下比较: controlIHave.getParent().getLabel()=="name of the section

But after some trials I found I could use Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).controls.get()to get the controls inside the section. That allowed me to use the following function:

但经过一些试验后,我发现我可以Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).controls.get()用来获得该部分内的控件。这让我可以使用以下功能:

function sectionSetDisabled(tabNumber, sectionNumber, disablestatus) {
    var section = Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber);
    var controls = section.controls.get();
    var controlsLenght = controls.length;

    for (var i = 0; i < controlsLenght; i++) {
        controls[i].setDisabled(disablestatus)
    }
}

by using controls[i].getAttribute()you can then get the attributes of a section.

通过使用,controls[i].getAttribute()您可以获取某个部分的属性。

I ended up creating a object that allows me to disable and clear all the fields in a section:

我最终创建了一个对象,允许我禁用和清除一个部分中的所有字段:

function sectionObject(tabNumber, sectionNumber) {
    var section = Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber);

    this.setDisabled = function (disablestatus) {
        var controls = section.controls.get();
        var controlsLenght = controls.length;
        for (var i = 0; i < controlsLenght; i++) {
            controls[i].setDisabled(disablestatus)
        }
    };

    this.clearFields = function () {
        var controls = section.controls.get();
        var controlsLenght = controls.length;
        for (var i = 0; i < controlsLenght; i++) {
            controls[i].getAttribute().setValue(null);
        }
    };

}

var section=new sectionObject(0,1);
section.setDisabled(true/false);

回答by Minal Salunkhe

function TabObject(tabName, DisableStatus) {  
         var sectionName = Xrm.Page.ui.tabs.get(tabName).sections.get();
         for (var i in sectionName) {
         var controls = sectionName[i].controls.get();
         var controlsLenght = controls.length;
         for (var i = 0; i < controlsLenght; i++) {
             controls[i].setDisabled(DisableStatus);
          }
        }
    }

回答by Jason Faulkner

In CRM 2013 (and later), you can use the forEachiterator. This essentially allows the functionality in a one-liner.

在 CRM 2013(及更高版本)中,您可以使用forEach迭代器。这基本上允许单行的功能。

/* Parameters:
 * tabNumber = Tab Name/Id assigned in the form editor.
 * sectionNumber = Section Name/Id assigned in the form editor.
 */

function sectionSetDisabled(tabNumber, sectionNumber, disabledStatus) {
    // Pull the tab, then section (within the tab) and create an iterator.
    Xrm.Page.ui.tabs.get(tabNumber).sections.get(sectionNumber).controls.forEach(
        // Delegate to set the status of all controls within the section.
        function (control, index) {
            control.setDisabled(disabledStatus);
        });
}