Processing cfgrids into a database.
01/02/2006 05:05:14 PM
When I first started using the CFGRID tag, I fould many articles on how to format it and populate it, etc, however, finding information as to how to process the cfgrid once it is submitted was like finding a needle in a haystack.
I have found that it is like processing an array of items.
First of all you have to check to see if it is defined as an array. I found that if you clicked inside a grid and did not change anything, it did not return an array, however, the variable that you would expect to be an array was not an array. So, for a grid named: gridDiscount and the column named description the code:
Would be added to make sure an array was returned.
Then you can get the length of the number of changes to items in the grid:
<cfset GridLength = ArrayLen(Form.gridDiscount.Description)>
No you can set up a loop to process the items in the grid:
<cfloop index="IX" from="1" to="#GridLength#">
You need to check the variable Form.gridDiscount.RowStatus.action[IX] for one of three values: I for insert, U for Update and D for delete and take the appropirate action.
For delete use the Form.gridDiscount.Original.KeyID[IX] field to get the original key. Of course the ID of the key will be different in your case.
Other fields that have been returned for changed rows will not have the word Original in the array. An updated field such as Description would be referenced as Form.gridDiscount.Description[IX].
These are just your basics. If you need to process the grid results individually. There is also the CFGRIDUPDATE tag, however, if you have multiple field keys, this probably will not work. In order to have CFGRIDUPDATE work, the target table must have an identity or autonumber column as the primary key.