Friday, March 15, 2013

Select Radio and ActionStatus


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

Usage of Edit, Delete, Next and Previous links with ActionStatus


Visualforce Page:

<apex:page standardController="Account" Extensions="nextcls" recordSetVar="accs" >
     <apex:form >
         <Apex:pageBlock id="pb" >
         
             <apex:pageBlockTable value="{!accs}" var="a">
                 <apex:column headerValue="Action" width="100">
                     <apex:commandLink value="Edit" action="{!editrec}" style="color:#015BA7">
                         <apex:param value="{!a.id}" assignTo="{!recordid}" name="p"/>
                     </apex:commandLink>
                     &nbsp;|&nbsp;
                     <apex:commandLink value="Del" action="{!delrec}" style="color:#015BA7">
                         <apex:param value="{!a.id}" assignTo="{!recordid}" name="p1"/>
                     </apex:commandLink>
                 </apex:column>
                 <apex:column headerValue="Account Name" width="300" >
                     <apex:commandLink value="{!a.name}" rerender="out" status="stat">
                         <apex:param value="{!a.id}" name="p2"/>
                     </apex:commandLink>
                 </apex:column>
                 <apex:column value="{!a.phone}"/>
                 <apex:column value="{!a.fax}"/>             
             </apex:pageBlockTable>
             
             <apex:commandLink action="{!previous}" reRender="pb">
                 <apex:image value="{!$Resource.prev}" style="height:25px; width:25px:"/>
             </apex:commandLink>
             &nbsp;&nbsp;
             <apex:commandLink action="{!NEXT}" reRender="pb">
             <apex:image value="{!$Resource.next}" style="height:25px; width:25px:"/>
             </apex:commandLink>
         </Apex:pageBlock>
         <div align="center">
         <apex:outputPanel id="out">
         <apex:actionStatus id="stat" >
             <apex:facet name="start">
                  <apex:image value="{!$Resource.Loading}" style="width:250px; height:200px"/>
             </apex:facet>
             <apex:facet name="stop">
                   <apex:detail subject="{!$CurrentPage.Parameters.p2}" relatedList="false" inlineEdit="true"/>
             </apex:facet>
         </apex:actionStatus>
       
         </apex:outputPanel>
         </div>
     </apex:form>
</apex:page>

Apex Class:

public with sharing class nextcls {
    
    public string recordid{get; set;}
    public nextcls(ApexPages.StandardSetController controller) {
        controller.setPagesize(5);
                                                                                                }
    
    public pagereference editrec(){
        System.debug('.......................'+recordid);
        pagereference ref = new pagereference('/'+recordid+'/e');
        return ref;
                                                 }
    
    public pagereference delrec(){
         System.debug('.......................'+recordid);
         Account acc =[select id from Account where id=:recordid];
         delete acc;
         pagereference ref= new pagereference('/apex/records2');
         ref.setredirect(true);
        return ref;
                                                } 
}