Solved

How to insert data into a CFGRID

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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 …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 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