Hi I am trying to implement sorting and pagination in visualforce. I am using the standardsetcontroller but for some reason the standardsetcontroller methods do not fire the actions when clicked on. Below is my apex controller as well as my visualforce markup. Will be glad for any suggestion on this. Thanks.
public with sharing class casesortingandpagination {
public String[] countries = new String[]{};
public String temp {get; set;}
public list<case> temp1 {get; set;}
public id uid;
public id creator;
public id actid;
public String contIdChosen {get; set;}
public String soql {get;set;}
public List <case> caseList1 = New List <case>();
public String soqlsort {get;set;}
public List <case> caseList2 = New List <case>();
public List <case> caseList3 = New List <case>();
public casesortingandpagination(){
this.uid = UserInfo.getUserid();
this.actid = [select accountid from user where id = :uid][0].accountid;
if (temp != null){
this.temp = temp;
}else{
this.temp = 'e';
}
getItems();
getCaseList2();
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('a','All Closed Cases'));
options.add(new SelectOption('b','All Open Case'));
options.add(new SelectOption('c','My Closed Cases'));
options.add(new SelectOption('d','My Open Case'));
options.add(new SelectOption('e','Recently Viewed Cases'));
return options;
}
public pageReference getCaseList2(){
this.temp = temp;
getCaseList();
return null;
}
// List used in to display the table in VF page.
public List<case> getCaseList() {
// Passing the values of list to VF page.
return con.getRecords();
}
public ApexPages.StandardSetController con {
get {
if(temp == 'a') {
// String Query to have a list of cases for a respective End-user.
soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
System.debug('temp a is: ' + temp);
// Passing the String array to a list with Selected field sorting.
caseList1 = Database.query(soql + ' where status = \'Closed\' order by ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
con = new ApexPages.StandardSetController(caseList1);
// sets the number of records in each page set
}else if(temp == 'b') {
// String Query to have a list of cases for a respective End-user.
soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
System.debug('temp b is: ' + temp);
// Passing the String array to a list with Selected field sorting.
caseList1 = Database.query(soql + ' where status != \'Closed\' order by ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
con = new ApexPages.StandardSetController(caseList1);
// sets the number of records in each page set
}else if(temp == 'c') {
// String Query to have a list of cases for a respective End-user.
soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
System.debug('temp c is: ' + temp);
// Passing the String array to a list with Selected field sorting.
caseList1 = Database.query(soql + ' where CreatedById = :uid and status = \'Closed\' order by ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
con = new ApexPages.StandardSetController(caseList1);
// sets the number of records in each page set
}else if(temp == 'd') {
// String Query to have a list of cases for a respective End-user.
soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
System.debug('temp d is: ' + temp);
// Passing the String array to a list with Selected field sorting.
caseList1 = Database.query(soql + ' where CreatedById = :uid and status != \'Closed\' order by ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
con = new ApexPages.StandardSetController(caseList1);
// sets the number of records in each page set
}else if(temp == 'e') {
// String Query to have a list of cases for a respective End-user.
soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
System.debug('temp e is: ' + temp);
// Passing the String array to a list with Selected field sorting.
caseList1 = Database.query(soql + ' where CreatedById = :uid AND LastViewedDate != NULL order by LastViewedDate DESC, ' + sortField + ' ' + sortDir );
// setting values of List in StandardSetController.
con = new ApexPages.StandardSetController(caseList1);
// sets the number of records in each page set
}
con.setPageSize(10);
return con;
}
set;
}
public Boolean hasNext {
get {
return con.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return con.getHasPrevious();
}
set;
}
public Integer pageSize {
get {
return con.getPageSize();
}
set;
}
Public Integer getTotalPages(){
Decimal totalSize = this.Con.getResultSize();
Decimal pageSize = this.Con.getPageSize();
Decimal pages = totalSize/pageSize;
return (Integer)pages.round(System.RoundingMode.CEILING);
}
public Integer pageNumber {
get {
return con.getPageNumber();
}
set;
}
public void first() {
this.con.first();
}
// returns the last page of records
public void last() {
this.con.last();
}
// returns the previous page of records
public void previous() {
this.con.previous();
}
// returns the next page of records
public void next() {
this.con.next();
}
// returns the PageReference of the original page, if known, or the home page.
public void cancel() {
con.cancel();
}
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again for sorting other columns
soqlsort = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
// Adding String array to a List array
caseList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir );
// Adding Caselist to Standard Pagination controller variable caselist2
con = new ApexPages.StandardSetController(caseList1);
// Set Page Size to 5
con.setPageSize(10);
}
public String sortDir {
// To set a Direction either in ascending order or descending order.
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir;}
set;
}
// the current field to sort by. defaults to last name
public String sortField {
// To set a Field for sorting.
get { if (sortField == null) {sortField = 'CaseNumber'; } return sortField; }
set;
}
}
Visualforce markup:
<apex:page controller="casesortingandpagination" showHeader="false" sidebar="false" >
<apex:form styleClass="w25" >
<apex:selectList id="selected_list" value="{!temp}" required="false" size="1">
<apex:selectOptions value="{!items}" />
<apex:actionSupport event="onchange" reRender="form" action="{!getCaseList2}" oncomplete="getCaseList2();" />
</apex:selectList>
</apex:form>
<apex:form id="form">
<apex:pageBlock id="pgblock" >
<apex:pageBlockSection title="Case Details - Page #{!pageNumber}" columns="1" collapsible="false" rendered="{!TotalPages > 0}">
<apex:pageBlockTable value="{!CaseList}" var="CsList" title="Click Column Header for Sorting" headerClass="FCenter" >
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Case Number" action="{!toggleSort}" rerender="pgblock">
<!-- Value attribute should have field (API Name) to sort in asc or desc order -->
<apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.CaseNumber}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Created Date" action="{!toggleSort}" rerender="pgblock">
<!-- Value attribute should have field (API Name) to sort in asc or desc order -->
<apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.CreatedDate}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Requested_By__c" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="Requested_By__c" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.Requested_By__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Subject" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.Subject}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Status" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.Status}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Priority" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="Priority" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.Priority}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Owner Id" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.OwnerId}"/>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Type" action="{!toggleSort}" rerender="pgblock">
<apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!CsList.Type}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:outputtext styleclass="detailText" value="Page {!pageNumber} of {!TotalPages}" rendered="{!TotalPages > 0}"></apex:outputtext>
<apex:panelGrid columns="4">
<apex:commandLink action="{!first}" value="First"></apex:commandlink>
<apex:commandLink action="{!previous}">Previous</apex:commandlink>
<apex:commandLink action="{!next}">Next</apex:commandlink>
<apex:commandLink action="{!last}" value="Last"></apex:commandlink>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>