You need to sign in to do that
Don't have an account?
Rerender dependent pick list from the controlling picklist
Hi All,
I want to rerender a dependent picklist as per the value of a master picklist, I am facing a strange behavior here. On re-render all the stuff other than the dependent picklist is updates like text field.
Here is the code, on change of "category"picklist I want to rerender "categoryValue". I tried using the Id of dependent picklist but thats not working so, I am rerendering the complete form.
<apex:page controller="RerenderController">
<h1>Select a Category</h1>
<apex:form id="theForm">
<apex:actionFunction name="onCategoryChange" action="{!onCategoryChange}" reRender="theForm" />
<apex:selectList value="{!category}" size="1" onchange="onCategoryChange()">
<apex:selectoptions value="{!categories}"/>
</apex:selectList>
<apex:selectList value="{!categoryValue}" size="1">
<apex:selectoptions value="{!categoryValues}"/>
</apex:selectList>
<apex:outputText value="{!categoryValuesAsCSV}" />
</apex:form>
</apex:page>
public class RerenderController {
private static Map<String, List<SelectOption>> catValueMap = new Map<String, List<SelectOption>> ();
static {
List <Selectoption> fruits = new List<SelectOption>();
fruits.add(new SelectOption('Apple','Apple'));
fruits.add(new SelectOption('Grape', 'Grape'));
fruits.add(new SelectOption('Banana','Banana'));
fruits.add(new SelectOption('Orange','Orange'));
catValueMap.put('Fruits', fruits);
List <Selectoption> animals = new List<SelectOption>();
animals.add(new SelectOption('Dog','Dog'));
animals.add(new SelectOption('Cat', 'Cat'));
animals.add(new SelectOption('Monkey','Monkey'));
animals.add(new SelectOption('Donkey','Donkey'));
catValueMap.put('Animals', animals);
List <Selectoption> computers = new List<SelectOption>();
computers.add(new SelectOption('Dell','Dell'));
computers.add(new SelectOption('Compaq', 'Compaq'));
computers.add(new SelectOption('Acer','Acer'));
computers.add(new SelectOption('Apple','Apple'));
catValueMap.put('Computers', computers);
}
public List<SelectOption> categories {get ; set;}
public String category {get ; set;}
public List<SelectOption> categoryValues {get ; set;}
public String categoryValue {get ; set;}
{
categories = new List<Selectoption>();
categories.add(new Selectoption('Fruits', 'Fruits'));
categories.add(new Selectoption('Animals', 'Animals'));
categories.add(new Selectoption('Computers','Computers'));
}
public String getCategoryValuesAsCSV () {
String csv = '';
if (categoryValues != null) {
for (Selectoption so: categoryValues ){
csv += so.getValue() + ' , ';
}
}
return csv;
}
public void onCategoryChange() {
categoryValues = catValueMap.get(category);
}
}
I tried searching on the boards, but people are saying this problem can be because of required fields in the form. But my form is not having any such data.
Please help.
Make a small change in your controller code and it should work:
{
categories = new List<Selectoption>();
categories.add(new Selectoption('Fruits', 'Fruits'));
categories.add(new Selectoption('Animals', 'Animals'));
categories.add(new Selectoption('Computers','Computers'));
// Notice this change here
categoryValues = New List<SelectOption>();
// Notice this change here
}
AD
-------------------------------------------------------------------------------
Mark it as an accepted solution if it works and help the community
All Answers
Make a small change in your controller code and it should work:
{
categories = new List<Selectoption>();
categories.add(new Selectoption('Fruits', 'Fruits'));
categories.add(new Selectoption('Animals', 'Animals'));
categories.add(new Selectoption('Computers','Computers'));
// Notice this change here
categoryValues = New List<SelectOption>();
// Notice this change here
}
AD
-------------------------------------------------------------------------------
Mark it as an accepted solution if it works and help the community
Thanks Man,
This is the correct solution to the problem.
But I am really pissed the way salesforce is handling NULL vs Empty Lists.