Infinite entities, cfgrid and one cfc to handle the data
March 10, 2010
While writing SpreadEdit I wanted every entity to be editable via cfgrid. In particular I thought it would be cool to have one cfc on the back end to process the data from cfgrid no matter what entity it was working with. With ColdFusion 9 and ORM this proved possible and is pretty cool, check out the screencast. Code the other side of the embed.
Here is the code for genericGrid.cfc:
<cfcomponent>
<cffunction name = "getData" access = "remote" returnformat = "JSON" >
<cfargument name = "page" required = "true" >
<cfargument name = "pageSize" required = "true" >
<cfargument name = "gridSortColumn" required = "true" >
<cfargument name = "gridSortDirection" required = "true" >
<cfargument name = "entity" required = "true" >
<cfif ! len ( arguments.gridSortColumn ) >
<cfset arguments.gridSortColumn = "1" >
</cfif>
<cfset local.getTasks = ormExecuteQuery ( " FROM #arguments.entity# ORDER BY #arguments.gridSortColumn# #arguments.gridSortDirection#" ) >
<cfreturn queryconvertforgrid ( entityToQuery ( local.getTasks ) , Arguments.page, Arguments.pageSize) >
</cffunction>
<cffunction name = "setData" access = "remote" >
<cfargument name = "action" required = "true" >
<cfargument name = "row" required = "true" >
<cfargument name = "changed" required = "true" >
<cfargument name = "entity" required = "true" >
<cfif arguments.action eq "U" >
<cfset local.obj = entityLoad ( arguments.entity, arguments.row, true ) >
<cfloop collection = "#arguments.changed#" item = "local.key" >
<cfinvoke component = "#local.obj#" method = "set#local.key#" >
<cfinvokeargument name = "#local.key#" value = "#arguments.changed[ local.key ]#" >
</cfinvoke>
</cfloop>
<cfelseif arguments.action eq "I" >
<cfset local.obj = entityNew ( arguments.entity ) >
<cfloop collection = "#arguments.row#" item = "local.key" >
<cfif local.key neq "CFGRIDROWINDEX" >
<cfinvoke component = "#local.obj#" method = "set#local.key#" >
<cfinvokeargument name = "#local.key#" value = "#cleaned( arguments.row[ local.key ] )#" >
</cfinvoke>
</cfif>
</cfloop>
</cfif>
<cfset entitySave ( local.obj ) >
</cffunction>
<cffunction name = "cleaned" access = "private" >
<cfargument name = "string" required = "true" >
<cfset var ret = arguments.string>
<!--- check for date --->
<cfif reFind ( "[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}" ,
ret ) >
<cfset ret = rereplaceNoCase ( ret, "T" , " " ) >
</cfif>
<cfreturn ret>
</cffunction>
</cfcomponent>
No Comments
Some comments have been lost over the years due to moving hosts.