SfC Home > ColdFusion >
Explanation of how to sort a table listing in Macromedia ColdFusion. Also refer to database, SQL, query, ascend, descend, order, variable, <CFIF>, <CFELSE>, tags, IsDefined, URL, Ron Kurtus, School for Champions. Copyright © Restrictions
Sorting a Table Listing in ColdFusion
by Ron Kurtus (5 January 2003)
When you populate a table with items from a database query, using Macromedia ColdFusion, you can list items in ascending or descending order with the ORDER BY command in SQL. But you may also want to allow the user to resort a specific column in the table.
This lesson will answer those questions. There is a mini-quiz near the end of the lesson.
Example of resorting
Name (sort descending) |
|
5 |
Carbohydrates |
4 |
Calorie |
3 |
Buffer solution |
2 |
British thermal unit |
1 |
Breaker |
The table above has been populated from a database query. If you click on the Number item in the heading, you can resort the listings. Clicking on Word will resort in a descending order.
Code
The code for sorting the table list is relatively simple. Clicking on a heading item sends a variable to a set of <CFIF>... <CFELSE>... </CFIF> CFML tags on the same page, where the initial query and the sorting query exist. Then the table is populated from <CFOUTPUT>.
Query
The <CFIF> tag shows its contents if the url.sorter variable has been sent.
<CFIF IsDefined("url.sorter")>
Then the query will be ordered by the value of url.sorter that is sent.
<CFQUERY NAME="resort" DATASOURCE="xxx">
SELECT numb, name FROM Table_Name
ORDER BY #url.sorter#
</CFQUERY>
If the variable has not been sent (like in the initial view of the page), we see the other query.
<CFELSE>
<CFQUERY NAME="resort" DATASOURCE="xxx">
SELECT numb, name FROM Table_Name
</CFQUERY>
</CFIF>
Table
The variable sorter is sent by clicking on a link. The result of sending the first variable will result in an ascending sort. The second variable sorter=word DESC will provide a descending sort.
<TABLE>
<TR>
<TD><A HREF="this_page.cfm?sorter=numb">Number</A></TD>
<TD><A HREF="this_page.cfm?sorter=word DESC">Name</A></TD>
</TR>
<CFOUTPUT QUERY="resort">
<TR>
<TD><P>#numb#</P></TD>
<TD><P>#name#</P></TD>
</TR>
</CFOUTPUT>
</TABLE>
Summary
You can populate a table with items from a database query, and then sort according selected items in the heading using simple ColdFusion code.
Keep code simple
Resources
The following resources provide information on this subject.
Websites
Books
Mini-quiz to check your understanding
1. What is ColdFusion Studio?
2. What is needed to display ColdFusion pages on the Web?
3. What is a major application of ColdFusion?
If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again.
What do you think?
Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible.
Share link
Feel free to establish a link from your website to pages in this site.
Or use our form to send this link to yourself or a friend.
Students and researchers
The Web address of this page is
www.school-for-champions.com/coldfusion/sort_table.htm.
Please include it as a reference in your report, document, or thesis.
Where can you go from here?
Sorting a Table Listing in ColdFusion
