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
Sorcerer7Sorcerer7 

Sorting a pageBlocktable in Visualforce

Good Morning,
I am having a very difficult time finding a simple solution to this.
I have created a VF page that displays record using a "pageBlocktable". The only issue is that the results that are displayed in on the VF page are not "sorted" correctly. Here's what the results on the VF page look like...

User-added image
As you can see, the recdords need to be sorted based on the "Course Code"...
The code for the VF page look slike this...

<apex:page standardController="Training_Certification__c" recordSetVar="Training_Certification__c" sidebar="false">
   <h1> Welcome to Guidance Software's Sales Training and Certification Courses </h1>
            <apex:pageBlock title="Guidance Software Sales Training and Certification Videos">
                   <apex:pageBlockTable value="{!Training_Certification__c}" var="a">
                            <apex:column value="{!a.Course_Code_v2__c}"/>
                            <apex:column value="{!a.Course_Level__c}"/>
                            <apex:column value="{!a.Course_Description__c}"/>
                    </apex:pageBlockTable>
            </apex:pageBlock>
</apex:page>

Is there some simple way to do this? I have searched the VF Developers guide and found nothing helpfull.
Thank you
Amit Chaudhary 8Amit Chaudhary 8
Please check below Post. I hope that will help you
http://salesforcesource.blogspot.in/2008/11/adding-sorting-capability-to.html
https://www.sundoginteractive.com/blog/a-recipe-for-column-sorting-salesforce-visualforce-page
http://blogforce9.blogspot.in/2012/08/sort-able-pageblock-table-component-for_23.html
https://force201.wordpress.com/2013/08/17/client-side-sorting-and-pagination-of-an-apexpageblocktable/
 
