function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suresh RaghuramSuresh Raghuram 

client side pagination with or with out using jquery

Hi Community

 

I tied using pagenation using the following example

http://th3silverlining.com/2010/04/21/client-side-visualforce-pagination-with-pajinate/ 

from the above blog but i did not make it working.

 

If some one have code with example please share with me.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
NashalNashal

Hi,

 

You can also use OFFSET in SOQL query which is a new feature from salesforce for pagination.

 

Sample Code:

 

public with sharing class CustomPaginationExt {
    public List<Account> accounts{get;set;}
    public Integer pageSize{get;set;}
    public Integer noOfPages{get;set;}
    public Integer pageNumber{get;set;}
    private String baseQuery = 'select name, industry, annualRevenue from Account order by name';
    private Integer totalNoOfRecs;
       
    public CustomPaginationExt(ApexPages.StandardController controller) {
        pageSize = 10;
        totalNoOfRecs = [select count() from Account limit 50000];
        getInitialAccountSet();
    }
    
    public PageReference getInitialAccountSet()
    {
        pageNumber = 0;
        noOfPages = totalNoOfRecs/pageSize;
        
        if (Math.mod(totalNoOfRecs, pageSize) > 0)
            noOfPages++;
        
        try{
            accounts = Database.query(baseQuery + ' limit '+pageSize);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
        return null;
    }
    
    public PageReference next(){
        pageNumber++;
        
        queryAccounts();
        return null;
    }
    public PageReference previous(){
        pageNumber--;
        if (pageNumber < 0)
            return null;
        
        queryAccounts();
        return null;
    }
    
    private void queryAccounts()
    {
        Integer offset = pageNumber * pageSize;
        String query = baseQuery + ' limit '+pageSize +' offset '+ offset;
        System.debug('Query is'+query);
        try{
            accounts = Database.query(query);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
    }
}
Page Code:
<apex:page standardController="Account" extensions="CustomPaginationExt">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Page Size" >
            <apex:pageBlockSection columns="1" >
                    <apex:selectList size="1" value="{!pageSize}">
                        <apex:selectOption itemValue="10" itemLabel="10"/>
                        <apex:selectOption itemValue="50" itemLabel="50"/>
                        <apex:selectOption itemValue="100" itemLabel="100"/>
                        <apex:actionSupport event="onchange" action="{!getInitialAccountSet}" rerender="accts"/>
                    </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock id="accts" title="Large Accounts" >
            <apex:pageBlockSection columns="1" >
                 <apex:pageblockTable value="{!accounts}" var="acct" >
                    <apex:column >
                            <apex:facet name="header">Account Name</apex:facet>
                            <apex:outputField value="{!acct.name}"/>
                    </apex:column>
                    <apex:column >
                            <apex:facet name="header">Industry</apex:facet>
                            <apex:outputField value="{!acct.Industry}"/>
                    </apex:column>
                    <apex:column >
                            <apex:facet name="header">Annual Revenue</apex:facet>
                            <apex:outputField value="{!acct.annualRevenue}"/>
                    </apex:column>
                 </apex:pageblockTable>
             </apex:pageBlockSection>
            <br/>
            <apex:outputPanel style="text-align:center;horizontal-align:center;">
                <apex:commandLink rendered="{!NOT(pageNumber == 0)}" action="{!previous}" rerender="accts">Prev</apex:commandLink> | <apex:commandLink rendered="{!NOT(pageNumber == noOfPages -1)}" action="{!next}" rerender="accts">Next</apex:commandLink>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Thanks

All Answers

NashalNashal

Hi,

 

You can get the pagination demo here with source code...

 

http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/

 

 

 

Thanks

NashalNashal

Hi,

 

You can also use OFFSET in SOQL query which is a new feature from salesforce for pagination.

 

Sample Code:

 

public with sharing class CustomPaginationExt {
    public List<Account> accounts{get;set;}
    public Integer pageSize{get;set;}
    public Integer noOfPages{get;set;}
    public Integer pageNumber{get;set;}
    private String baseQuery = 'select name, industry, annualRevenue from Account order by name';
    private Integer totalNoOfRecs;
       
    public CustomPaginationExt(ApexPages.StandardController controller) {
        pageSize = 10;
        totalNoOfRecs = [select count() from Account limit 50000];
        getInitialAccountSet();
    }
    
    public PageReference getInitialAccountSet()
    {
        pageNumber = 0;
        noOfPages = totalNoOfRecs/pageSize;
        
        if (Math.mod(totalNoOfRecs, pageSize) > 0)
            noOfPages++;
        
        try{
            accounts = Database.query(baseQuery + ' limit '+pageSize);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
        return null;
    }
    
    public PageReference next(){
        pageNumber++;
        
        queryAccounts();
        return null;
    }
    public PageReference previous(){
        pageNumber--;
        if (pageNumber < 0)
            return null;
        
        queryAccounts();
        return null;
    }
    
    private void queryAccounts()
    {
        Integer offset = pageNumber * pageSize;
        String query = baseQuery + ' limit '+pageSize +' offset '+ offset;
        System.debug('Query is'+query);
        try{
            accounts = Database.query(query);
        }
        catch(Exception e){
            ApexPages.addMessages(e);
        }
    }
}
Page Code:
<apex:page standardController="Account" extensions="CustomPaginationExt">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Page Size" >
            <apex:pageBlockSection columns="1" >
                    <apex:selectList size="1" value="{!pageSize}">
                        <apex:selectOption itemValue="10" itemLabel="10"/>
                        <apex:selectOption itemValue="50" itemLabel="50"/>
                        <apex:selectOption itemValue="100" itemLabel="100"/>
                        <apex:actionSupport event="onchange" action="{!getInitialAccountSet}" rerender="accts"/>
                    </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock id="accts" title="Large Accounts" >
            <apex:pageBlockSection columns="1" >
                 <apex:pageblockTable value="{!accounts}" var="acct" >
                    <apex:column >
                            <apex:facet name="header">Account Name</apex:facet>
                            <apex:outputField value="{!acct.name}"/>
                    </apex:column>
                    <apex:column >
                            <apex:facet name="header">Industry</apex:facet>
                            <apex:outputField value="{!acct.Industry}"/>
                    </apex:column>
                    <apex:column >
                            <apex:facet name="header">Annual Revenue</apex:facet>
                            <apex:outputField value="{!acct.annualRevenue}"/>
                    </apex:column>
                 </apex:pageblockTable>
             </apex:pageBlockSection>
            <br/>
            <apex:outputPanel style="text-align:center;horizontal-align:center;">
                <apex:commandLink rendered="{!NOT(pageNumber == 0)}" action="{!previous}" rerender="accts">Prev</apex:commandLink> | <apex:commandLink rendered="{!NOT(pageNumber == noOfPages -1)}" action="{!next}" rerender="accts">Next</apex:commandLink>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Thanks
This was selected as the best answer