setDisable for all fields of a Section in a Crm Form

痞子三分冷 提交于 2019-12-08 18:45:28

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);
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);
          }
        }
    }

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);
        });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!