anusha(salesforce developer)

Thursday, 15 September 2016

Pagination using apex:dataTable in Salesforce

Pagination using apex:dataTable in Salesforce

Sample Code:

Visualforce page:

<apex:page Controller="sample" sidebar="false" >
<apex:form >
    <apex:pageblock id="pg" >
        <apex:datatable value="{!mem}" var="m" rules="all" border="1" columnsWidth="100px,100px,100px" cellspacing="8" cellpadding="8" rows="5">
            <apex:facet name="footer">Total Number of records: {!count}</apex:facet>
            <apex:column value="{!m.Name}">
                <apex:facet name="header">Member Name</apex:facet>
            </apex:column>
            <apex:column value="{!m.City__c}">
                <apex:facet name="header">City</apex:facet>
            </apex:column>           
            <apex:column value="{!m.Country__c}">
                <apex:facet name="header">Country</apex:facet>
            </apex:column>           
        </apex:datatable>
        <br/><br/>
        <apex:commandButton value="Previous" action="{!previous}" reRender="pg" disabled="{!prevBool}"/><apex:commandButton value="Next" action="{!next}" reRender="pg" disabled="{!nextBool}"/>       
    </apex:pageblock>
</apex:form>   
</apex:page> 


Apex Controller:

public with sharing class sample
{
    public List<Member__c> mem {get;set;}
    public Integer count {get;set;}
    public List<Member__c> memList = new List<Member__c>();
   
    public Integer index = 5;
    public Integer start = 0;
   
    public Boolean nextBool {get;set;}
    public Boolean prevBool {get;set;}
   
    public sample()
    {
        memList = [SELECT Name, City__c, Country__c FROM Member__c ORDER BY Name];
        count = [SELECT Count() FROM Member__c];
        List<Member__c> temp = new List<Member__c>();
        for(Integer i = start; i<index; i++)
        {       
            temp.add(memList.get(i));
        }
        mem = temp;
        prevBool = true;
        nextBool = false;
    }
   
    public void next()
    {
        index = index + 5;
        start = start + 5;
        mem.clear();
        if(index > count)
        {
            index = Math.Mod(count,5) + start;
            system.debug('Index is ' + index);
            nextBool = true;
            prevBool = false;
            List<Member__c> temp = new List<Member__c>();
            for(Integer i = start; i<index; i++)
            {       
                temp.add(memList.get(i));
            }
            mem = temp;        
            index = start + 5;   
        }
        else
        {
            List<Member__c> temp = new List<Member__c>();
            for(Integer i = start; i<index; i++)
            {       
                temp.add(memList.get(i));
            }
            mem = temp;    
            prevBool = false;
        }  
    }
   
    public void previous()
    {
        if(start > 5)
        {   
            index = index - 5;
            start = start - 5;
            List<Member__c> temp = new List<Member__c>();
            for(Integer i = start; i<index; i++)
            {       
                temp.add(memList.get(i));
            }
            mem = temp;
            prevBool = false;
            nextBool = false;
        }   
        else
        {
            index = index - 5;
            start = start - 5;
            List<Member__c> temp = new List<Member__c>();
            for(Integer i = start; i<index; i++)
            {       
                temp.add(memList.get(i));
            }
            mem = temp;
            prevBool = true;
            nextBool = false;       
        }  
    }


Output:

No comments:

Post a Comment