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:page>
 <apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js"/>
  <apex:form id="formId">
    <apex:pageblock>
   <select id="dropDownId">
   <apex:commandButton value="Update" onclick="addPicklistValues('dropDownId');return false;" rerender="formId"/>
 </apex:pageblock>
  </apex:form>

<script>
  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>');
              
  }
</script>
</apex:page>

Thanks for visiting..hope this helps you!

No comments:

Post a Comment