You need to sign in to do that
Don't have an account?
sorting pageBlockTable
Hi Everyone,
I have seen this wiki article http://wiki.developerforce.com/index.php/Sorting_Tables. I implemented this exactly in the sandbox and the code works absolutely fine.
Now, I have tried to adapt this to a different apex class / visualforce page, but somehow the sorting doesnt work when I put the doSort function into an extesion class. see below:
Visualforce Page
<apex:page standardController="Account" extensions="testContact"> <apex:form > <apex:pageBlock > <apex:pageBlockTable value="{!cntz}" var="o" id="table"> <apex:column > <apex:facet name="header"> <apex:commandLink value="{!$ObjectType.Contact.Fields.Name.Label}" action="{!doSort}" rerender="table"> <apex:param name="sortField" value="Name" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:inputField value="{!o.name}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
And here is the apex class which is the extension to the Accoun standardController in the visualforce page above:
public class testContact { private List<Contact> cntz; private Account acct; public String sortField {get; set;} public String previousSortField {get; set;} public testContact(ApexPages.StandardController controller) { this.acct= (Account)controller.getRecord(); } public Account getAt() { Account[] at = [Select id,name FROM Account where id= :acct.id]; if(at.size()>0){ return at[0]; } else{ return null; } } public List<Contact> getCntz() { Account act = [Select id FROM Account where id = :acct.id]; cntz = [Select id, Name, Client_Potential__c, title, Address_City__c, Direct_Phone__c, Email, Days_Since_Last_Contact__c, Owner.alias, Divisional_Account__c from Contact where ((account.parentid = :act.id OR account.id = :act.id)AND LastName!='Budget')]; return cntz; } public void UpdateRecords() { // this simple line of code finds out which column was changed and update the // relevant account record accordingly! update cntz; } public void doSort(){ String order = 'asc'; /*This checks to see if the same header was click two times in a row, if so it switches the order.*/ if(previousSortField == sortField){ order = 'desc'; previousSortField = null; }else{ previousSortField = sortField; } //To sort the table we simply need to use this one line, nice! superSort.sortList(cntz,sortField,order); } }
The main purpose of this class is to show all children contacts of a parent account (even if they are attached to a child account of a prent account). Now once I have this list, I would like to sort it (which is why I used the sorting funciton), but somehow this doesnt work. No errors in testing, but just doesnt work as expected!
Any help would be much appreciated.
Thanks,
Pranav
I think this is because your getCntz method is always retrieving the list of contacts anew, and thus they will appear in the order they are retrieved from the database.
If you change this method to only retrieve the data from the database if the list is null (which it will be first time through only), then you will return the sorted values.
Bob,
you were absolutely right, and that seems to have solved the sortng problem. However, now I was expanding my page, and came across another problem.
I have a lookup field on contact called Divisional Account (looking up accounts). I want to be able to sort based on this, but when I try that I get an error - Developer Script Exception - superSort : Attempt to de-reference a null object
The code where this error is created in superSort is an if statement that tries tofigure out the type of the sortField:
Now, my VF page looks like:
Any help will be much appreciated.
This smacks that Divisional_Account__c is not recognised as a field on the contact, but I'd expect there to be errors when saving the page if that were the case.
Try adding some debug around this line:
e.g.
and try to find where the result of one of the nested method calls returns null.
When I was trying to sort the datatable, it sorts fine but the resulting sort order is based on the ASCII values. Is there a way to sort normally