Simple Pagination using apex in Salesforce
Sample code:
Visualforce page:
<apex:page controller="MemberPaginationController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Interests Results - Page #{!pageNumber}" columns="1">
<apex:pageBlockTable value="{!listMember}" var="m">
<apex:column value="{!m.Name}"/>
<apex:column value="{!m.Age__c}"/>
</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>
Apex Controller:
public class MemberPaginationController {
public ApexPages.StandardSetController con {
get {
if(con == null) {
con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name, Age__c FROM Member__c Order By Name limit 100]));
con.setPageSize(5);
}
return con;
}
set;
}
public List<Member__c> getlistMember(){
return (List<Member__c>)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();
}
}
Output:
Visualforce page:
<apex:page controller="MemberPaginationController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Interests Results - Page #{!pageNumber}" columns="1">
<apex:pageBlockTable value="{!listMember}" var="m">
<apex:column value="{!m.Name}"/>
<apex:column value="{!m.Age__c}"/>
</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>
Apex Controller:
public class MemberPaginationController {
public ApexPages.StandardSetController con {
get {
if(con == null) {
con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name, Age__c FROM Member__c Order By Name limit 100]));
con.setPageSize(5);
}
return con;
}
set;
}
public List<Member__c> getlistMember(){
return (List<Member__c>)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();
}
}
Output:
No comments:
Post a Comment