ColdFusion Grid Editing Basic

Word Count: 1414

When the new grid features were announced in ColdFusion 8 people got very excited. I am not quite sure why but not to many people are talking about this feature these days. If you are new to grid editing support I encourage you to read an article by Ben Forta that outlines how to edit data in a grid. I am going to repeat some of that article but also show you a little more. Think of this as a refresher because the next tutorial I have will dive into some customization techniques.

Getting started with the tutorial we are going to stick with what got us here. I will be using the cfartgallery data source that ships with ColdFusion 8 so this should work out of the box. Here we have the code that creates our grid. There are only 2 attributes we need to add to our grid to make this work. First we set select mode to edit. This tells our grid to add editing capabilities. If we wanted to add deleting capabilities we could set the delete attribute to true but we will just be editing in this tutorial. The second difference is the the onChange attibute. We will look into this a little deeper in a second.

<cfquery name="getArtists" datasource="cfartgallery">
SELECT artistId, firstname, lastname, address, city, state, postalcode, email
FROM Artists
</cfquery>

<cfset args = structNew()>
<cfset args.name = "ArtistGrid">
<cfset args.format = "html">
<cfset args.query = "getArtists">
<cfset args.stripeRows = true>
<cfset args.selectColor = "##D9E8FB">
<cfset args.selectmode = "edit">
<cfset args.onchange = "cfc:artists.editArtist({cfgridaction},{cfgridrow},{cfgridchanged})">

<cfform>
<cfgrid attributeCollection="#args#">
<cfgridcolumn name="artistid" display="false">
<cfgridcolumn name="firstname" header="First Name">
<cfgridcolumn name="lastname" header="Last Name">
<cfgridcolumn name="email" header="Email Address">
<cfgridcolumn name="address" header="Address">
<cfgridcolumn name="city" header="City">
<cfgridcolumn name="state" header="State">
<cfgridcolumn name="postalcode" header="Zip">
</cfgrid>
</cfform>
Here is a how our grid turns out. You will notice that when you double click in a cell a text editor appears and after you make a change a red marker appears in the grid noting that the data has changed.

Now that we understand how to enable editing we need to cover how we actually save the changed data. If you look at the grid image you will notice that the last name Donolan now has a z on the end. If we enable firebug and take a look at the post we will see the following information. As you can see our grid passes 3 parameters; gridAction, gridRow and gridChanged. The gridAction argument will pass U for update and D for delete. The gridRow is a name value pair of the row being changed. Finally the gridChanged argument tells us what column is being changed and what the new value is.
{"gridaction":"U","gridrow":{"ARTISTID":1,"FIRSTNAME":"Aiden","LASTNAME":"Donolan","EMAIL":"aiden.donolan@donolan.com","ADDRESS":"352 Corporate Ave.","CITY":"Denver","STATE":"CO","POSTALCODE":"80206-4526","CFGRIDROWINDEX":1},"gridchanged":{"LASTNAME":"Donolanz"}}
If we look back at our code we should look at the onChange attribute. Here we are telling the grid that when the data changes we want to call our artists cfc and the method editArtist and into that method we are going to pass those 3 parameters.
<cfset args.onchange = "cfc:artists.editArtist({cfgridaction},{cfgridrow},{cfgridchanged})">
Finally here is the code from Ben's tutorial. If we get passed U for update we set the name of the column being changed and the new value and use that to update our database.
<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">

<cfset var colname = "">
<cfset var value = "">

<cfswitch expression="#arguments.gridaction#">
<!--- update --->
<cfcase value="U">
<cfset colname = StructKeyList(arguments.gridchanged)>
<cfset value = arguments.gridchanged[colname]>

<cfquery datasource="#variables.dsn#">
UPDATE Artists
SET #colname# = '#value#'
WHERE artistid = <cfqueryparam value="#arguments.gridrow.artistid#" cfsqltype="cf_sql_integer">
</cfquery>
</cfcase>
<!--- delete --->
<cfcase value="D">
<cfquery datasource="#THIS.dsn#">
DELETE FROM Artists
where artistid = <cfqueryparam value="#arguments.gridrow.artistid#" cfsqltype="cf_sql_integer">
</cfquery>
</cfcase>
</cfswitch>

</cffunction>
As you can see editing inline on the grid is very easy to do. I see many question out there about customizing the editing features, stay tuned because I have something you are going to like.

Comments

