So I am trying to make a VF Page to display list of account records. We must have pagination and filtering from standard controller. Also we must use custom controller to create methods to alternate between ascending and descending sort on column name using column facet and commandlink. So I can get the pagination and filtering to work by itself. I can also get the column sorting to work by itself. However after extending standard account list controller column sorting functionality breaks. The visualforce page will always display sorted by account Name in ascending order. even though in the controller i have tried to default to sort by phone in descending order just to see if it would change.
Here is my VF PAGE:
<apex:page standardController="Account" recordSetVar="accounts" extensions="fakeAccountsListController">
<apex:form >
<apex:pageBlock title="Accounts List" id="accounts_list">
Filter:
<apex:selectList value="{! filterId }" size="1">
<apex:selectOptions value="{! listViewOptions }"/>
<apex:actionSupport event="onchange" reRender="accounts_list"/>
</apex:selectList><apex:pageBlockTable value="{! accounts }" var="act">
<!-- THE ACTIONS IN EACH COLUMN ARE NOT WORKING --><apex:column value="{! act.Name }">
<apex:facet name="header">
<apex:commandLink action="{! sortByName }"
reRender="accounts_list">First Name
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! act.Phone }">
<apex:facet name="header">
<apex:commandLink action="{! sortByPhone }"
reRender="accounts_list">Phone
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! act.NumberOfEmployees }">
<apex:facet name="header">
<apex:commandLink action="{! sortByNumEmpl }"
reRender="accounts_list">Num of Empl
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! act.Associated_Contacts__C }">
<apex:facet name="header">
<apex:commandLink action="{! sortByAssCons }"
reRender="accounts_list">Associated Contacts
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! act.Owner.Email }">
<apex:facet name="header">
<apex:commandLink action="{! sortByOwnerEmail }"
reRender="accounts_list">Owner Email
</apex:commandLink>
</apex:facet>
</apex:column></apex:pageBlockTable>
<!-- Pagination -->
<table style="width: 100%"><tr>
<td>
Page: <apex:outputText value=" {!PageNumber} of {! CEILING(ResultSize / PageSize) }"/>
</td>
<td align="center">
<!-- Previous page -->
<!-- active -->
<apex:commandLink action="{! Previous }" value="« Previous"
rendered="{! HasPrevious }"/>
<!-- inactive (no earlier pages) -->
<apex:outputText style="color: #ccc;" value="« Previous"
rendered="{! NOT(HasPrevious) }"/>
<!-- Next page -->
<!-- active -->
<apex:commandLink action="{! Next }" value="Next »"
rendered="{! HasNext }"/>
<!-- inactive (no more pages) -->
<apex:outputText style="color: #ccc;" value="Next »"
rendered="{! NOT(HasNext) }"/>
</td>
<td align="right">
Records per page:
<apex:selectList value="{! PageSize }" size="1">
<apex:selectOption itemValue="25" itemLabel="25"/>
<apex:selectOption itemValue="5" itemLabel="5"/>
<apex:selectOption itemValue="10" itemLabel="10"/>
<apex:selectOption itemValue="50" itemLabel="50"/>
<apex:selectOption itemValue="100" itemLabel="100"/>
<apex:actionSupport event="onchange" reRender="accounts_list"/>
</apex:selectList>
</td>
</tr></table>
</apex:pageBlock>
</apex:form>
Here is my custom controller
public class fakeAccountsListController {
public String sortColumn;
public String orderBy;
public List<Account> results;
public List<Account> getAccountList() {
results = Database.query(
'SELECT Id, Name, Phone, NumberOfEmployees, Associated_Contacts__c, Owner.Email ' +
'FROM Account ' + 'ORDER BY' + sortColumn + '' + orderBy);
return results;
}
public fakeAccountsListController(ApexPages.StandardSetController stdCon){
results = (List<Account>)stdCon.getRecords();
sortColumn = ' Phone';
orderBy = ' DESC';
}
// THESE METHODS ARE FOR ALTERNATING SORT ON CLICK OF FACET. WHEN USING ONLY CUSTOM CONTROLLER WITH VF PAGE
// THEY WORK. WHEN USING STANDARD CONTROLLER WITH CONTROLLER EXTENSION FUNCTIONALITY IS BROKEN.
public void sortByName() {
this.sortColumn = ' Name';
if(orderBy == ' ASC'){
orderBy = ' DESC';
}
else{
orderBy = ' ASC';
}
}
public void sortByPhone() {
if(orderBy == ' ASC'){
this.sortColumn = 'Phone';
orderBy = ' DESC';
}
else{
this.sortColumn = 'Phone';
orderBy = ' ASC'; }
}
public void sortByNumEmpl() {
this.sortColumn = 'NumberOfEmployees';
if(orderBy == ' ASC'){
orderBy = ' DESC';
}
else{
orderBy = ' ASC';}
}
public void sortByAssCons() {
this.sortColumn = 'Associated_Contacts__c';
if(orderBy == ' ASC'){
orderBy = ' DESC';
}
else{
orderBy = ' ASC'; }
}
public void sortByOwnerEmail() {
this.sortColumn = 'Owner.Email';
if(orderBy == ' ASC'){
orderBy = ' DESC';
}
else{
orderBy = ' ASC';
}
}
}
sorry the formatting is off inside my code blocks. I was having trouble with the auto formatting on the stackexchange page but code is still readable.. Thank you for any help.