Monday, March 11, 2013

Usage of "ActionFunction" and "SelectList" apex tag in Visualforce Page


Visualforce Page:

<apex:page controller="selectlistcls">
 <apex:form >
 <apex:actionFunction name="change" action="{!showselected}"/>
    <apex:selectList value="{!selected}" size="1" onchange="change()">
        <apex:selectOptions value="{!items}"/>
    </apex:selectList>
    <apex:selectList value="{!selectedcon}" size="1" disabled="{!display}">
        <apex:selectOptions value="{!contactitems}"/>
    </apex:selectList>
 </apex:form>
</apex:page>

Apex Class:

public with sharing class selectlistcls {
    public boolean display { get; set; }
    public selectlistcls (){
        display= true;
                                  }
    public List<SelectOption> getContactitems() {
       return contoptions;
                                                                         }
       List<SelectOption> contoptions= new List<SelectOption>();
    public String selectedcon { get; set; }
    public PageReference showselected() {
       System.debug('------------>'+selected);
       contoptions.clear();
       display=false;
       List<Contact> lstcon=[Select id,name from Contact where accountid=:selected];
       for(Contact c:lstcon){
       contoptions.add(new selectOption(c.id,c.name));
                                       }
       return null;
                                                              }
    public List<SelectOption> getitems(){
        List<Selectoption> options= new List<SelectOption>();
        options.add(new selectOption('','--Please select one Account--'));
        List<Account> lst=[select id, name from Account];
        for(Account a:lst){
        options.add(new selectOption(a.id,a.name));
                                   }
        return options;
                                                            }
    public String selected { get; set; }
                                                                }

No comments:

Post a Comment