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
AlanisticAlanistic 

Sorting a page block table

Hi,

 

I have created a very basic custom object called "News".  It uses a standard controller.  I have a page that displays a headline, the owner and the created date.  My visualforce code is:

 

<apex:pageBlockTable value="{!News}" var="n">
    <apex:column headerValue="Recent Posts"><br/><font size="5"><apex:outputLink value="/apex/NewsItem?id={!n.id}">{!n.Name}</apex:outputLink></font><br/><font size="1"><i>Posted by {!n.Owner.Name on {!n.CreatedDate}.</i></font><br/><br/>{!n.Preview_Text__c}<br/><br/><apex:outputLink value="/apex/NewsItem?id={!n.id}">Read More >> </apex:outputLink><br/><br/></apex:column>
    </apex:pageBlockTable>

 

How would I change this to sort by the created date by default?  I don't want the users to be able to change the order, I just want to code it to sort so that the newest records appear first.

Jake GmerekJake Gmerek

I'm thinking that you probably have two choices:

 

1.  Switch to a custom controller or extension, query your records in a specific order and return them to the page that way.

 

2.  Use JQuery or a similar JS library to sort the table on page load after the data is returned to the page.

 

I am not sure which way would be more performant and it would probably vary based on the number of records displayed, so I would probably go down the path that you are most comfortable with.

AlanisticAlanistic

I'm new to working with visualforce and apex so ideally I'd like to avoid the custom controller for the moment.

 

Do you have any details on how I could use JQuery to accomplish this?

Jake GmerekJake Gmerek

I am actually fairly weak with JQuery and do not know how to do it off hand.  A quick Google search turns up some promising results:

 

https://www.google.com/search?q=jquery+to+sort+a+table&oq=jquery+to+sort+a+table&aqs=chrome..69i57j69i61l2j69i60j0l2.4069j0&sourceid=chrome&espvd=210&es_sm=93&ie=UTF-8

 

An article on that page should help you.  I do know that you will need to render your VF page and use an inspection tool to find the names and ids that you need in order to accomplish this.  Hope that this helps.