Wednesday, January 4, 2017

How to add pick list field values dynamically in visual force

If we want to add picklist values at run time in visual force it can be done very easily by using <apex:selectOptions> tag .Their might be a situation where you need to go with html 'select ' tag to form the picklist. In this situations if you want add values to it run time their is no in built attributes as we follows in visual force tags.To achieve this we need to use some scripting .In this example I'm going to show with the help of jQuery.

Sample code snippet:

 <apex:includeScript value="//"/>
  <apex:form id="formId">
   <select id="dropDownId">
   <apex:commandButton value="Update" onclick="addPicklistValues('dropDownId');return false;" rerender="formId"/>

  function addPicklistValues(dropDownId )
  var dropId = '#'+dropDownId;
  $(dropId).find('option').remove().end().append('<option value="-None-">--None--</option>','<option value="Approved">Approved</option>','<option value="Rejected">Rejected</option>');

Thanks for visiting..hope this helps you!

No comments:

Post a Comment