Solved

How to insert data into a CFGRID

Posted on 2013-10-24
5
573 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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