Visualforce Page:
<apex:page controller="picklistcls" >
<apex:form >
<apex:actionFunction name="getcon" action="{!show}" rerender="out" status="stat"/>
<apex:Selectradio value="{!Selected}" layout="pagedirection" onchange="getcon()">
<apex:selectOptions value="{!items}"/>
</apex:Selectradio>
<apex:outputpanel id="out">
<apex:actionstatus id="stat">
<apex:facet name="start">
<apex:image value="{!$Resource.Loading}"/>
</apex:facet>
<apex:facet name="stop">
<apex:selectList value="{!Contactselected}" size="1" disabled="{!bool}">
<apex:selectOptions value="{!conitems}"/>
</apex:selectList>
</apex:facet>
</apex:actionstatus>
</apex:outputpanel>
</apex:form>
</apex:page>
Apex Class:
public with sharing class picklistcls {
public string selected{get; set;}
public List<SelectOption> items{get; set;}
public boolean bool{get; set;}
public string contactselected{get; set;}
public List<SelectOption> conitems{get; set;}
public picklistcls(){
bool=true;
items= new List<SelectOption>();
/*items.add(new SelectOption('1','a'));
items.add(new SelectOption('2','b'));
items.add(new SelectOption('3','c'));
items.add(new SelectOption('4','d'));
*/
List<Account> lst= [Select id, name from Account limit 20];
items.add(new SelectOption('','--Select An Account--'));
for(account a:lst){
items.add(new SelectOption(a.id,a.name));
}
}
public void show(){
bool=false;
System.debug('--------------'+selected);
List<Contact> lstcon =[Select id, name from Contact where accountid=:selected];
conitems= new List<SelectOption>();
if(lstcon.size()>0){
for(Contact c:lstcon){
conitems.add(new Selectoption(c.id, c.name));
}
}
else{
conitems.add(new Selectoption('','--No Contacts--'));
}
}
}
Test Class for above Apex Class:
@isTest
private class testinsertcls {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Training__c t = new Training__c();
t.name='Salesforce';
insert t;
Batch__c b = new Batch__c();
b.training__c = t.id;
insert b;
Student__c s = new Student__c();
s.Batch__c = b.id;
s.name='Test';
insert s;
insertcls obj = new insertcls(new ApexPages.StandardController(s));
obj.stud = new Student__c();
obj.stud.batch__c = b.id;
obj.stud.name='Test1';
obj.saverec();
}
}
No comments:
Post a Comment