+ Start a Discussion
SBKSBK 

rendering one pageBlockSection at a time.

I have multiple select lists. I want the second select list to be rendered only after the user makes a selection in the first list. How would I make it happen using VisualForce, any examples would be appreciated.

 

Since the second list is populated after the first, how can I communicate between the first selection and the controller. I do not want the contoller methods associated with the second list execute before user action in the first.

 

Thanks. 

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hello SBK;

In your controller you need to include something as marked in green below.

 

public class MyClass{

String country = '';

Boolean isSecondListRendered = false;

 

public List<SelectOption> getCountries() {

List<SelectOption> options = new List<SelectOption>();

options.add(new SelectOption('US','US'));

options.add(new SelectOption('CANADA','Canada'));

options.add(new SelectOption('MEXICO','Mexico'));

return options;

}

 

public String getCountry() {

return country;

}

 

public void setCountry(String country) {

this.isSecondListRendered = true;

this.country = country;

}

 

public Boolean getIsSecondListRendered(){

return isSecondListRendered;

}

}  

 

 In your page you need to use an actionSupport tag:

 

<apex:page controller="MyClass">

<apex:form>

<apex:pageblock>

<apex:outputPanel layout="block" id="list1">

<apex:selectList value="{!country}" size="1">

<apex:selectOptions value="{!items}"/>

<apex:actionSupport event="onchange" rerender="list2"/>

</apex:selectList>

</apex:outputPanel>

<apex:outputPanel layout="block" id="list2">

<apex:selectList rendered="{!isSecondListRendered}" value="{!country}" size="1">

<apex:selectOptions value="{!items}"/>

</apex:selectList>

</apex:outputPanel>

</apex:pageblock>

</apex:form>

</apex:page> 

 

 

 

 

 

 

 

Message Edited by prageeth on 03-04-2010 12:45 AM

All Answers

prageethprageeth

Hello SBK;

In your controller you need to include something as marked in green below.

 

public class MyClass{

String country = '';

Boolean isSecondListRendered = false;

 

public List<SelectOption> getCountries() {

List<SelectOption> options = new List<SelectOption>();

options.add(new SelectOption('US','US'));

options.add(new SelectOption('CANADA','Canada'));

options.add(new SelectOption('MEXICO','Mexico'));

return options;

}

 

public String getCountry() {

return country;

}

 

public void setCountry(String country) {

this.isSecondListRendered = true;

this.country = country;

}

 

public Boolean getIsSecondListRendered(){

return isSecondListRendered;

}

}  

 

 In your page you need to use an actionSupport tag:

 

<apex:page controller="MyClass">

<apex:form>

<apex:pageblock>

<apex:outputPanel layout="block" id="list1">

<apex:selectList value="{!country}" size="1">

<apex:selectOptions value="{!items}"/>

<apex:actionSupport event="onchange" rerender="list2"/>

</apex:selectList>

</apex:outputPanel>

<apex:outputPanel layout="block" id="list2">

<apex:selectList rendered="{!isSecondListRendered}" value="{!country}" size="1">

<apex:selectOptions value="{!items}"/>

</apex:selectList>

</apex:outputPanel>

</apex:pageblock>

</apex:form>

</apex:page> 

 

 

 

 

 

 

 

Message Edited by prageeth on 03-04-2010 12:45 AM
This was selected as the best answer
SBKSBK
Thanks Prageeth