Created
August 3, 2015 02:36
-
-
Save kevindb/dee8f50a29541aa67841 to your computer and use it in GitHub Desktop.
Looping Over ColdFusion JSON Queries In jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*--------------------------------------------------------------------------------------- | |
Blog Entry: | |
Looping Over ColdFusion JSON Queries In jQuery | |
Author: | |
Ben Nadel / Kinky Solutions | |
Link: | |
http://www.bennadel.com/index.cfm?event=blog.view&id=1755 | |
Date Posted: | |
Nov 16, 2009 at 9:45 AM | |
---------------------------------------------------------------------------------------*/ | |
(function( $ ){ | |
// Create a name space for ColdFusion related functionality. | |
jQuery.coldfusion = {}; | |
// Create a function that will iterate over each row of the | |
// serialized JSON query. This iteration can handle three | |
// types of query structure: | |
// | |
// - Default from SerializeJSON() | |
// - WDDX Compatible from SerializeJSON() | |
// - Array of structs. | |
jQuery.coldfusion.eachRow = function( query, callback ){ | |
// Check to see which type of iterator we are going to | |
// use when looping over this query. | |
if ($.isArray( query )){ | |
// This is an array of structs. | |
jQuery.coldfusion.eachRow.arrayIterator( | |
query, | |
callback | |
); | |
} else if ("ROWCOUNT" in query){ | |
// This is the WDDX-compatible format. | |
jQuery.coldfusion.eachRow.wddxIterator( | |
query, | |
callback | |
); | |
} else { | |
// This is the default format. | |
jQuery.coldfusion.eachRow.defaultIterator( | |
query, | |
callback | |
); | |
} | |
// Return the jQuery library. | |
return( this ); | |
}; | |
// Define a cfquery loop iteration method that can handle the | |
// default SerializeJSON() method. | |
jQuery.coldfusion.eachRow.defaultIterator = function( query, callback ){ | |
var i = 0; | |
// Loop over the data array. | |
for (var i = 0 ; i < query.DATA.length ; i++){ | |
(function( rowIndex ){ | |
var row = {}; | |
// Loop over the column names to create the data | |
// collection as column-value pairs. | |
$.each( | |
query.COLUMNS, | |
function( index, column ){ | |
row[ column ] = query.DATA[ rowIndex ][ index ]; | |
} | |
); | |
// Execute the callback method in the context of | |
// the row data. | |
callback.call( row, rowIndex, row ); | |
})( i ); | |
} | |
}; | |
// Define a cfquery loop iteration method that can handle the | |
// SerializeJSON() method that returns WDDX-compatible data. | |
jQuery.coldfusion.eachRow.wddxIterator = function( query, callback ){ | |
var i = 0; | |
// Loop over the records. | |
for (var i = 0 ; i < query.ROWCOUNT ; i++){ | |
(function( rowIndex ){ | |
var row = {}; | |
// Loop over the column names to create the data | |
// collection as column-value pairs. | |
$.each( | |
query.DATA, | |
function( column, values ){ | |
row[ column.toUpperCase() ] = values[ rowIndex ]; | |
} | |
); | |
// Execute the callback method in the context of | |
// the row data. | |
callback.call( row, rowIndex, row ); | |
})( i ); | |
} | |
}; | |
// Define a cfquery loop iteration method that can handle the | |
// query as an array of structs. | |
jQuery.coldfusion.eachRow.arrayIterator = function( query, callback ){ | |
var i = 0; | |
// Loop over the records. | |
for (var i = 0 ; i < query.length ; i++){ | |
// Execute the callback method in the context of | |
// the row data. | |
callback.call( query[ i ], i, query[ i ] ); | |
} | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment