0

I'm having two pick list based on 1st picklist values 2nd picklist must be displayed. I tried in two ways but its not working.

Way 1: using rerendered in VF page

<apex:pageBlockSectionItem >
    <apex:outputLabel value="Item" for="item"/>
    <apex:selectList id="item" value="{!selectedItem}"  size="1">
        <apex:selectOptions value="{!items}"/>
    </apex:selectList>
</apex:pageBlockSectionItem>


<apex:pageBlockSectionItem >
    <apex:outputLabel value="Colum Two" for="Two"/>
    <apex:selectList id="Two" value="{!selectedItem1 }"  rendered="{!selectedItem == 'Math JrBg || Math SrBg|| Math 0a || English SrBg || Abacus Jr Level 2  || Abacus Sr Level 1 || Abacus Jr Level 1 || Abacus Sr Level 2' }" size="1">
        <apex:selectOptions value="{!items1}"/>
    </apex:selectList>
</apex:pageBlockSectionItem>

public List<SelectOption>  getItems()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select an Item--','--Select an Item--'));
        options.add(new SelectOption('Pens - Qty 50' , 'Pens - Qty 50'));
        options.add(new SelectOption('Pencils - Qty 50','Pencils - Qty 50'));
        options.add(new SelectOption('Blue Draw String Bags - Qty 10','Blue Draw String Bags - Qty 10'));
        options.add(new SelectOption('BestBrains Bags-Zip Type - Qty 50','BestBrains Bags-Zip Type - Qty 50'));
        options.add(new SelectOption('$1 Bucks - Qty 500 pcs','$1 Bucks - Qty 500 pcs'));
        options.add(new SelectOption('$10 Bucks - Qty 500 pcs','$10 Bucks - Qty 500 pcs'));  
        options.add(new SelectOption('Progress Books - Qty 20','Progress Books - Qty 20'));
        options.add(new SelectOption('Abacus Tools - Qty 20','Abacus Tools - Qty 20'));  
        options.add(new SelectOption('Master Abacus - Qty 1 ','Master Abacus - Qty 1 '));
        options.add(new SelectOption('Parent Curriculum Charts - Qty 20','Parent Curriculum Charts - Qty 20'));
        options.add(new SelectOption('Owner Curriculum Chart - Qty 1','Owner Curriculum Chart - Qty 1'));
        options.add(new SelectOption('Math Certificates - Qty 20','Math Certificates - Qty 20'));  
        options.add(new SelectOption('English Certificates - Qty 20','English Certificates - Qty 20'));   
        options.add(new SelectOption('Abacus Certificates - Qty 20','Abacus Certificates - Qty 20'));
        options.add(new SelectOption('General Knowledge Certificates - Qty - 20','General Knowledge Certificates - Qty - 20'));
        options.add(new SelectOption('Abacus JR Level 1 Workbook - Qty 10' , 'Abacus JR Level 1 Workbook - Qty 10'));


        options.add(new SelectOption('Math JrBg' , 'Math JrBg'));
        options.add(new SelectOption('Math SrBg','Math SrBg'));
        options.add(new SelectOption('Math 0a','Math 0a'));
        options.add(new SelectOption('English SrBg','English SrBg'));
        options.add(new SelectOption('Abacus Jr Level 1','Abacus Jr Level 1'));
        options.add(new SelectOption('Abacus Jr Level 2','Abacus Jr Level 2'));  
        options.add(new SelectOption('Abacus Sr Level 1','Abacus Sr Level 1'));
        options.add(new SelectOption('Abacus Sr Level 2','Abacus Sr Level 2'));


        return options ;
    }



     public List<SelectOption>  getItems1()
    {

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

        options.add(new SelectOption('A - Qty 5','A - Qty 5'));
        options.add(new SelectOption('B - Qty 5','B - Qty 5'));
        options.add(new SelectOption('C - Qty 5','C - Qty 5'));
        options.add(new SelectOption('D - Qty 5','D - Qty 5'));
        options.add(new SelectOption('E - Qty 5','E - Qty 5'));
        options.add(new SelectOption('F - Qty 5','F - Qty 5'));
        options.add(new SelectOption('G - Qty 5','G - Qty 5'));
        options.add(new SelectOption('H - Qty 5','H - Qty 5'));
        options.add(new SelectOption('I - Qty 5','I - Qty 5'));
        options.add(new SelectOption('J - Qty 5','J - Qty 5'));
        options.add(new SelectOption('K - Qty 5','K - Qty 5'));
        options.add(new SelectOption('L - Qty 5','L - Qty 5'));
        options.add(new SelectOption('M - Qty 5','M - Qty 5'));
        options.add(new SelectOption('N - Qty 5','N - Qty 5'));
        options.add(new SelectOption('O - Qty 5','O - Qty 5'));
        options.add(new SelectOption('P - Qty 5','P - Qty 5'));
        options.add(new SelectOption('Q - Qty 5','Q - Qty 5'));
        options.add(new SelectOption('R - Qty 5','R - Qty 5'));
        options.add(new SelectOption('S - Qty 5','S - Qty 5'));
        options.add(new SelectOption('T - Qty 5','T - Qty 5'));
        options.add(new SelectOption('U - Qty 5','U - Qty 5'));
        options.add(new SelectOption('V - Qty 5','V - Qty 5'));
        options.add(new SelectOption('W - Qty 5','W - Qty 5'));
        options.add(new SelectOption('X - Qty 5','X - Qty 5'));
        options.add(new SelectOption('Y - Qty 5','Y - Qty 5'));
        options.add(new SelectOption('Z - Qty 5','Z - Qty 5'));


        return options ;
    }

