Solved

Coldfusion  and MS SQL Order By and Save Sort Order

Posted on 2008-06-18
3
324 Views
Last Modified: 2009-08-07
I have some Coldfusion code that outputs a directory. My challenge is to have three options to sort by. I have a integer field sortorder in the table. It already has a value on how to sort the records. I have a javascript drop down with the three options to sort by. All sort by options are fields in the table. Two of the fields are varchar the names are DIVISION and DEPT. The third is a integer field SORTORDER. When I sort by DIVISION I want it to change the SORTORDER integer field in ascending number order no matter what sortorder number was given when the record was added.
0
Comment
Question by:smares323
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:adobe116
ID: 21818942
Hey, if you are still having this problem, can you post your code?  It would make it easier to answer I believe.  For instance, how are you displaying the grid (table)?  Are you using cfgrid?  Are you using client-side sorting or server-side, etc..

If you want a quick solution, check out cfgrid.  Attached is the sample code (modified a bit) from the CFDocs.  Or else http://developer.yahoo.com/yui/datatable/ is good if you know a bit of js.  Otherwise if you post your code I can help more..
    <cfgrid 
        name = "FirstGrid" 
        width = "500"
        query = "DIRECTORY_VARIABLE_HERE" colheaderbold="Yes"
        rowHeaders = "No" >
        
        <!--- cfgridcolumn tags arrange the table and control the display. --->
        <!--- select="No" does not seem to have any effect!!! --->
        <cfgridcolumn name = "VARIABLE_NAMES_HERE" header = "Department" Select="No" width="75"
            textcolor="blue" bold="Yes">
        <cfgridcolumn name = "CorNumber" header = "Course ##" Select="No" width="65">
        <cfgridcolumn name = "CorName" header = "Name" width="125">
        <cfgridcolumn name = "CorLevel" header = "Level" width="85">
        <cfgridcolumn name = "CorDesc" header = "Description" width="125">
    </cfgrid>

Open in new window

0
 

Author Comment

by:smares323
ID: 21819077
We are running Coldfusion MX7. Does the cfgrid tag work with Coldfusion mx7
0
 
LVL 2

Accepted Solution

by:
adobe116 earned 500 total points
ID: 21819200
unfortunately not in html format.  Does Flash though... The cf AJAX scripts were all based on a real early version of yui.
If you 've never checked out the yui library, it's pretty cool.  I used it to output a qry, allow the user to edit each field, and then pass the data back and it didn't take long at all.

here is their basic example:
http://developer.yahoo.com/yui/examples/datatable/dt_basic.html
The data for the table is here (you only need the small bookOrders part):
http://developer.yahoo.com/yui/examples/datatable/assets/js/data.js

Your code would be something like what I have below..

Believe me, if it looks confusing it is really not.  If you don't know scripting or OO languages it can be freaky... but you can just look at the <b>key:"varName"</b> fields and substitute your variables.  And then remove formatters if you don't want them (but you probably will).

If you know js and are familiar with yui, I also combined this example in the code:
http://developer.yahoo.com/yui/examples/datatable/dt_clientsorting.html
It sorts on state, then areacode.

Best of luck, sorry I couldn't be more of a help.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
 
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/datatable/assets/skins/sam/datatable.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/dragdrop/dragdrop-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/element/element-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/datasource/datasource-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/datatable/datatable-beta-min.js"></script>
 
</head>
 
<body class=" yui-skin-sam">
 
<div id="basic"></div>
 
<script type="text/javascript">
YAHOO.example.Data = {
    bookorders: [
        <!--- STATIC DATA (COMMENTED OUT BUT KEPT SO YOU CAN SEE WHAT WOULD HAPPEN)
        {id:"po-0167", date:new Date(1980, 2, 24), quantity:1, amount:4, title:"A Book About Nothing"},
        {id:"po-0783", date:new Date("January 3, 1983"), quantity:null, amount:12.12345, title:"The Meaning of Life"},
        {id:"po-0297", date:new Date(1978, 11, 12), quantity:12, amount:1.25, title:"This Book Was Meant to Be Read Aloud"},
        {id:"po-1482", date:new Date("March 11, 1985"), quantity:6, amount:3.5, title:"Read Me Twice"}
         --->
         <!--- YOUR DATA (the extra comma at the end of every loop is ok)--->
         <CFLOOP query name="yourQry">
         {id:"po-0167", date:new Date(1980, 2, 24), quantity:1, amount:4, title:"A Book About Nothing"},
         </cfloop>
    ]
};
    
YAHOO.util.Event.addListener(window, "load", function() {
    YAHOO.example.Basic = new function() {
        var myColumnDefs = [
            <!--- THIS COL IS RESIZABLE AND SORTABLE --->
            {key:"id", sortable:true, resizeable:true},
            <!--- THIS COL HAS THE DATE FORMATTED USING THE YUI DATE FORMATTER --->
            <!--- THE SORTOPTIONS VARIABLE INDICATES THAT WE SHOULD SORT THE QTY FIELD AFTER THE DATE FIELD (like you wanted) --->
            {key:"date", formatter:YAHOO.widget.DataTable.formatDate, sortable:true, sortOptions:{sortFunction:this.sortDateThenQty},resizeable:true},
            {key:"quantity", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true, resizeable:true},
            {key:"amount", formatter:YAHOO.widget.DataTable.formatCurrency, sortable:true, resizeable:true},
            {key:"title", sortable:true, resizeable:true}
        ];
 
        this.myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.bookorders);
        this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
        this.myDataSource.responseSchema = {
            fields: ["id","date","quantity","amount","title"]
        };
        
        this.sortDateThenQty = function(a, b, desc) {
            // Deal with empty values
            if(!YAHOO.lang.isValue(a)) { return (!YAHOO.lang.isValue(b)) ? 0 : 1; }
            else if(!YAHOO.lang.isValue(b)) { return -1; }
 
            // First compare by Date
            var comp = YAHOO.util.Sort.compare;
            var compDate = comp(a.getData("date"), b.getData("date"), desc);
 
            // If dates are equal, then compare by Qty
            return (compDate !== 0) ? compDate : comp(a.getData("quantity"), b.getData("quantity"), desc);
        };
        
        this.myDataTable = new YAHOO.widget.DataTable("basic",
                myColumnDefs, this.myDataSource, {caption:"DataTable Caption"});
    };
});
</script>
 
 
</body>
</html>

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question