Wednesday, 14 November 2018

Basic Pagination with Custom Controller



VF Code:

<apex:page controller="PaginationWithCustomControllerClass">
    <apex:form >
        <apex:pageBlock title="Pagination with Custom Controller
" id="pgBlock">
            <apex:pageBlockTable value="{!Accounts}" var="acc" id="pgTable">
                <apex:column value="{!acc.Name}"/>
                <apex:column value="{!acc.Industry}"/>
                <apex:column value="{!acc.Rating}"/>
                <apex:column value="{!acc.Phone}"/>
            </apex:pageBlockTable>
            <br/>
            <b><apex:outputText value="Total Number of Records are {!totalrecords}"/> </b>
       
            <apex:pageBlockButtons >
                <apex:commandButton value="Previous" action="{!Previous}" rerender="pgBlock"
                                    status="status" disabled="{!DisablePrevious}" />
                <apex:commandButton value="Next" action="{!Next}" reRender="pgBlock"
                                    status="status" disabled="{!DisableNext}" />
                <apex:actionStatus id="status" startText="Loading..."/>
             

            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>

</apex:page>




Controller Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public with sharing class PaginationWithCustomControllerClass{

    private Integer CountTotalRecords;
    private Integer OffsetSize = 0;
    private Integer QueryLimit = 10;
    public list <Account> lstAccount{get;set;}
    
    public PaginationWithCustomControllerClass(){
        CountTotalRecords= [select count() from Account];
    }
      
    public list <Account> getAccounts(){
        lstAccount = new list  <Account> ();
        lstAccount = [Select id, Name, Industry, Rating, Phone from Account order by Name limit :QueryLimit offset :OffsetSize];
        return lstAccount; 
    }
    
    public PageReference Next() {
        OffsetSize += QueryLimit;
        return null;
    }
    
    public PageReference Previous() {
        OffsetSize -= QueryLimit;
        return null;
    }
  
    public Boolean getDisablePrevious(){
        if(OffsetSize>0){
            return false;
        }else {
            return true;
        }
    }
    
    public Boolean getDisableNext() {
        if (OffsetSize + QueryLimit < countTotalRecords){
            return false;
        }else {
            return true;
        }
     }
    
    public integer gettotalrecords(){
        return [select count() from Account];
    }
}

No comments:

Post a Comment