You need to sign in to do that
Don't have an account?
Sorcerer7
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...
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
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...
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
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/
Please let us know if this will help you..
Thanks
Amit Chaudhary
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
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.....
I'm missing something. Suggestions...
Thank you
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
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".
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)
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)
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