1

I am working on creating a visualforce page (edit page), in which there is a picklist field. The values that are available within this picklist field should be dependent on whether or not a number of checkbox fields are selected.

Example:
If checkbox A is selected, values 6, 7, 8, and 9 should be available in the picklist
If checkbox B + C are both selected, values 1, 2, and 3 should be available
If checkbox B + D are both selected, value 4 should be available
If all checkboxes are selected, all values should be available

The checkbox fields already have actionsupport - onchange enabled to control visibility on the page for some other fields by rerendering the page.

Would anyone be able to help me by providing some sample code that would enable the above functionality? So far, my page is only using the standard controller for the object.

Thanks in advance, Aaron

2 Answers 2

1

This should get you started:

Learn from here how to work on custom picklist. http://www.infallibletechie.com/2012/10/dependent-picklist-using-apex-in.html

Modified the picklist to checkboxes. change the code below to your requirement.

Page:

<apex:page controller="sample">

    <apex:form >

    <apex:pageBlock >
        <apex:pageBlockSection columns="1">        
            <apex:pageblockSectionitem >                
                Check 1: 
                <apex:inputCheckbox value="{!check1}">
                    <apex:actionSupport event="onchange" reRender="a"/>   
                </apex:inputCheckbox>  
             </apex:pageblockSectionitem> 
             <apex:pageblockSectionitem >  
             Check 2:   
                <apex:inputCheckbox value="{!check2}">
                    <apex:actionSupport event="onchange" reRender="a"/>   
                </apex:inputCheckbox>  
             </apex:pageblockSectionitem> 
             <apex:pageblockSectionitem > 
             Check 3:
                <apex:inputCheckbox value="{!check3}">
                    <apex:actionSupport event="onchange" reRender="a"/>   
                </apex:inputCheckbox>     
            </apex:pageblockSectionitem>           
            <apex:pageblockSectionItem >
                <apex:outputLabel value="City"/>
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>            
        </apex:pageBlockSection>        
    </apex:pageBlock>

    </apex:form>

</apex:page>

Controller:

public class sample
{
    public boolean check1 {get;set;}
    public boolean check2 {get;set;}
    public boolean check3 {get;set;}    
    public String  city {get;set;}

    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(check1 == TRUE)
        {       
            options.add(new SelectOption('check1opt1','check1options1'));
            options.add(new SelectOption('check1opt2','check1options2'));
            if(check2 == TRUE)
            {       
                options.add(new SelectOption('check2opt1','check2options1'));
                options.add(new SelectOption('check2opt2','check2options2'));
            }
        }
        else if(check2 == TRUE)
        {       
            options.add(new SelectOption('check2opt1','check2options1'));
            options.add(new SelectOption('check2opt2','check2options2'));
            if(check1 == TRUE)
            {       
                options.add(new SelectOption('check1opt1','check2options1'));
                options.add(new SelectOption('check1opt2','check2options2'));
            }
        }
        else if(check3 == TRUE)
        {       
            options.add(new SelectOption('check3opt1','check3options1'));
            options.add(new SelectOption('check3opt2','check3options2'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }   

}
1

You need to create a controller that manages the data for the needed checkboxes and a dynamic selectOption list. You can then use action regions on the checkbox to tell the system to populate the picklist on change.

Hopefully the following code helps illustrate.

Controller:

    public List<selectOption> pickList {get;set;}
    public Boolean checkboxA {get;set;}
    public Boolean checkboxB {get;set;}
    public Boolean checkboxC {get;set;}

    public void populatePicklist()
    {
        Case caseRec = (Case)stdCtrl.getRecord();

        pickList= new List<selectOption>();
        pickList.add(new selectOption('','-None-'));

        if(checkboxA)
        {
            pickList.add(new selectOption('','6'));
            pickList.add(new selectOption('','7'));
            pickList.add(new selectOption('','8'));
            pickList.add(new selectOption('','9'));
        }
        else if(checkboxB && checkboxC)
        {
            pickList.add(new selectOption('','1'));
            pickList.add(new selectOption('','2'));
            pickList.add(new selectOption('','3'));
        }    
    }

Visualforce:

<apex:page standardController="Case" tabStyle="Case" extensions="CaseTechnicalSupportExt">
    <apex:form >
        <apex:sectionHeader title="Case Edit" subtitle="{!if(Case.Id==null,'New Case',Case.CaseNumber)}"/>
        <apex:pageBlock mode="edit" id="casePB" title="Case Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageMessages id="messages"/>
            <apex:pageBlockSection id="CaseInformationPBS" title="Case Information">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="CheckboxA" for="caseCheckboxA"/>
                    <apex:actionRegion >
                        <apex:inputField value="{!Case.checkboxA}" rendered="true" id="caseCheckboxA">
                            <apex:actionSupport event="onchange" action="{!populatePicklist}" reRender="messages,pickListSelectList"/>
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="CheckboxB" for="caseCheckboxB"/>
                    <apex:actionRegion >
                        <apex:inputField value="{!Case.checkboxA}" rendered="true" id="caseCheckboxB">
                            <apex:actionSupport event="onchange" action="{!populatePicklist}" reRender="messages,pickListSelectList"/>
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="CheckboxC" for="caseCheckboxC"/>
                    <apex:actionRegion >
                        <apex:inputField value="{!Case.checkboxA}" rendered="true" id="caseCheckboxC">
                            <apex:actionSupport event="onchange" action="{!populatePicklist}" reRender="messages,pickListSelectList"/>
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="PickList" for="pickLIstSelectList"/>
                    <apex:selectList value="{!pickList}" multiselect="false" size="1" id="pickListSelectList">
                        <apex:selectOptions value="{!pickList}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .