Tuesday, June 27, 2017

Pagination in salesforce:

What is StandardSetController Class?


StandardSetController objects allow you to create list controllers similar to, or as extensions of, the pre-built Visualforce list controllers provided by Salesforce.

Instantiation of Standardset Controller is 

You can instantiate a StandardSetController in either of the following ways:

From a list of sObjects:

List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);

From a query locator:

ApexPages.StandardSetController ssc = 
new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));

StandardSetController Methods

The following are methods for StandardSetController. All are instance methods.

cancel() 
Returns the PageReference of the original page, if known, or the home page.

first() 
Returns the first page of records.

getCompleteResult() 
Indicates whether there are more records in the set than the maximum record limit. If this is false, there are more records than you can process using the list controller. The maximum record limit is 10,000 records.

getFilterId() 
Returns the ID of the filter that is currently in context.

getHasNext() 
Indicates whether there are more records after the current page set.

getHasPrevious() 
Indicates whether there are more records before the current page set.

getListViewOptions() 
Returns a list of the listviews available to the current user.

getPageNumber() 
Returns the page number of the current page set. Note that the first page returns 1.

getPageSize() 
Returns the number of records included in each page set.

getRecord() 
Returns the sObject that represents the changes to the selected records. This retrieves the prototype object contained within the class, and is used for performing mass updates.

getRecords() 
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can't call clear() on it.

getResultSize() 
Returns the number of records in the set.

getSelected() 
Returns the list of sObjects that have been selected.

last() 
Returns the last page of records.

next() 
Returns the next page of records.

previous() 
Returns the previous page of records.

save() 
Inserts new records or updates existing records that have been changed. After this operation is finished, it returns a PageReference to the original page, if known, or the home page.

setFilterID(String) 
Sets the filter ID of the controller.

setpageNumber(Integer) 
Sets the page number.

setPageSize(Integer) 
Sets the number of records in each page set.

setSelected(sObject[]) 
Set the selected records.

How to show 10,000 records in visualforce page

As the number of records increases, the time required for the browser to render them increases. Paging is used to reduce the amount of data exchanged with the client. Paging is typically handled on the server side (standardsetcontroller). The page sends parameters to the controller, which the controller needs to interpret and then respond with the appropriate data set. Here is the controller which makes use of standard set controller for pagination.
?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public with sharing class Pagination {
    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;
    }
      
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
   
    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }
   
    public void first() {
        setCon.first();
    }
   
    public void last() {
        setCon.last();
    }
   
    public void previous() {
        setCon.previous();
    }
   
    public void next() {
        setCon.next();
    }
}
Using the above controller methods we can define the pagination.
?
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
<apex:page controller="Pagination">
    <apex:form>
        <apex:pageblock id="pb">
            <apex:pageblocktable value="{!Accounts}" var="a">
                <apex:column value="{!a.Name}"></apex:column>
                <apex:column value="{!a.Type}"></apex:column>
                <apex:column value="{!a.BillingCity}"></apex:column>
                <apex:column value="{!a.BillingState}"></apex:column>
                <apex:column value="{!a.BillingCountry}"></apex:column>
           </apex:pageblocktable>
            <apex:panelgrid columns="7">
                <apex:commandbutton action="{!first}" disabled="{!!hasPrevious}" rerender="pb" status="fetchStatus" title="First Page" value="|<"></apex:commandbutton>
                <apex:commandbutton action="{!previous}" disabled="{!!hasPrevious}" rerender="pb" status="fetchStatus" title="Previous Page" value="<"></apex:commandbutton>
                <apex:commandbutton action="{!next}" disabled="{!!hasNext}" rerender="pb" status="fetchStatus" title="Next Page" value=">"></apex:commandbutton>
                <apex:commandbutton action="{!last}" disabled="{!!hasNext}" rerender="pb" status="fetchStatus" title="Last Page" value=">|"> </apex:commandbutton>
                <apex:outputtext>{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputtext>
                <apex:commandbutton action="{!refresh}" rerender="pb" status="fetchStatus" title="Refresh Page" value="Refresh"></apex:commandbutton>
                <apex:outputpanel style="color: #4aa02c; font-weight: bold;">
                    <apex:actionstatus id="fetchStatus" starttext="Fetching..." stoptext="">
                </apex:actionstatus>
             </apex:outputpanel>
           </apex:panelgrid>
        </apex:pageblock>
    </apex:form>
</apex:page>

No comments:

Post a Comment