Thursday, December 1, 2016

Dependent Picklist Creation in VisualForce

In this scenario we can see how to generate a picklist dynamically and preparation of dependant picklists.
Here dependant picklist  will be populated using apex:actionsupport.

Example: VisualForce Page To display Contacts based on the accounts selection.


Page:


<apex:page sidebar="false" controller="DependentClassabcd">
<apex:form >
Accounts : <apex:selectList value="{!SelectedAccount}" multiselect="false" size="1">
                <apex:actionSupport event="onchange" reRender="out"/>
                <apex:selectOptions value="{!Accounts}">
                </apex:selectOptions>
            </apex:selectList> <br/>
Contacts : <apex:selectList multiselect="false" id="out" size="1">
                <apex:selectOptions value="{!Contacts}">
                </apex:selectOptions>
            </apex:selectList>
</apex:form>   
</apex:page>

Controller:


public with sharing class DependentClassabcd {

public string selectedaccount {get;set;}
List<selectOption> Cons = new List<selectOption>();
List<selectOption> Accs = new List<selectOption>();

    public List<selectOption> getContacts() {
        Cons.clear();
        Cons.add(new selectOption('–None–','–None–'));
        for(contact objEmp : [select Id,name,accountid from contact where accountid=:selectedaccount and accountid!=null order by name asc ]){
            Cons .add(new selectOption(objEmp.name,objEmp.name));
        }
        return Cons;
    }

    public List<selectOption> getAccounts() {
        Accs.add(new selectOption('–None–','–None–'));
        for(account objDept : [select id,name from account order by Name Asc]){
            Accs.add(new selectOption(objDept.id,objDept.name));
        }
        return Accs;
    }
}



No comments:

Post a Comment