Blog posts made on 25-Jun-07
The Dreamweaver Team Is Blogging

Title says it all, the Dreamweaver team blog is online.

Read More ›

CFQuickDocs Updated For CF8

Jake Munson has updated his CFQuickDocs to include ColdFusion 8 tags and functions (and even the new CF8 Ajax JavaScript objects).

Read More ›

ColdFusion Ajax Tutorial 6: Editable Data Grids

Previously we looked at the new ColdFusion 8 data grid and how to populate that control using asynchronous calls back to a ColdFusion Component. In that example the CFC contained a single method that returned a page of data as requested by the data grid.

Like the previous incarnations of <CFGRID>, the new Ajax enabled HTML grid allows data to be updated right within the grid. When the <CFGRID> is used in edit mode, column values may be edited as needed, and rows may be deleted. Unfortunately, the current implementation of the HTML <CFGRID> does not support inserting new rows. This is a pretty serious limitation, and one that we'll hopefully address in the future - for now you'll need to use another form to add new rows.

You will recall that <CFGRID> requests data as needed by making calls to a CFC method specified in the bind attribute. To process edits a second CFC method is needed, and it must be passed to the onchange attribute. Here is a modified <CFGRID> that supports data editing:

view plain print about
1<cfwindow initshow="true" center="true"
2            width="430" height="340" title="Artists">

5    <cfgrid name="artists"
6            format="html"
7            pagesize="10"
8            striperows="yes"
9            selectmode="edit"
10            delete="yes"
11            bind="cfc:artists.getArtists({cfgridpage},
12                                        {cfgridpagesize},
13                                        {cfgridsortcolumn},
14                                        {cfgridsortdirection})"

15            onchange="cfc:artists.editArtist({cfgridaction},
16                                            {cfgridrow},
17                                            {cfgridchanged})"

18        <cfgridcolumn name="is" display="false" />
19        <cfgridcolumn name="lastname" header="Last Name" width="100"/>
20        <cfgridcolumn name="firstname" header="First Name" width="100"/>
21        <cfgridcolumn name="email" header="E-Mail" width="200"/>
22    </cfgrid>

There are three changes in this <CFGRID> (compared to the grid created previously). First of all, selectmode="edit" puts the data grid in edit mode. This allows editing, but not deleting. To allow rows to be deleted, delete="yes" is also specified. And finally, a CFC method is specified in the onchange attribute. When invoked (upon an edit or a delete) three arguments will be passed, the action (U for update or D for delete), the row being changed, and the changes (only populated for updates, and not for deletes).

The specified CFC has to accept these three arguments, and returns no data. Within the CFC you can use <CFQUERY> tags (or perform any other operations) to actually perform the updates. Here's an example:

view plain print about
1<!--- Edit an artist --->
2    <cffunction name="editArtist" access="remote">
3        <cfargument name="gridaction" type="string" required="yes">
4        <cfargument name="gridrow" type="struct" required="yes">
5        <cfargument name="gridchanged" type="struct" required="yes">
7        <!--- Local variables --->
8        <cfset var colname="">
9        <cfset var value="">
11        <!--- Process gridaction --->
12        <cfswitch expression="#ARGUMENTS.gridaction#">
13            <!--- Process updates --->
14            <cfcase value="U">
15                <!--- Get column name and value --->
16                <cfset colname=StructKeyList(ARGUMENTS.gridchanged)>
17                <cfset value=ARGUMENTS.gridchanged[colname]>
18                <!--- Perform actual update --->
19                <cfquery datasource="#THIS.dsn#">
20                UPDATE artists
21                SET #colname# = '#value#'
22                WHERE artistid = #ARGUMENTS.gridrow.artistid#
23                </cfquery>
24            </cfcase>
25            <!--- Process deletes --->
26            <cfcase value="D">
27                <!--- Perform actual delete --->
28                <cfquery datasource="#THIS.dsn#">
29                DELETE FROM artists
30                where artistid = #ARGUMENTS.gridrow.artistid#
31                </cfquery>
32            </cfcase>
33        </cfswitch>
34    </cffunction>

The code uses a <CFSWITCH> to process a gridaction of U (update) or D (delete). For updates, argument gridchanged will be a structure containing an element for each column changed, the element name is the column name and the element value is the new value. Each column is updated individually, if a user makes three edits to the same row in the data grid the this method will be called three times, once for each row. As such, for updates, gridchanged only ever contains a single element, and so the code extracts the column name and value and saves them to local variables. These variables are then used in a <CFQUERY> to perform the actual update, using the primary key in the passed row (ARGUMENTS.gridrow) for the SQL WHERE clause. Deletes are processed similarly, with only the primary key needed.

Here is the complete artists.cfc, with both the bind and onchange methods:

view plain print about
1<cfcomponent output="false">
4    <cfset THIS.dsn="cfartgallery">
7    <!--- Get artists --->
8    <cffunction name="getArtists" access="remote" returntype="struct">
9        <cfargument name="page" type="numeric" required="yes">
10        <cfargument name="pageSize" type="numeric" required="yes">
11        <cfargument name="gridsortcolumn" type="string" required="no" default="">
12        <cfargument name="gridsortdir" type="string" required="no" default="">
14        <!--- Local variables --->
15        <cfset var artists="">
17        <!--- Get data --->
18        <cfquery name="artists" datasource="#THIS.dsn#">
19        SELECT artistid, lastname, firstname, email
20        FROM artists
21        <cfif ARGUMENTS.gridsortcolumn NEQ ""
22            and ARGUMENTS.gridsortdir NEQ "">
23            ORDER BY #ARGUMENTS.gridsortcolumn# #ARGUMENTS.gridsortdir#
24        </cfif>
25        </cfquery>
27        <!--- And return it as a grid structure --->
28        <cfreturn QueryConvertForGrid(artists,
29                  ,
30                            ARGUMENTS.pageSize)>

31    </cffunction>
34    <!--- Edit an artist --->
35    <cffunction name="editArtist" access="remote">
36        <cfargument name="gridaction" type="string" required="yes">
37        <cfargument name="gridrow" type="struct" required="yes">
38        <cfargument name="gridchanged" type="struct" required="yes">
40        <!--- Local variables --->
41        <cfset var colname="">
42        <cfset var value="">
44        <!--- Process gridaction --->
45        <cfswitch expression="#ARGUMENTS.gridaction#">
46            <!--- Process updates --->
47            <cfcase value="U">
48                <!--- Get column name and value --->
49                <cfset colname=StructKeyList(ARGUMENTS.gridchanged)>
50                <cfset value=ARGUMENTS.gridchanged[colname]>
51                <!--- Perform actual update --->
52                <cfquery datasource="#THIS.dsn#">
53                UPDATE artists
54                SET #colname# = '#value#'
55                WHERE artistid = #ARGUMENTS.gridrow.artistid#
56                </cfquery>
57            </cfcase>
58            <!--- Process deletes --->
59            <cfcase value="D">
60                <!--- Perform actual delete --->
61                <cfquery datasource="#THIS.dsn#">
62                DELETE FROM artists
63                WHERE artistid = #ARGUMENTS.gridrow.artistid#
64                </cfquery>
65            </cfcase>
66        </cfswitch>
67    </cffunction>

We'll look at additional <CFGRID> examples in the future.

Read More ›