function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adarsh Sharma 4Adarsh Sharma 4 

custom pagination without offset

Hi ,all.I'm doing custom pagination and I'm receving the following error
System.ListException: List index out of bounds: 2
Error is in expression '{!doSearch}' in component <apex:commandButton> in page simplecustompagination: Class.simpleCustomPagination.doSearch: line 100, column 1

this is my code

public with sharing class simpleCustomPagination {
    Public Boolean hasPrevious {
        get;
        set;
    }
    Public Boolean hasNext {
        get;
        set;
    }
    Public Integer pageno {
        get;
        set;
    }
    Public List < Account > lstacc {
        get;
        set;
    }
    Public Integer count {
        get;
        set;
    }


    String searchText;
    public List < Account > accounts {
        get;
        set;
    }

    Public simpleCustomPagination(ApexPages.StandardSetController controller) {}
    public simpleCustomPagination(ApexPages.StandardController controller) {}

    private String sortDirection = 'ASC';
    private String sortExp = 'name';

    public String sortExpression {
        get {
            return sortExp;
        }
        set {
            //if the column is clicked on then switch between Ascending and Descending modes
            if (value == sortExp)
                sortDirection = (sortDirection == 'ASC') ? 'DESC' : 'ASC';
            else
                sortDirection = 'ASC';
            sortExp = value;
        }
    }

    public String getSortDirection() {
        //if not column is selected 
        if (sortExpression == null || sortExpression == '')
            return 'ASC';
        else
            return sortDirection;
    }

    public void setSortDirection(String value) {
        sortDirection = value;
    }


    public String getSearchText() {
        return searchText;
    }
    public void setSearchText(String s) {

        searchText = s;

    }
    public PageReference doSearch() {
        String query = '';
        String strFilter = '';
        string sortFullExp = sortExpression + ' ' + sortDirection;
        if (searchText != null) {
            strFilter = strFilter + ' where Name Like \'' + searchText + '%\'';
        }

        if (strFilter != '') {
            query = 'Select id, Name, BillingCity, BillingCountry, Phone from Account ' + strFilter + 'order by ' + sortFullExp;

        } else {
            query = 'Select id, Name, BillingCity, BillingCountry, Phone from Account order by ' + sortFullExp;

        }



        accounts = Database.query(query);

        lstacc = new List < Account > ();

        if (accounts.size() > 0) {

            count = accounts.size();
            hasPrevious = false;
            hasNext = true;
            pageno = 1;
            for (integer i = 0; i < 5; i++) {
                lstacc.add(accounts[i]);
            }

        }


        return null;
    }


    Public void First() {
        hasNext = true;
        lstacc.clear();
        hasPrevious = false;
        pageno = 1;
        for (integer i = 0; i < 5; i++) {
            lstacc.add(accounts[i]);
        }
    }

    Public void Previous() {
        hasNext = true;
        lstacc.clear();
        if (pageno != 1) {
            pageno = pageno - 1;
            If(pageno == 1)
            hasPrevious = false;
            for (integer i = (pageno - 1) * 5; i < (pageno * 5); i++) {
                lstacc.add(accounts[i]);
                System.debug('--------------------SIZE OF PAGINATION LIST---------------------' + lstacc.size() + ' RECORDS ' + accounts[i]);
            }
        } else {
            hasPrevious = false;

            for (integer i = (pageno - 1) * 5; i < (pageno * 5); i++) {
                lstacc.add(accounts[i]);
                System.debug('--------------------SIZE OF PAGINATION LIST---------------------' + lstacc.size() + ' RECORDS ' + accounts[i]);
            }
        }
    }

    Public void Next() {
        pageno = pageno + 1;
        integer pgno;
        lstacc.clear();
        if (math.mod(accounts.size(), 5) == 0) {
            pgno = accounts.size() / 5;
        } else
            pgno = (accounts.size() / 5) + 1;
        if (pageno == pgno) {
            hasNext = false;
            for (integer i = (pageno - 1) * 5; i < accounts.size(); i++) {
                lstacc.add(accounts[i]);
                System.debug('--------------------SIZE OF PAGINATION LIST---------------------' + lstacc.size() + ' RECORDS ' + accounts[i]);
            }
        } else
            for (integer i = (pageno - 1) * 5; i < (pageno * 5); i++) {
                lstacc.add(accounts[i]);
                System.debug('--------------------SIZE OF PAGINATION LIST---------------------' + lstacc.size() + ' RECORDS ' + accounts[i]);
            }
        hasPrevious = true;
        System.debug('---------------------------- LIST SIZE OF TEMPLIST ---------------------' + accounts.size());
    }

    Public void Last() {
        System.debug('---------------------------------------------- LAST METHOD IS CALLING ----------------------');
        hasPrevious = true;
        lstacc.clear();
        hasNext = false;
        if (math.mod(accounts.size(), 5) == 0)
            pageno = accounts.size() / 5;
        else
            pageno = (accounts.size() / 5) + 1;
        for (integer i = (pageno - 1) * 5; i < accounts.size(); i++) {
            lstacc.add(accounts[i]);
        }
    }

}
pconpcon
This is most likely because you are only returning to accounts and your loop is expecting to have 5 back.  The best practice for this type of loop is to use the built in iterator and do the following:
 
lstacc = new List < Account > ();

 if (!accounts.isEmpty()) {
    count = accounts.size();
    hasPrevious = false;
    hasNext = true;
    pageno = 1;
    for (Account acct : accounts) {
        lstacc.add(acct);
    }
}

If you feel you have to use the integer like that you should instead set your upper bound better
 
lstacc = new List < Account > ();

 if (!accounts.isEmpty()) {
    count = accounts.size();
    hasPrevious = false;
    hasNext = true;
    pageno = 1;
    for (Integer i = 0; i < (accounts.size() < 5) ? accounts.size() : 5; i++) {
        lstacc.add(accounts[i]);
    }
}

The ternary in the if statement there will return an integer equal to the size of the account list or 5.  Whichever is lower.
Adarsh Sharma 4Adarsh Sharma 4
thank u sir,this is work fine
pconpcon
Great! If you could please choose a "best answer" so that this question can be removed from the unresolved queue and so others may easily find the answer.