Skip navigation.

C'est mon blog

on connait la chanson

QueryToStore to transform a coldfusion query to an array suitable for ExtJS Store

Before I decided to write this function, I found a way arround:

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>

Ma personnalité enfin dévoiléeUn témoignage de plus à propos de Belgacom

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

Download Opera, the fastest and most secure browser
March 2010
M T W T F S S
February 2010April 2010
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31