Way 2 :: Using IF condition in controller

<apex:pageBlockSectionItem >
    <apex:outputLabel value="Item" for="item"/>
    <apex:selectList id="item" value="{!selectedItem }"  size="1">
        <apex:selectOptions value="{!items}"/>
    </apex:selectList>
</apex:pageBlockSectionItem>


<apex:pageBlockSectionItem >
    <apex:outputLabel value="Colum Two" for="Two"/>
    <apex:selectList id="Two" value="{!selectedItem1 }" size="1">
        <apex:selectOptions value="{!items1}"/>
    </apex:selectList>
</apex:pageBlockSectionItem>

public List<SelectOption>  getItems()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select an Item--','--Select an Item--'));
        options.add(new SelectOption('Pens - Qty 50' , 'Pens - Qty 50'));
        options.add(new SelectOption('Pencils - Qty 50','Pencils - Qty 50'));
        options.add(new SelectOption('Blue Draw String Bags - Qty 10','Blue Draw String Bags - Qty 10'));
        options.add(new SelectOption('BestBrains Bags-Zip Type - Qty 50','BestBrains Bags-Zip Type - Qty 50'));
        options.add(new SelectOption('$1 Bucks - Qty 500 pcs','$1 Bucks - Qty 500 pcs'));
        options.add(new SelectOption('$10 Bucks - Qty 500 pcs','$10 Bucks - Qty 500 pcs'));  
        options.add(new SelectOption('Progress Books - Qty 20','Progress Books - Qty 20'));
        options.add(new SelectOption('Abacus Tools - Qty 20','Abacus Tools - Qty 20'));  
        options.add(new SelectOption('Master Abacus - Qty 1 ','Master Abacus - Qty 1 '));
        options.add(new SelectOption('Parent Curriculum Charts - Qty 20','Parent Curriculum Charts - Qty 20'));
        options.add(new SelectOption('Owner Curriculum Chart - Qty 1','Owner Curriculum Chart - Qty 1'));
        options.add(new SelectOption('Math Certificates - Qty 20','Math Certificates - Qty 20'));  
        options.add(new SelectOption('English Certificates - Qty 20','English Certificates - Qty 20'));   
        options.add(new SelectOption('Abacus Certificates - Qty 20','Abacus Certificates - Qty 20'));
        options.add(new SelectOption('General Knowledge Certificates - Qty - 20','General Knowledge Certificates - Qty - 20'));
        options.add(new SelectOption('Abacus JR Level 1 Workbook - Qty 10' , 'Abacus JR Level 1 Workbook - Qty 10'));


        options.add(new SelectOption('Math JrBg' , 'Math JrBg'));
        options.add(new SelectOption('Math SrBg','Math SrBg'));
        options.add(new SelectOption('Math 0a','Math 0a'));
        options.add(new SelectOption('English SrBg','English SrBg'));
        options.add(new SelectOption('Abacus Jr Level 1','Abacus Jr Level 1'));
        options.add(new SelectOption('Abacus Jr Level 2','Abacus Jr Level 2'));  
        options.add(new SelectOption('Abacus Sr Level 1','Abacus Sr Level 1'));
        options.add(new SelectOption('Abacus Sr Level 2','Abacus Sr Level 2'));


        return options ;
    }

