With a little side of applesauce...

Monday, September 27, 2010

Coldfusion - cfajaxproxy and natural sorting

I couldn't seem to get natural sort working when I using a bind on my CFSELECT. To get around it, I just passed the query object back as JSON to Javascript, and used this nifty Javascript naturalSort.js by Jim Palmer.

CFC:
    <cffunction name="getMeasurementBySourceID"
returntype="query"
returnformat="json"
access="remote"
output="no"
hint="get a Source's associated Measurement by SourceID from the ION_EEMData view vwSMP_SourceMeasurementPairs
@return query ">
...


Javascript:
<cfajaxproxy cfc="#Application.cfcPath#.Enviromon" jsclassname="e">
...

function getMeasurementBySourceID( sourceid ){
var inst = new e();
var result = inst.getMeasurementBySourceID( sourceid );

return result;
}

function getNodeList( nodefield, sourceid, nodeid ) {
var orig_nodes = getMeasurementBySourceID( sourceid );
var options = '';
var nodes = orig_nodes.DATA.sort(naturalSort);
for (var i=0; i<nodes.length; i++) {
if (nodes[i][1] == nodeid) {
options += '<option value="' + nodes[i][1] + '" selected="selected">' + nodes[i][0] + '</option>';
} else {
options += '<option value="' + nodes[i][1] + '">' + nodes[i][0] + '</option>';
}
}
if (nodefield == 1) {
$("select#nodelist").html(options);
} else {
$("select#nodelist2").html(options);
}
}
...


'nodefield' allows me to reuse this code for multiple nodes.

Here is a link to Jim's blog:
http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support/

No comments: