public with sharing class sample { public List<employee__c> mem {get;set;} public Integer count {get;set;} public List<employee__c> memList = new List<employee__c>();
public Integer index = 5; public Integer start = 0; // https://ap1.salesforce.com/a01/o public Boolean nextBool {get;set;} public Boolean prevBool {get;set;}
public sample() { memList = [SELECT City__c, Country__c FROM employee__c ORDER BY Name]; count = [SELECT Count() FROM employee__c]; List<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__c>(); for(Integer i = start; i<index; i++) { temp.add(memList.get(i)); } mem = temp; index = start + 5; } else { List<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__c>(); for(Integer i = start; i<index; i++) { temp.add(memList.get(i)); } mem = temp; prevBool = true; nextBool = false; } } }
You can refer this blog link http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/
Thanks,
N.J
Following is the code....
I hope you understand it...
<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.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>
public with sharing class sample
{
public List<employee__c> mem {get;set;}
public Integer count {get;set;}
public List<employee__c> memList = new List<employee__c>();
public Integer index = 5;
public Integer start = 0;
// https://ap1.salesforce.com/a01/o
public Boolean nextBool {get;set;}
public Boolean prevBool {get;set;}
public sample()
{
memList = [SELECT City__c, Country__c FROM employee__c ORDER BY Name];
count = [SELECT Count() FROM employee__c];
List<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__c>();
for(Integer i = start; i<index; i++)
{
temp.add(memList.get(i));
}
mem = temp;
index = start + 5;
}
else
{
List<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__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<employee__c> temp = new List<employee__c>();
for(Integer i = start; i<index; i++)
{
temp.add(memList.get(i));
}
mem = temp;
prevBool = true;
nextBool = false;
}
}
}