Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Alternate Sorting

Click column heading to sort, click again to sort in other direction. by TruthInSatire
Posted: 14 Feb 05 (Edited 15 Feb 05)

How do you click on a column heading to change the sort order of the report, then click again to sort the other direction?

This is a pretty common problem, I think everyone has run into it at least once.  It's pretty simple to make a report sortable by clicking a heading.  Just pass a url variable with the column you want to sort on.  It's the clicking again to reverse the sort order that gets tricky.  This cfscript will help do that.  Like all my other FAQ's there isn't that much code and a ton of comments.  I try to highlight the actual code.


<!--- set the default column to sort by. here ours is called Lname --->
<cfparam name = "session.lastCol" default = "Lname">
    function changeOrder(colName){

         /*If this is the first time the page is loaded we have to be sure to change the default column to "DESC".  Otherwise the first time a user clicks the link to sort it the other direction nothing will happen.*/
        if(not isdefined("url.sortCol")){
            /*If the default column name and the name passed to the function are the same set the next sort order to "DESC"*/
            if(session.lastCol eq colName){
                newOrder = "DESC";
                newOrder = "ASC";

            /*return the query string that will sort the query. In this example the return may look like sortCol=Lname&sortOrder=DESC*/
            return "sortCol=" & colName & "&sortOrder=" & newOrder;

            /*Check to see if the user is clicking on the same column to change the sort order.*/
            if((session.lastCol eq url.sortCol) AND (colName eq url.sortCol)){
                /*change the sort order in the link*/
                if(url.sortOrder eq "ASC"){
                    newOrder = "DESC";
                    newOrder = "ASC";
                return "sortCol=" & colName & "&sortOrder=" & newOrder;

                /*if the user clicked a different heading to sort by we must change the last sort column held in the session variable and change the next sort order to DESC if the column is not a clicked column the sort order will go back to ASC*/
                if(colName eq url.sortCol){
                    session.lastCol = colName;
                    newOrder = "DESC";
                    newOrder = "ASC";
                return "sortCol=" & colName & "&sortOrder=" & newOrder;

<!--- Sample query--->
    SELECT        Lname, title, empNumber
    FROM        myTable

            <!--- if this is the first time the page loads it will sort on the column you set as the default in ASC order.  If the url variable exists it will sort by them instead. --->
                <cfif isdefined("url.sortCol")>
                    #url.sortCol# #url.sortOrder#
                    #session.lastCol# ASC

Here we have 3 columns, Lname, positionTitle, and EmpNumber but you can have any number of columns with any valid name.

  <td><a href = "yourpage.cfm?#changeOrder('Lname')#">Name</a></td>
  <td><a href = "yourpage.cfm?#changeOrder('positionTitle')#">Title</a></td>
  <td><a href = "yourpage.cfm?#changeOrder('EmpNumber')#">Employee Number</a></td>

Back to Adobe FAQ Index
Back to Adobe Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close