setDisable for all fields of a Section in a Crm Fo

2019-06-03 06:16发布

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

4条回答
放我归山
3楼-- · 2019-06-03 06:57
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);
          }
        }
    }
查看更多
趁早两清
4楼-- · 2019-06-03 06:59

Most answers I have seen you have use the use the sectionLable and do the following comparison: 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:

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.

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);
查看更多
老娘就宠你
5楼-- · 2019-06-03 07:11

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

/* 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);
        });
}
查看更多
登录 后发表回答