Solved

How to insert data into a CFGRID

Posted on 2013-10-24
5
561 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 15

Expert Comment

by:myselfrandhawa
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 15

Expert Comment

by:myselfrandhawa
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now