Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have learned more through this forum than I did on a two day course. Thanks to everyone for their help and other postings that I have found useful..."

Geography

Where in the world do Tek-Tips members come from?

Adobe: ColdFusion FAQ

Alternate Sorting

Click column heading to sort, click again to sort in other direction.
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.

CODE

<!--- set the default column to sort by. here ours is called Lname --->
<cfparam name = "session.lastCol" default = "Lname">
<cfscript>
    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";
            }else{
                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;
        }else{

            /*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";
                }else{
                    newOrder = "ASC";
                }
                return "sortCol=" & colName & "&sortOrder=" & newOrder;
            }else{

                /*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";
                }else{
                    newOrder = "ASC";
                }
                return "sortCol=" & colName & "&sortOrder=" & newOrder;
            }
        }
    }
</cfscript>

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

            <!--- 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#
                <cfelse>
                    #session.lastCol# ASC
                </cfif>
</cfquery>


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

<cfoutput>
<tr>
  <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>
</tr>
</cfoutput>

Back to Adobe: ColdFusion FAQ Index
Back to Adobe: ColdFusion 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