You need to sign in to do that
Don't have an account?
mriz
VisualForce + jQuery: How to add a selectList on button Click?
I want to add multiple SelectLists in new row on each button click using jQuery.
Following code is not working for me
VF Code:
Apex Class
I have even tried this variation, but with no success:
Following code is not working for me
VF Code:
<apex:page controller="TestClass" id="pg" sidebar="false" showHeader="false" doctype="html-5.0" standardStylesheets="false"> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1.0"/> <style> body{font-family:Arial,Sans;} a{text-decoration:none;} </style> </head> <body> <div> <apex:form > <apex:selectList size="1" multiselect="false" value="{!str}"> <apex:selectoptions value="{!Roles}"/> </apex:selectList><br/> </apex:form> <button id="addRow" type="button">Add Row</button> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("#addRow").click(function(){ // alert('hi'); $("#addRow").before('<apex:form ><apex:selectList value="{!str}"><apex:selectOptions value="{!Roles}"/></apex:SelectList></apex:form>'); }); }); </script> </body> </apex:page>
Apex Class
public class TestClass{ public string str{get;set;} public TestClass(){ } public List<selectOption> getRoles(){ List<SelectOption> myList=new List<selectOption>(); myList.add(new SelectOption('0','--Select--')); myList.add(new SelectOption('val1','Val1')); myList.add(new SelectOption('val2','Val2')); myList.add(new SelectOption('val3','Val3')); return myList; } }
I have even tried this variation, but with no success:
<apex:page controller="TestClass" id="pg" sidebar="false" showHeader="false" doctype="html-5.0" standardStylesheets="false"> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1.0"/> <style> body{font-family:Arial,Sans;} a{text-decoration:none;} </style> </head> <body> <div> <apex:form > <apex:selectList size="1" multiselect="false" value="{!str}"> <apex:selectoptions value="{!Roles}"/> </apex:selectList><br/> <!-- changed part --> <button id="addRow" type="button">Add Row</button> </apex:form> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("#addRow").click(function(){ // alert('hi'); $("#addRow").before('<apex:selectList value="{!str}"><apex:selectOptions value="{!Roles}"/></apex:SelectList>'); }); }); </script> </body> </apex:page>
Not sure how we can embed the vf tags using jquery in page. But, I am able to do this by embedding native HTML. This is how I modified your code:
There is not a way to pass visualforce components into the page with jquery. If you absolutely need jQuery to do the task, you will have to write out the html code that Pankaj provided.
There is another option that you can try. Apex has a Component class that allows for dynamic Visualforce. Check out this post https://developer.salesforce.com/forums/?id=906F000000097ZjIAI. It has code for dynamic visualforce selectLists. Your page button can call a method in your constructor to add the selectList.
Hope that helps!
The problem with this approach is, that i wont get all values of the SelectList that are fetched from Apex. This becomes more or less like static selectList with an equivalent HTML.
Actually my end goal is to populate SelectList from Custom Objects using SOQL queries. For the sake of posting i added four static values in Apex.