Wednesday, June 6, 2012

SELECTED CHECK BOX:

SELECTED CHECK BOX:
This example shows how to render selected records using check box also gives flexibility of finding records alphabetically.

public with sharing class Pagin_alpha {


     
    private List<contactSet> contactSetList{get;set;}
    private string contactListQuery;
    private set<contact> selectedContact;
    
    public List<string> alphabet{get;set;}
    public boolean fals{get;set;}   
    
    public Pagin_alpha(){
      fals=false;
      alphabet=new string[]{'All','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };  
      contactSetList = new List<contactSet>();
      selectedContact = new set<contact>();
      contactListQuery= 'select id,name,phone from contact limit 1000';    
    }
    
     Private void updateSelectedContact(){
        for(contactSet cs:contactSetList ){
           if(cs.checked)
               selectedcontact.add(cs.ocontact);
           else{
               if(selectedcontact.contains(cs.ocontact))
                   selectedcontact.remove(cs.ocontact);
               }
       }   
    } 
    
      public ApexPages.StandardSetController standardSetCon {
        get {
            if(standardSetCon == null) {
                standardSetCon = new ApexPages.StandardSetController(Database.getQueryLocator(contactListQuery));
                // sets the number of records in each page set
                standardSetCon .setPageSize(20);
            }
            return standardSetCon ;
        }
        set;
    }


    public List<contactSet> getCurrentList() {
       updateSelectedContact(); 
       contactSetList = new List<contactSet>();       
        for (contact category : (List<contact>)standardSetCon.getRecords()){        
            if(selectedContact.contains(category))          
            contactSetList.add(new contactSet(category,'true'));
            else
            contactSetList.add(new contactSet(category));
        }
        fals=false;
        return contactSetList;
    } 
    
    public PageReference refreshList2() {       
       standardSetCon = null;     
       string s;
       if(apexpages.currentpage().getparameters().get('alpha') == 'All')
           s='%';
       else
           s= apexpages.currentpage().getparameters().get('alpha')+'%';
       
       contactListQuery= 'select id,name,phone from contact where name like' +'\''+s +'\''+ 'limit 5000';           
        return null;
    }
    
   
    
    public List<contact> getDisplaySelectedList(){
        updateSelectedContact();
        List<contact> displaycon = new list<contact>();
        displaycon.addall(selectedcontact);
        return displaycon;
    }
       
    public class contactSet { 
        public Boolean checked{ get; set; }
        public contact ocontact { get; set;} 


        public contactSet(){
            ocontact = new contact();
            checked = false;
        } 
        public contactSet(contact c){
            ocontact = c;
            checked = false;


        } 
        public contactSet(contact c,string s){
            ocontact = c;
            checked = true;


        } 
    }
    
  
}




<apex:page controller="Pagin_alpha">
<style type="text/css">
      .loadingIcon {
            background-image: url(/img/loading.gif);
            width: 16px;
            height: 16px;
        }
     </style>
<script type="text/javascript">
function checkAll(cb,cbid)
        {
            var inputElem = document.getElementsByTagName("input");                      
            for(var i=0; i<inputElem.length; i++)
            {   
                
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                         
                inputElem[i].checked = cb.checked;
                }
            }
        }
</script>
<apex:form id="form">
<br/>
<div style="align:right;">
<apex:repeat value="{!alphabet}" var="a">
<apex:commandLink value="{!a}" action="{!refreshList2}" rerender="form" style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}" status="stat">
<apex:param name="alpha" value="{!a}"/>
</apex:commandLink>
&nbsp;|&nbsp;
</apex:repeat>
</div>
<br/>
<apex:pageBlock id="block">
<apex:pageBlockButtons >
<apex:commandButton rendered="{!standardsetcon.hasprevious}" value="Previous" action="{!standardsetcon.previous}" rerender="block,block2" status="stat"/>
<apex:commandButton rendered="{!standardsetcon.hasnext}" value="Next" action="{!standardsetcon.next}" rerender="block,block2" status="stat"/>
</apex:pageBlockButtons>
<apex:actionStatus id="stat">
<apex:facet name="start"> 
<apex:outputPanel layout="block" styleClass="message infoM4">
<apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;">
<apex:panelGroup >
<img class="loadingIcon" src="/s.gif"/>
</apex:panelGroup>
<apex:panelGroup >
<div class="messageText">Please wait...</div>
</apex:panelGroup>
</apex:panelGrid>
</apex:outputPanel>
</apex:facet>
<apex:facet name="stop">
<apex:pageBlockTable value="{!CurrentList}" var="c" id="table">
<apex:column >
<apex:facet name="header"><apex:inputcheckbox onclick="checkAll(this,'check')" value="{!fals}" /></apex:facet>
<apex:inputcheckbox value="{!c.checked}" id="check">
</apex:inputcheckbox>
</apex:column>
<apex:column value="{!c.ocontact.name}" headerValue="Name"/>
</apex:pageBlockTable>
</apex:facet>
</apex:actionStatus>
</apex:pageBlock>
<apex:pageBlock title="Selected Contacts" id="block2">
<apex:commandButton value="Display Selected" rerender="block2"/>
<apex:pageBlockTable value="{!DisplaySelectedList}" var="c">
<apex:column value="{!c.name}" headerValue="Name"/>
<apex:column value="{!c.phone}" headerValue="Phone"/>
</apex:pageBlockTable>
</apex:pageBlock>


</apex:form>  
</apex:page>



No comments:

Post a Comment