[SalesForce] Visualforce page with picklist dependent on multiple checkbox fields

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

Best Answer

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;
    }   

}
Related Topic