Showing posts with label Visualforce Pagination.. Show all posts
Showing posts with label Visualforce Pagination.. Show all posts

Thursday, May 29, 2014

Simple Pagination using apex in Salesforce?

Simple Pagination using apex in Salesforce?


Hi !,

Here i am going to give an example of simple pagination on Account object. to achieve this we need to create Visualforce page and Controller Class.

Visualforce Page:-

<apex:page controller="AccountPaginationController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Account Results -  Page #{!pageNumber}" columns="1">
                <apex:pageBlockTable value="{!listAccount}" var="a">
                    <apex:column value="{!a.Name}"/>
                    <apex:column value="{!a.AccountNumber}"/>
                    <apex:column value="{!a.Type}"/>
                </apex:pageBlockTable>
                <apex:panelGrid columns="4">
                    <apex:commandLink action="{!first}">First </apex:commandlink> 
                    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous |</apex:commandlink>
                    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next |</apex:commandlink>
                    <apex:commandLink action="{!last}">Last</apex:commandlink>
                </apex:panelGrid>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Controller Class:-

public class AccountPaginationController {
 public ApexPages.StandardSetController con {
        get {
                if(con == null) {
                    con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name, AccountNumber,Type FROM Account Order By Name limit 100])); 
                    //here you can set the record size to display                
                    con.setPageSize(3);       
                }
                return con;
        }
        set;
    }
    
    public List<Account> getlistAccount(){
        return (List<Account>)con.getRecords();
    }
    
    public Boolean hasNext {
        get {
                return con.getHasNext();
            }
        set;
    }
   
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
   
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
   
    public void first() {
        con.first();
    }
   
    public void last() {
        con.last();
    }

    public void previous() {
        con.previous();
    }
    
    public void next() {
        con.next();
    }
  
    public void cancel() {
        con.cancel();
    }        


}

then here is the final out put of this page



That's it.
Enjoy the Coding !!!!!!

 
| ,