转眼2个多星期过去了,学到的还只是些皮毛,今天本来想自己写一个CRUD练练,写起来还真是费劲啊,跟当初学java的时候有一拼,咳,上网找吧,发现一个标签cfgrid,接着搜到了好多的有关的代码(都是老外写的,中文的资料啥时能多一点啊),找到一经典代码:
test.cfm:
<cfwindow initshow="true" center="true" width="430" height="340" title="Artists"> <cfform> <cfgrid name="artists" format="html" pagesize="10" striperows="yes" selectmode="edit" delete="yes" bind="cfc:artists.getArtists({cfgridpage}, {cfgridpagesize}, {cfgridsortcolumn}, {cfgridsortdirection})" οnchange="cfc:artists.editArtist({cfgridaction}, {cfgridrow}, {cfgridchanged})"> <cfgridcolumn name="is" display="false" /> <cfgridcolumn name="lastname" header="Last Name" width="100"/> <cfgridcolumn name="firstname" header="First Name" width="100"/> <cfgridcolumn name="email" header="E-Mail" width="200"/> </cfgrid> </cfform> </cfwindow>
artists.cfc
<cfcomponent output="false"> <cfset THIS.dsn="cfartgallery"> <!--- Get artists ---> <cffunction name="getArtists" access="remote" returntype="struct"> <cfargument name="page" type="numeric" required="yes"> <cfargument name="pageSize" type="numeric" required="yes"> <cfargument name="gridsortcolumn" type="string" required="no" default=""> <cfargument name="gridsortdir" type="string" required="no" default=""> <!--- Local variables ---> <cfset var artists=""> <!--- Get data ---> <cfquery name="artists" datasource="#THIS.dsn#"> SELECT artistid, lastname, firstname, email FROM artists <cfif ARGUMENTS.gridsortcolumn NEQ "" and ARGUMENTS.gridsortdir NEQ ""> ORDER BY #ARGUMENTS.gridsortcolumn# #ARGUMENTS.gridsortdir# </cfif> </cfquery> <!--- And return it as a grid structure ---> <cfreturn QueryConvertForGrid(artists, ARGUMENTS.page, ARGUMENTS.pageSize)> </cffunction> <!--- Edit an artist ---> <cffunction name="editArtist" access="remote"> <cfargument name="gridaction" type="string" required="yes"> <cfargument name="gridrow" type="struct" required="yes"> <cfargument name="gridchanged" type="struct" required="yes"> <!--- Local variables ---> <cfset var colname=""> <cfset var value=""> <!--- Process gridaction ---> <cfswitch expression="#ARGUMENTS.gridaction#"> <!--- Process updates ---> <cfcase value="U"> <!--- Get column name and value ---> <cfset colname=StructKeyList(ARGUMENTS.gridchanged)> <cfset value=ARGUMENTS.gridchanged[colname]> <!--- Perform actual update ---> <cfquery datasource="#THIS.dsn#"> UPDATE artists SET #colname# = '#value#' WHERE artistid = #ARGUMENTS.gridrow.artistid# </cfquery> </cfcase> <!--- Process deletes ---> <cfcase value="D"> <!--- Perform actual delete ---> <cfquery datasource="#THIS.dsn#"> DELETE FROM artists WHERE artistid = #ARGUMENTS.gridrow.artistid# </cfquery> </cfcase> </cfswitch> </cffunction> </cfcomponent>
虽然也有其它的版本,但是这个最权威,因为它是ColdFusion之父Ben Forta写的,呵呵。
原文链接地址: http://www.forta.com/blog/index.cfm/2007/6/25/ColdFusion-Ajax-Tutorial-6-Editable-Data-Grids