How to remove and add options in Choice List?

iyyappan_3
Giga Contributor

Hi,

Apart from classic way using client script g_form.removeOption('') and g_form.addOption(''), is there any best practice add and remove options in Choice List?

Thanks

Iyyappan

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi Iyyappan,



Although as the whole thread says...addOption, clearOptions and removeOption are the best way to go and i totally agree to that. As far as performance is concerned these functions don't have any noticeable impact. These are service now methods to browser dependency and upgrade support is also there OOB.



Still if you want another option.. here is a code by Harish Murikinati ...This code doesn't remove the option but disable it, options will be there but user wont be able to select them.



disableOption();




function disableOption()


{


    var fieldName = 'u_status';


      var control = g_form.getControl(fieldName);


      if (control && !control.options)


          {


                      var name = 'sys_select.' + this.tableName + '.' + fieldName;


                      control = gel(name);


             


          }


          if (!control)


          return;




          if (!control.options)


          return;




          var options = control.options;



        for (var i = 1; i < options.length; i++)


        {


         


              var option = options[i];


              alert('hello'+ options[i].value);


            if(g_user.hasRole('itil') && (options[i].value == "Draft" || options[i].value == 'Review' || options[i].value == 'Closed'))


          {


              control.options[i].disabled = 'true';


            }


     


          }


}



Check this thread for more information



Lock down change states for users with change_management role


-Anurag

View solution in original post

7 REPLIES 7

addOption and removeOption are client side code .. If you are not querying some table and populating the values , dont think there is a performance impact as such


Thanks Kalai


Anurag Tripathi
Mega Patron
Mega Patron

Hi Iyyappan,



Although as the whole thread says...addOption, clearOptions and removeOption are the best way to go and i totally agree to that. As far as performance is concerned these functions don't have any noticeable impact. These are service now methods to browser dependency and upgrade support is also there OOB.



Still if you want another option.. here is a code by Harish Murikinati ...This code doesn't remove the option but disable it, options will be there but user wont be able to select them.



disableOption();




function disableOption()


{


    var fieldName = 'u_status';


      var control = g_form.getControl(fieldName);


      if (control && !control.options)


          {


                      var name = 'sys_select.' + this.tableName + '.' + fieldName;


                      control = gel(name);


             


          }


          if (!control)


          return;




          if (!control.options)


          return;




          var options = control.options;



        for (var i = 1; i < options.length; i++)


        {


         


              var option = options[i];


              alert('hello'+ options[i].value);


            if(g_user.hasRole('itil') && (options[i].value == "Draft" || options[i].value == 'Review' || options[i].value == 'Closed'))


          {


              control.options[i].disabled = 'true';


            }


     


          }


}



Check this thread for more information



Lock down change states for users with change_management role


-Anurag