function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lovecrmlovecrm 

Hiding the section based on Picklist value

Hi,

 

 I am having a Picklist with 3 options: None, Commercial and Rolloff.

 

If None is selected then the below two sections in VF page should be hidden.

If commercial/Roll Off is selected then the below 2 sections should be shown to the user to enter the data.

 

Anyone please help me doing this.

 

Appreciate your help.

 

Thankyou,

Carol

Best Answer chosen by Admin (Salesforce Developers) 
SSRS2SSRS2

Dear Carol

 Can you check with this sample code whether you met the requirement?

  VF Page:

<apex:page controller="sampleCon">
  <apex:form>
    <apex:selectList value="{!country}" multiselect="false" size="1">
     <apex:selectOptions value="{!items}"/>
     <apex:actionSupport event="onchange" rerender="out1,out2"/>
   </apex:selectList>

    <apex:pageBlock >  
      <apex:pageBlockSection id="out1" >
        <apex:pageBlockSectionItem rendered="{!IF(country == 'None', false , true)}">
          Customize your input Text1 <apex:inputtext></apex:inputtext>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection id="out2" >
        <apex:pageBlockSectionItem rendered="{!IF(country == 'None', false , true)}">
          Customize your input Text2 <apex:inputtext></apex:inputtext>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
     </apex:pageBlock>
  </apex:form>
</apex:page>

 Apex code:

 

    public class sampleCon {
        String country = 'None';
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('None','None'));
            options.add(new SelectOption('Commercial','Commercial'));
            options.add(new SelectOption('Rolloff','Rolloff'));
            return options;
        }
            
        public String getCountry() {
            return country;
        }
            
        public void setCountry(String country) {
            this.country = country;
        }
    }

 

  -Suresh

 

 

All Answers

ImaghrooriImaghroori

This sounds like a great use for the standard Record Types, Page Layouts, and Workflow.

 

When the field changes, have a workflow rule fire. The workflow will perform a field update.  It will update the Record Type field.

 

When the record type is changed, a new page layout will be displayed.

 

This new page layout has whatever fields / sections you want on it.

 

This will work great for simple 3 choices, but doesn't scale well.

 

If you need the potential to add MANY choices, then you may be looking at visualforce.

Pradeep_NavatarPradeep_Navatar

You can do this with the help of javascript. You can invoke the javascript function for onchange() event and hide and display the section on the basis of your requirements.

 

Go through the sample code given below :

 

        <apex:selectList value="{!ListProfile}"  size="1"  onchange="FetchData();" >

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

            </apex:selectList>

                                                function fetchdata()

                                                {

                                                                if(this.options[this.selectedIndex].text == 'None')

                                                                {

                                                                document.getElementById('id').style.display = 'none';// Does not show the section.

                                                                }

                                                                else

                                                                {

                                                                 document.getElementById('id').style.display = 'block';

                                                                }

                                                }

 

Hope this helps.

SSRS2SSRS2

Dear Carol

 Can you check with this sample code whether you met the requirement?

  VF Page:

<apex:page controller="sampleCon">
  <apex:form>
    <apex:selectList value="{!country}" multiselect="false" size="1">
     <apex:selectOptions value="{!items}"/>
     <apex:actionSupport event="onchange" rerender="out1,out2"/>
   </apex:selectList>

    <apex:pageBlock >  
      <apex:pageBlockSection id="out1" >
        <apex:pageBlockSectionItem rendered="{!IF(country == 'None', false , true)}">
          Customize your input Text1 <apex:inputtext></apex:inputtext>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection id="out2" >
        <apex:pageBlockSectionItem rendered="{!IF(country == 'None', false , true)}">
          Customize your input Text2 <apex:inputtext></apex:inputtext>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
     </apex:pageBlock>
  </apex:form>
</apex:page>

 Apex code:

 

    public class sampleCon {
        String country = 'None';
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('None','None'));
            options.add(new SelectOption('Commercial','Commercial'));
            options.add(new SelectOption('Rolloff','Rolloff'));
            return options;
        }
            
        public String getCountry() {
            return country;
        }
            
        public void setCountry(String country) {
            this.country = country;
        }
    }

 

  -Suresh

 

 

This was selected as the best answer
lovecrmlovecrm

Thankyou Suresh!

 

It worked well for me.

 

Thankyou,

carol

skjaiswalskjaiswal

Hi

 

I have three picklist values: A,B,and C.

Based on the value selected, i want to display different blocksections.

 

I tried to do it, but failed.