public List<SelectOption>  getItems1() 
    {

        List<SelectOption> options = new List<SelectOption>();
        if(selectedItem == 'Abacus Sr Level 2')
        options.add(new SelectOption('A - Qty 5','A - Qty 5'));
        options.add(new SelectOption('B - Qty 5','B - Qty 5'));
        options.add(new SelectOption('C - Qty 5','C - Qty 5'));
        options.add(new SelectOption('D - Qty 5','D - Qty 5'));
        options.add(new SelectOption('E - Qty 5','E - Qty 5'));
        options.add(new SelectOption('F - Qty 5','F - Qty 5'));
        options.add(new SelectOption('G - Qty 5','G - Qty 5'));
        options.add(new SelectOption('H - Qty 5','H - Qty 5'));
        options.add(new SelectOption('I - Qty 5','I - Qty 5'));
        options.add(new SelectOption('J - Qty 5','J - Qty 5'));
        options.add(new SelectOption('K - Qty 5','K - Qty 5'));
        options.add(new SelectOption('L - Qty 5','L - Qty 5'));
        options.add(new SelectOption('M - Qty 5','M - Qty 5'));
        options.add(new SelectOption('N - Qty 5','N - Qty 5'));
        options.add(new SelectOption('O - Qty 5','O - Qty 5'));
        options.add(new SelectOption('P - Qty 5','P - Qty 5'));
        options.add(new SelectOption('Q - Qty 5','Q - Qty 5'));
        options.add(new SelectOption('R - Qty 5','R - Qty 5'));
        options.add(new SelectOption('S - Qty 5','S - Qty 5'));
        options.add(new SelectOption('T - Qty 5','T - Qty 5'));
        options.add(new SelectOption('U - Qty 5','U - Qty 5'));
        options.add(new SelectOption('V - Qty 5','V - Qty 5'));
        options.add(new SelectOption('W - Qty 5','W - Qty 5'));
        options.add(new SelectOption('X - Qty 5','X - Qty 5'));
        options.add(new SelectOption('Y - Qty 5','Y - Qty 5'));
        options.add(new SelectOption('Z - Qty 5','Z - Qty 5'));


        return options ;
    }
  • Hi kiran, welcome to SFSE! Please take a moment to visit the help center and read How to Ask. Your question is not very clear. Please edit your post to be more explicit about what you mean by it's not working. – Adrian Larson Oct 19 '16 at 14:51
  • Hi Larson, I'm having two pick lists, in the 1st pick list for some values only 2nd picklist values must be enabled. – kiran Oct 19 '16 at 14:57
  • I tried rerendered func, IF condition both are not working – kiran Oct 19 '16 at 14:58
1

You never rerender anything. You should add an <apex:actionSupport> directly under your <apex:selectList> tag (not your <apex:selectOptions> tag). Something like the below works just fine:

Markup

<apex:page controller="RerenderDemo">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection id="section">

                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Option 1" />
                    <apex:selectList value="{!selection1}" size="1">
                        <apex:selectOptions value="{!options1}" />
                        <apex:actionSupport event="onchange" rerender="section" />
                        <!-- CRITICAL TAG ABOVE -->
                    </apex:selectList>
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem rendered="{!shouldDisplay}">
                    <apex:outputLabel value="Option 2" />
                    <apex:selectList value="{!selection2}" size="1">
                        <apex:selectOptions value="{!options2}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>

            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public with sharing class Throwaway
{
    public List<SelectOption> options1 { get; private set; }
    public List<SelectOption> options2 { get; private set; }

    public String selection1 { get; set; }
    public String selection2 { get; set; }

    public Boolean shouldDisplay { get { system.debug(selection1); return whitelist.contains(selection1); } }

    final Set<String> whitelist = new Set<String> { 'a', 'b', 'c' };
    public Throwaway()
    {
        options1 = new List<SelectOption>
        {
            new SelectOption('a', 'a'),
            new SelectOption('b', 'b'),
            new SelectOption('c', 'c'),
            new SelectOption('d', 'd'),
            new SelectOption('e', 'e'),
            new SelectOption('f', 'f')
        };
        options2 = new List<SelectOption>
        {
            new SelectOption('x', 'x'),
            new SelectOption('y', 'y'),
            new SelectOption('z', 'z')
        };
        selection1 = 'a';
    }
}
  • Is this revenge now @AdrianLarson?! Beat me to this one in 7 seconds! :D – Dan Jones Oct 19 '16 at 15:29
  • Hahaha. Didn't realize you were working on it. :) Does it actually work if you rerender the element itself though? The renderer only actually rerenders child elements of whatever you specify. Hmm. – Adrian Larson Oct 19 '16 at 15:33
  • Looks that way. I didn't copy his code but I don't know if you can see deleted answers, but I just made the worlds most basic select option changer and basically said he was missing an <apex:actionSupport event="onchange" reRender="theIdOfTheRecord" /> in the first list. – Dan Jones Oct 19 '16 at 15:40
  • Right, I am referencing your deleted answer. I see that you rerendered the second selectList, but I would expect that not to work. I would think you'd have to rerender the form instead. – Adrian Larson Oct 19 '16 at 15:41
  • Give it a try! I didn't know that it only reRendered children, but I suppose from a pure HTML point of view, <option> is a child of <select> so it would make sense as to why that way works. – Dan Jones Oct 19 '16 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.