#1 Posted By: Rachael Posted On: 4/4/08 11:31 PM
I try to use the codes above, but it gives me error. "Error invoking CFC... : Internal Server Error".
I've changed the datasource to reflect my datasource. Tablenames to related table names. Just do not know what's wrong.
Please help? Thanks
#2 Posted By: Dan Vega Posted On: 4/8/08 10:18 AM |
Author Comment
Rachel,
That is not an issue with the grid. Are you able to invoke any other ColdFusion components? Are you still having this issue?
#3 Posted By: Rachael Posted On: 4/8/08 7:48 PM
Dan,
Yes, still on this issue. I was able to invoke if i'm using this :
<cfinvoke component="#fw.Config.AppDir#.#section#.#cfc#" method="GetProject">

but not this
onchange = "cfc:#cfc#.editSchedule({{cfgridaction},{cfgridrow},{cfgridchanged}})"

Using version 8 and connecting to sqlserver.
#4 Posted By: Rachael Posted On: 4/10/08 10:05 PM
Dan,
I've got it already. It's the dsn. When i hardcode the dsn, it works. No more errors.
Thanks! Your code is great
#5 Posted By: Brad Posted On: 5/27/08 7:06 PM
Thank you for this tutorial!!! I was wondering how do you get the gridAction to pass a "D" to delete? Thank you in advance for you help!
#6 Posted By: Sheila Handler Posted On: 6/5/08 6:13 PM
Is anyone else seeing this: after I change a cell in IE7, the cell goes blank except for the red marker. If I click elsewhere the info reappears. It's as is it is color white and bgcolor white. It looks okay in Firefox. Anyone else? Any solution?
#7 Posted By: Nathan Stanford Posted On: 6/6/08 4:43 PM
Can you do form validation with this to keep them from entering bad data?

char in numeric field, date out of date range like lice but put it in the past when it has to be a future date...

etc...
#8 Posted By: bkTheFrench Posted On: 9/19/08 4:44 PM
@Sheila Handler
"after I change a cell in IE7, the cell goes blank except for the red marker" i've got the same probleme in ie6. have you find an issue?
#9 Posted By: Sheila Handler Posted On: 9/19/08 5:00 PM
Nope. Never found a solution to the disappearing cell.
#10 Posted By: vishal Posted On: 9/24/08 7:12 PM
Thanks for sharing this. Quite helpful!!
#11 Posted By: Giedrius Posted On: 9/25/08 8:54 AM
#6 and #8 Pleasze read the comment by Jared Shields at http://www.coldfusionjedi.com/index.cfm/2008/8/5/A...
#12 Posted By: Julievi Posted On: 11/20/08 10:14 PM
I try to use the codes above, but it gives me error. "Error invoking CFC... : Internal Server Error".
I've changed the datasource to reflect my datasource. Tablenames to related table names. Just do not know what's wrong. Also when i hardcode the dsn stil getting the same error.


Please help? Thanks
#13 Posted By: Dan Vega Posted On: 11/20/08 10:27 PM |
Author Comment
Are you sure you have the right path to your CFC? This line <cfset args.onchange = "cfc:artists.editArtist(" tells me the artists cfc is in the same directory of the calling template. If it is not then you need to give it the correct path. All though the error your getting is kinda weird
#14 Posted By: Julievi Quijano Posted On: 11/20/08 10:37 PM
Thank you so much guys. I already got it.. thanks for this wonderful tutorial... God Bless
#15 Posted By: Bonnie Posted On: 12/22/08 2:01 PM
@Sheila; @bkTheFrench
Re the IE issue, this was a problem with improper commenting in the ext-all.css file. The solution is posted in the comments with even the specific classes at http://www.coldfusionjedi.com/index.cfm/2008/8/5/A...

Hope that helps!
#16 Posted By: Chris Smith Posted On: 1/28/09 4:58 PM
Hey Dan, any idea how to change the normal double click functionality of an editable grid row to a single click? Ive looked ALL OVER the place for some info on this but I can find a thing ;(
#17 Posted By: Dan Vega Posted On: 1/29/09 10:36 AM |
Author Comment
I know you can do that in 2.0 but I am not quite sure about 1.1. Check around and find out if its possible in ExtJS 1.1. If it is I am sure there is a way we can do it.
#18 Posted By: Ron Lebfrom Posted On: 6/15/09 6:59 PM
@Chris,

Add a event listener on the initiation of the grid.

var initusergrid = function(){
       grid = ColdFusion.Grid.getGridObject("WebAuthorGrid");
grid.addListener("rowclick",GetUserInfo);      

    }

-------------

On your page add a AjaxOnload
<cfset ajaxOnLoad("initusergrid")>

Hope this helps.

Ron


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.