Another Salesforce blog!!!

Salesforce, Apex

checkbox pagination using Database.getQueryLocator


Visualforce page:

<apex:page controller="ContactTable" sidebar="false">
    <apex:form id="myform">        
        <apex:pageblock title="Maintaining Selected Records">            
            <apex:pageBlockButtons location="Top">                
                <apex:commandButton value="Processed" action="{!clickMe}" />                
            </apex:pageBlockButtons>            
            <!-- In our table, we are displaying the Contact records -->            
            <apex:pageblocktable value="{!Contact}" var="cc" >                
                <apex:column headerValue="Choose Contact">                    
                    <!-- This is our selected Boolean property in our wrapper class -->                    
                    <apex:inputCheckbox value="{!cc.bool}"/>                    
                </apex:column>                
                <!-- This is how we access the contact values within our Contact container/wrapper -->                
                <apex:column value="{!cc.con.Name}"/>                
                <apex:column value="{!cc.con.Accountid}"/>                
                <apex:column value="{!cc.con.Email}"/>                
                <apex:column Value="{!cc.con.MobilePhone}"/>                
                <apex:column value="{!cc.con.LeadSource}"/>                
            </apex:pageblocktable>
            
             <!--PAGINATION outputPanel-->
            <apex:pageBlockButtons location="Bottom">                
                <apex:commandButton status="st" value="First Page" action="{!Setcon.First}" disabled="{!!hasPrevious}" reRender="myform" />                
                <apex:commandButton status="st" value="Previous Page" action="{!Setcon.Previous}" disabled="{!!hasPrevious}" reRender="myform" />   
                         <apex:outputText > &nbsp;Page {!(pageNumber * size)+1-size} of {!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of total {!noOfRecords}</apex:outputText>&nbsp;
                <apex:commandButton status="st" value="Next Page" action="{!Setcon.Next}" disabled="{!!hasNext}" reRender="myform" />                
                <apex:commandButton status="st" value="Last Page" action="{!Setcon.Last}" disabled="{!!hasNext}" reRender="myform" /> 
                
                <apex:outputPanel style="color:#008000;font-weight:bold">
                <apex:actionStatus id="st" startText="Fetching..." stopText=""/>
            </apex:outputPanel>
            </apex:pageBlockButtons>
        </apex:pageblock>    
        
        <apex:pageblock title="Selected Record(s)">            
            <apex:outputPanel rendered="{!display}">                
                <apex:pageblocktable value="{!selectedList}" var="w" >                    
                    <apex:column value="{!w.Name}"/>                    
                    <apex:column value="{!w.Accountid}"/>                    
                    <apex:column value="{!w.Email}"/>                    
                    <apex:column value="{!w.MobilePhone}"/>                    
                    <apex:column value="{!w.LeadSource}"/>                    
                </apex:pageblocktable>                
            </apex:outputPanel>                  
        </apex:pageblock>        
        
    </apex:form>
</apex:page>

Controller:

public class ContactTable        
{        
          
    //This is Our collection of the class/wrapper objects WrapperContactWrapper        
    public List<WrapperContactWrapper> wrapperlist;        
   // public Integer noOfRecords{get; set;}        
    // Create a new Map to verify whether the contact is already added in the Map        
    Map <id,Contact> SelectedcontactMap = new Map <id,Contact>();        
    public boolean display{get;set;}        
    public list<Contact> selectedList {get;set;}        
    // instantiate the StandardSetController from a query locator        
    public ApexPages.StandardSetController Setcon            
    {            
        get                
        {                
            if(Setcon == Null)                    
            {                    
                Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Accountid,Email,MobilePhone,LeadSource from Contact limit 100]));                    
                // sets the number of records in each page set                    
                 this.size = 5;
                this.setCon.setpageNumber(1);
                this.setCon.setPageSize(size);                   
                noOfRecords = setCon.getResultSize();                    
            }                
            return Setcon;                
        }            
        set;            
    }
    
    //Returns a list of wrapper objects for the sObjects in the current page set        
    public List<WrapperContactWrapper> getContact()            
    {            
        getSelectedContact();            
        // Initilaize the list to add the selected contact            
        wrapperlist = new List <WrapperContactWrapper>();            
        for(Contact cc : (List<contact>)Setcon.getRecords())                
        {                
            if( SelectedcontactMap .ContainsKey(cc.id))                    
            {                    
                wrapperlist.add (new WrapperContactWrapper(cc,true));                    
            }                
            else                    
            {                    
                wrapperlist.add(new WrapperContactWrapper(cc,false));                    
            }                
        }            
        return wrapperlist;
    }
    
    public void getSelectedContact()
    {            
        if(wrapperlist!=null)                
        {                
            for(WrapperContactWrapper wr:wrapperlist)                    
            {                    
                if(wr.bool == true)                        
                {                        
                    SelectedcontactMap.put(wr.con.id,wr.con); // Add the selected contact id in to the SelectedcontactMap.                        
                }                    
                else                        
                {                        
                    SelectedcontactMap.remove(wr.con.id); // If you uncheck the contact, remove it from the selectedcontactMap                        
                }                    
            }                
        }            
    }        
    public void clickMe()            
    {            
        display = true;            
        getSelectedContact();                        
        selectedList = SelectedcontactMap.values();            
    }        
    public integer pageNumber            
    {            
        get                
        {                
            return Setcon.getPageNumber();                
        }            
        set;            
    }     
    
    /**
     * If the pagiantion has next page
     */
    public Boolean hasNext {
        get {
            return Setcon.getHasNext();
        }
        set;
    }

    //Indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return Setcon.getHasPrevious();
        }
        set;
    }

    //returns the page number of the current page set
    

    public Integer totalPageNumber {
        get {
            Decimal totalSize = this.Setcon.getResultSize();
            Decimal pageSize = this.Setcon.getPageSize();
            Decimal pages = totalSize/pageSize;
            return (Integer)pages.round(System.RoundingMode.CEILING);
        }
        set;
    }

    /**
     * Go to the first page directly
     */
    public void first() {
         Setcon.first();
     }

     // returns the last page of records
     public void last() {
         Setcon.last();
     }

     // returns the previous page of records
     public void previous() {
         Setcon.previous();
     }

     // returns the next page of records
     public void next() {
         Setcon.next();
     }

    Public Integer size{get;set;}
    public Integer noOfRecords{get; set;}
    
    public class WrapperContactWrapper        
    {        
        public Contact con{get;set;}        
        public boolean bool{get;set;}        
        public WrapperContactWrapper(Contact c,boolean bool)            
        {            
            this.con = c;            
            this.bool = bool;            
        }        
    }
}
public class WrapperContactWrapper
{    
    public Contact con{get;set;}    
    public boolean bool{get;set;}    
    public WrapperContactWrapper(Contact c,boolean bool)        
    {        
        this.con = c;        
        this.bool = bool;        
    } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*