You need to sign in to do that
Don't have an account?
Adarsh 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]);
}
}
}
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]);
}
}
}
If you feel you have to use the integer like that you should instead set your upper bound better
The ternary in the if statement there will return an integer equal to the size of the account list or 5. Whichever is lower.