<apex:page controller="PageBlockTableSortingCon" tabStyle="Account">
<apex:sectionHeader title="Accounts List with Sorting"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="" id="pageBlock">
    <apex:pageBlockButtons location="top">
      <apex:commandButton value="View" action="{!ViewData}" id="theButton" rerender="pageBlock"></apex:commandButton>
    </apex:pageBlockButtons>
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlockTable value="{!accounts}" var="a" rendered="{!NOT(ISNULL(accounts))}">
       <apex:column>
         <apex:facet name="header">   
           <apex:commandLink action="{!ViewData}" value="Account Name{!IF(sortExpression=='name',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort">
             <apex:param value="name" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
         <apex:outputLink value="/{!a.Id}" target="_blank">{!a.Name}</apex:outputLink>
       </apex:column>
       <apex:column value="{!a.Phone}">
         <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Phone{!IF(sortExpression=='Phone',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="Phone" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
        <apex:column value="{!a.BillingCity}">
          <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Billing City{!IF(sortExpression=='BillingCity',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="BillingCity" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
        <apex:column value="{!a.BillingCountry}">
          <apex:facet name="header">
           <apex:commandLink action="{!ViewData}" value="Billing Country{!IF(sortExpression=='BillingCountry',IF(sortDirection='ASC','▼','▲'),'')}">
             <apex:param value="BillingCountry" name="column" assignTo="{!sortExpression}" ></apex:param>
           </apex:commandLink>
         </apex:facet>
       </apex:column>
     
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
public class PageBlockTableSortingCon {
 
   private List<Account> accounts;
   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 List<Account> getAccounts() {
       return accounts;
   }


   public PageReference ViewData() {
       //build the full sort expression
       string sortFullExp = sortExpression  + ' ' + sortDirection;
      
       //query the database based on the sort expression
       accounts = Database.query('Select id, Name, BillingCity, BillingCountry, Phone from Account order by ' + sortFullExp + ' limit 1000');
       return null;
   }

}
Please let us know if this will help you..
Thanks
Amit Chaudhary


 
Sorcerer7Sorcerer7
Thanks Amit,
I wanted to ask if all that code is necessary? I would like to sort the list on just that first column of the table. Can I just change out the field "phone" for the "course code" from my custom object, and then omit the code that filters on the other fields (billing city and country)?
Thanks
Amit Chaudhary 8Amit Chaudhary 8
If you are fatching date from Query then you can use order by other wise plz try above code. That will help you to sort code by any column
Sorcerer7Sorcerer7
Hi Amit,
OK, I created an APEX Class. Here is the code...

public class PageBlockTableSortingCon {        
     private List<Training_Certification__c> Training_Certification;        
      private String sortDirection = 'ASC';       
      private String sortExp = 'Course_Code_v2__c';             
       public String sortExpression      
           { 
             get
                        { 
                            return sortExp;  }
            set
                       {    if (value == sortExp)  
                         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';  else sortDirection = 'ASC';   sortExp = value; 
                   }
                 }
    public String getSortDirection()      {
         if (sortExpression == null || sortExpression == '') 
               return 'ASC';  else  return sortDirection;      } 
    public void setSortDirection(String value)      { 
              sortDirection = value;    }
     public List<Training_Certification__c> getTraining_Certification() {
               return Training_Certification;  }
     public PageReference ViewData() {
                string sortFullExp = sortExpression  + ' ' + sortDirection;                          Training_Certification = Database.query('Select id, Name, Course_Code_v2__c, Course_Level__c, Course_Description__c from Training_Certification__c order by ' + sortFullExp + ' limit 1000');            return null;        }     }

This Class saved good.
Then I went back and changed the code on the page to this...

<apex:page controller="PageBlockTableSortingCon" tabStyle="Account"  sidebar="false">
    <apex:sectionHeader title="Welcome to Guidance Software's Sales Training and Certification Courses"></apex:sectionHeader>
         <apex:form >
               <apex:pageBlock title="" id="pageBlock">
                    <apex:pageBlockButtons location="top">
                     <apex:commandButton value="View" action="{!ViewData}" id="theButton" rerender="pageBlock"></apex:commandButton>
                      </apex:pageBlockButtons>
                      <apex:pageMessages ></apex:pageMessages>
     
            <apex:pageBlockTable value="{!Training_Certification__c}" var="a" rendered="{!NOT(ISNULL(Training_Certification))}">
                 <apex:column>
                      <apex:facet name="header">  
                          <apex:commandLink action="{!ViewData}"  value="Course_Code_v2__c{!IF(sortExpression=='Course_Code_v2__c',IF(sortDirection='ASC','▼','▲'),'')}" id="cmdSort">
                 <apex:param value="name" name="column" assignTo="{!sortExpression}" ></apex:param>
               </apex:commandLink>
             </apex:facet>
           </apex:column>
        <apex:column value="{!a.Course_Level__c}">

             <apex:facet name="header">
               <apex:commandLink action="{!ViewData}" value="Course_Level__c{!IF(sortExpression=='Course_Level__c',IF(sortDirection='ASC','▼','▲'),'')}">
                 <apex:param value="Course_Level__c" name="column" assignTo="{!sortExpression}" ></apex:param>
               </apex:commandLink>
             </apex:facet>
           </apex:column>
            <apex:column value="{!a.Course_Description__c}">

              <apex:facet name="header">
               <apex:commandLink action="{!ViewData}" value="Course_Description__c{!IF(sortExpression=='Course_Description__c',IF(sortDirection='ASC','▼','▲'),'')}">
                 <apex:param value="Course_Description__c" name="column" assignTo="{!sortExpression}" ></apex:param>
               </apex:commandLink>
             </apex:facet>
           </apex:column>

       </apex:pageBlockTable>

    </apex:pageBlock>

    </apex:form>

</apex:page>

I am getting this error.....

User-added image

I'm missing something. Suggestions...

Thank you
Shawn
Amit Chaudhary 8Amit Chaudhary 8
Hi Shawn,

Try below code

<apex:pageBlockTable value="{!Training_Certification}" var="a" rendered="{!NOT(ISNULL(Training_Certification))}">

Please let us know if this will help you
Thanks
Amit Chaudhary
 
Sorcerer7Sorcerer7
Hi Amit,
Well, the good thing is that the longer I play with this code, the more I understand what I am writing and what it is doing.
I made the change you suggested and it saved great. When I went to see the Preview, here is what I get...
No data, and just a button that says "View".

User-added image

If I click "View" I get this...
1. No Data in the Course Code column (Red Box)
2. The Column names have the "__c" after them (should just be Course Code, Course Level, Course Description) (Green Box)
3. There is the littel "sort" arrow (Blue arrow)

User-added image

If I click the sort arrow, the arrangement does change, but it now looks like this (I am assuming becasiue there is no data in  Course Code field, the sort is not working correctly.)  If it matters, the Course_Code_v2__c field is a formula field that generates the code based on three other fields)

User-added image

The order is still not correct, "Getting Started" is in the midle of the list and the Final Exam is also mid list. (Red Arrows)
The Introductory and Intermediate courses are mixing (Blue Box).
Again, this might be due the fact there is no data displayed in the Course_Code field.

I have been playing arund with it, trying different things, but I keep getting errors, which is great for my learning process, but not good when I must get this "live" today on our Portal Page.

Thank You so very much Amit, this discussion has really helped me out a tremendous amount in my learing curve.
Thanks
Shawn