Toggle or Hidden MS CRM Tab

Often times we need to toggle the visiblity of an element based on the value of a field. This JScript file adds the methods to accomplish this by calling the setVisibility function. For example, in an onChange event for new_mypicklist I can call:

  1. setVisibility('section','new_mypicklist','section_main',42);  

This hides the section_main section if the value is not 42, which simplifies the process of hiding and showing relevant information.

I also like to use checkboxes to handle the visibility of elements, but checkbox values dont’ change until they lose focus. To work around the issue, I register an onClick event during the form load instead of using the onChange event. This unsupported method allows me to modify the value onClick and change the visibility immediately. To use this I just need to call it in the onload:

  1. registerToggle('section', 'new_checkbox', 'section_main');  

If you wanted to hide it when new_checkbox was false (or no), then you would need to specify the value, 0.

  1. registerToggle('section', 'new_checkbox', 'section_main',0);  

JScript: visibility.js

This is the main javascript file… visibility.js that you would need to add and reference as a web resource in CRM 2011, everything except for the registerToggle uses supported methods.

  1. function registerToggle(t, attr, c, v) {  
  2.     if (typeof (v) === "undefined" || v === null) {  
  3.         var v = 1;  
  4.     }  
  5.     var ctrl = Xrm.Page.ui.controls.get(attr);  
  6.     var a = ctrl.getAttribute();  
  7.     var el = document.getElementById(attr);  
  8.   
  9.     // Build Toggle Function  
  10.     var f = "var ef=function() { " +  
  11.               "var a = Xrm.Page.data.entity.attributes.get(attr); " +  
  12.               "a.setValue(!a.getValue()); " +  
  13.               "setVisibility('" + t + "','" + attr + "','" + c + "'," + v + ");" +  
  14.               " };";  
  15.   
  16.     eval(f);  
  17.   
  18.     // Attach to click event  
  19.     el.attachEvent('onclick', ef, false);  
  20.   
  21.     // Set visibility  
  22.     setVisibility(t, attr, c, v);  
  23. }  
  24.   
  25. function setVisibility(t, attr, c, v) {  
  26.     switch (t.toLowerCase().charAt(0)) {  
  27.         //tab  
  28.         case 't': setTabVisibility(attr, c, v);  
  29.             break;  
  30.         //section  
  31.         case 's': setSectionVisibility(attr, c, v);  
  32.             break;  
  33.         //control  
  34.         case 'c': setControlVisibility(attr, c, v);  
  35.             break;  
  36.         //navigation  
  37.         case 'n': setNavigationVisibility(attr, c, v);  
  38.             break;  
  39.     }  
  40. }  
  41.   
  42. function setNavigationVisibility(attributename, navitemname, value) {  
  43.     var attribute = Xrm.Page.data.entity.attributes.get(attributename);  
  44.     var navitem = Xrm.Page.ui.navigation.items.get(navitemname);  
  45.     if (navitem === null)  
  46.     {  
  47.         return;  
  48.     }  
  49.     navitem.setVisible(attribute.getValue() == value);  
  50. }  
  51.   
  52. function setTabVisibility(attributename, tabname, value) {  
  53.     var attribute = Xrm.Page.data.entity.attributes.get(attributename);  
  54.     var tab = Xrm.Page.ui.tabs.get(tabname);  
  55.     if (tab === null)  
  56.     {  
  57.         return;  
  58.     }  
  59.     tab.setVisible(attribute.getValue() == value);  
  60. }  
  61.   
  62. function setSectionVisibility(attributename, sectionname, value) {  
  63.     var attribute = Xrm.Page.data.entity.attributes.get(attributename);  
  64.     var tabs = Xrm.Page.ui.tabs.get();  
  65.     for(var i in tabs) {  
  66.         var tab = tabs[i];  
  67.         var section = tab.sections.get(sectionname);  
  68.         if (section !== null) {  
  69.             section.setVisible(attribute.getValue() == value);  
  70.             return;  
  71.         }  
  72.     }  
  73. }  
  74.   
  75. function setControlVisibility(attributename, controlname, value) {  
  76.     var attribute = Xrm.Page.data.entity.attributes.get(attributename);  
  77.     var control = Xrm.Page.ui.controls.get(controlname);  
  78.     if (control === null)  
  79.     {  
  80.         return;  
  81.     }  
  82.     control.setVisible(attribute.getValue() == value);  

你可能感兴趣的:(hidden)