Solved

How to insert data into a CFGRID

Posted on 2013-10-24
5
586 Views
Last Modified: 2013-10-30
I am using this method from Ben Forta to create a grid that allows edits and deletes. I would also like the ability to use insert="yes" on the grid but I cannot get it working. Could someone assist? The insert button appears but I do not know how to send the newly entered information to the .cfc properly. Below is what I have so far with a "cfcase" of "I".

Page/Form:
<cfwindow initshow="true" center="true"
			width="430" height="340" title="Artists">

<cfform>
	<cfgrid name="artists"
			format="html"
			pagesize="10"
			striperows="yes"
			selectmode="edit"
                                                insert="yes"
			delete="yes"
			bind="cfc:artists.getArtists({cfgridpage},
										{cfgridpagesize},
										{cfgridsortcolumn},
										{cfgridsortdirection})"
			onchange="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>

Open in new window


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>
			<cfcase value="I">
				<!--- Perform insert--->
				<cfquery datasource="#THIS.dsn#">
				Insert Into artists.................
								</cfquery>
			</cfcase>

		</cfswitch>
	</cffunction>


</cfcomponent>

Open in new window

Any help would be greatly appreciated.
0
Comment
Question by:earwig75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39599544
If you are using Coldfusion 8, Check this CRUD operations [Create, read, Update, Delete]

http://www.anujgakhar.com/2008/03/18/crud-with-cfgrid-html-format/

Hre is another link as CFGrid Uses EXT JS 3 as its engine for cf9

http://www.coldfusion-ria.com/Blog/index.cfm/2009/12/14/ExtJS-30-Editor-Grid-with-ColdFusion

let me know which coldfusion version you are using
0
 

Author Comment

by:earwig75
ID: 39600381
I am using CF9 and would like to do this without relying on an ext hack or whatever. I know CF9 has the functionality built in but I can't seem to get it working.
0
 

Accepted Solution

by:
earwig75 earned 0 total points
ID: 39600463
I got this working. Added the below to my function:
<cfcase value="I">
<cfquery name="insertRow">
                INSERT INTO RECORDS (ROW_ID, ROW_COL1,ROW_COL2)
VALUES ('#gridrow.ROW_ID#',
'#gridrow.ROW_COL1#',
'#gridrow.ROW_COL2#'
)
</cfcase>

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 39602324
great but the inbuild does not have that many features what ext js allows you and its not a hack with ext js, actually its a underlying structure of cfgrid and eventually the usage of ext JS causes the CFGRID to work awesome, the way you did is very traditional

Cheers
0
 

Author Closing Comment

by:earwig75
ID: 39611020
I found a solution to allow inserts. This works perfectly.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question