QueryToStore to transform a coldfusion query to an array suitable for ExtJS Store
Tuesday, 24. November 2009, 09:49:57
Temp=deserializeJSON(serializeJSON(MyQuery)); MyStore=MyQuery.data;
That gives me exactly what I need, But then, the "not so great because it alters your data without asking and it would have been nice to be able to tell him not to" serializeJSON() turn your postal codes and boleans into numbers, and your dates into localized user friendly sentences, which is realy not optimal when you try to send pure data to ExtJS!
That's why I created this function, which I'm using in combination with encode(), the old JSON custom function available arround who don't mess with your data types.
<cffunction name="QueryToStore" access="public" output="false"
hint="This turns a query into an array of arrays, first one with columns, second with arrays of data, suitable for ExtJS Store">
<!--- Define arguments. - -->
<cfargument name="Data" type="query" required="yes" />
<cfscript>
// Define the local scope.
var LOCAL = StructNew();
// Get the column names as an array.
LOCAL.Columns = ListToArray( ARGUMENTS.Data.ColumnList );
// Create a structure that will hold the Store.
LOCAL.Store.columns = LOCAL.Columns;
// Create a second array for the data
LOCAL.DataArray = ArrayNew(1);
// Loop over the query.
for (LOCAL.RowIndex = 1 ; LOCAL.RowIndex LTE ARGUMENTS.Data.RecordCount ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
// Create an array for this row
LOCAL.Row = ArrayNew(1);
// Loop over the columns in this row.
for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen( LOCAL.Columns ) ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
// Get a reference to the query column.
LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];
// Store the query cell value into the array by key.
LOCAL.Row[ LOCAL.ColumnIndex ] = ARGUMENTS.Data[ LOCAL.ColumnName ][ LOCAL.RowIndex ];
}
// Add the row array to the data array.
ArrayAppend( LOCAL.DataArray, LOCAL.Row );
}
// Add the Data array to the query array
LOCAL.Store.data = LOCAL.DataArray ;
// Return the store
return( LOCAL.Store );
</cfscript>
</cffunction>












How to use Quote function: