+ Start a Discussion
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Help need to do pagination with datatable

Hi All,

 

I have a list of 25 records.

I need to display them in 5 pages (5 records in each page). i.e, with pagination.

 

Is there any way to do pagination without using StandardSetController ?

 

Please help me with the above.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

public void next(){
     try
        {
            ViewPrev = true; //flag to display next button when there are more than 5 records
           ListToDisplay.clear();
            currentPage++;
               
            if(next + Count < totList.size()){
                limit1 = next + Count;
            }else{
                limit1 = totList.size();
                shownext = false;
            }
             
            for(Integer i=next; i<limit1; i++){
                ListToDisplay.add(totList[i]);
            }    
                Next+= Count;
        }catch(Exception e){system.debug('Exception :'+e);}
    }
    public void previous(){
        try
        {   
            Viewnext = true;
            ListToDisplay.clear();
            currentPage--;
              
            if(next-(Count + Count ) > 0){
                limit1 = next - Count;
            }else{
                limit1 = next- Count;
                viewprev = false;
            }
           
            for(Integer i=next-(Count + Count); i<limit1; i++){    
             ListToDisplay.add(totList[i]);
            }    
                Next-= Count;
        }
        catch(Exception e)
        {
            system.debug('Exception :'+e);
        }              
Hey this is a working piece of code and one more thing declare INTEGER count =5, next=5;

This will definetly solves your problem , make this as a solution so every one can get benfit out of this.

Its up to you.

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below snippet as reference:

<apex:page controller="Pagination_min">

    <apex:form >

        <apex:pageBlock id="pb">

            <apex:pageBlockTable value="{!Accounts}" var="a">

                <apex:column value="{!a.Name}"/>

                <apex:column value="{!a.Type}"/>

                <apex:column value="{!a.BillingCity}"/>

                <apex:column value="{!a.BillingState}"/>

                <apex:column value="{!a.BillingCountry}"/>

            </apex:pageBlockTable>

            <apex:panelGrid columns="7">

                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>

                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>

                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>

                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>

                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>

                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>

                <apex:outputPanel style="color:#4AA02C;font-weight:bold">

                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>

                </apex:outputPanel>

            </apex:panelGrid>

        </apex:pageBlock>

    </apex:form>

</apex:page>

 

..............

Controller

..............

 

public with sharing class Pagination_min {

    Public Integer noOfRecords{get; set;}

    Public Integer size{get;set;}

    public ApexPages.StandardSetController setCon {

        get{

            if(setCon == null){

                size = 10;

                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';

                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));

                setCon.setPageSize(size);

                noOfRecords = setCon.getResultSize();

            }

            return setCon;

        }set;

    }

   

    Public List<Account> getAccounts(){

        List<Account> accList = new List<Account>();

        for(Account a : (List<Account>)setCon.getRecords())

            accList.add(a);

        return accList;

    }

   

    public pageReference refresh() {

        setCon = null;

        getAccounts();

        setCon.setPageNumber(1);

        return null;

    }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Naresh Krishna.ax1176Naresh Krishna.ax1176

Thanks Navatar for your quick reply.

 

I am getting the list of records by invoking the web service.

So I can't use ApexPages.StandardSetController.

 

Please suggest any other alternate method.

 

 

Suresh RaghuramSuresh Raghuram

hi naresh.

I already implemented this as follows.

 

collect all the 25 records into a list by

query for the 25 records;

based on if condition like this

here Integer count =5 , next =5;

If(list(25 records) < = count )

{

List(5 records) = List(25 records)

}else{

for(integer i=0 ; i< count; i++)

{

List(5 records) = List(25 records);

}

then write the methods for next and previous.

Try as i said above if you still not able to make it i will help you in writing  the code.

If this answers your question make it as solution.

Suresh RaghuramSuresh Raghuram

If(list(25 records).size() < = count )

{

List(5 records) = List(25 records)

}else{

for(integer i=0 ; i< count; i++)

{

List(5 records) = List(25 records);

}

Naresh Krishna.ax1176Naresh Krishna.ax1176

Thanks for your reply. Can you please paste sample code for next/previous.

Suresh RaghuramSuresh Raghuram

public void next(){
     try
        {
            ViewPrev = true; //flag to display next button when there are more than 5 records
           ListToDisplay.clear();
            currentPage++;
               
            if(next + Count < totList.size()){
                limit1 = next + Count;
            }else{
                limit1 = totList.size();
                shownext = false;
            }
             
            for(Integer i=next; i<limit1; i++){
                ListToDisplay.add(totList[i]);
            }    
                Next+= Count;
        }catch(Exception e){system.debug('Exception :'+e);}
    }
    public void previous(){
        try
        {   
            Viewnext = true;
            ListToDisplay.clear();
            currentPage--;
              
            if(next-(Count + Count ) > 0){
                limit1 = next - Count;
            }else{
                limit1 = next- Count;
                viewprev = false;
            }
           
            for(Integer i=next-(Count + Count); i<limit1; i++){    
             ListToDisplay.add(totList[i]);
            }    
                Next-= Count;
        }
        catch(Exception e)
        {
            system.debug('Exception :'+e);
        }              
Hey this is a working piece of code and one more thing declare INTEGER count =5, next=5;

This will definetly solves your problem , make this as a solution so every one can get benfit out of this.

Its up to you.

 

This was selected as the best answer