Used inside cfform
, cfgrid
lets you place a grid control in a ColdFusion form. A grid control is a table of data divided into rows and columns. The cfgrid
tag column data is specified with cfgridcolumn
tags.
<cfgrid name = "name" height = "integer" width = "integer" autoWidth = "Yes" or "No" vSpace = "integer" hSpace = "integer" align = "value" query = "query_name" insert = "Yes" or "No" delete = "Yes" or "No" sort = "Yes" or "No" font = "column_font" fontSize = "size" italic = "Yes" or "No" bold = "Yes" or "No" textColor = "Web color" href = "URL" hrefKey = "column_name" target = "URL_target" appendKey = "Yes" or "No" highlightHref = "Yes" or "No" onValidate = "javascript_function" onError = "text" gridDataAlign = "position" gridLines = "Yes" or "No" rowHeight = "pixels" rowHeaders = "Yes" or "No" rowHeaderAlign = "position" rowHeaderFont = "font_name" rowHeaderFontSize = "size" rowHeaderItalic = "Yes" or "No" rowHeaderBold = "Yes" or "No" rowHeaderTextColor = "Web color" rowHeaderWidth = "col_width" colHeaders = "Yes" or "No" colHeaderAlign = "position" colHeaderFont = "font_name" colHeaderFontSize = "size" colHeaderItalic = "Yes" or "No" colHeaderBold = "Yes" or "No" colHeaderTextColor = "Web color" bgColor = "Web color" selectColor = "Web color" selectMode = "mode" maxRows = "number" notSupported = "text" pictureBar = "Yes" or "No" insertButton = "text" deleteButton = "text" sortAscendingButton = "text" sortDescendingButton = "text"> </cfgrid>
cfapplet,
cfform,
cfinput,
cfselect,
cfslider,
cftextinput,
cftree,
cfgridcolumn,
cfgridrow,
cfgridupdate
You can populate a cfgrid
with data from a cfquery. If you do not specify any cfgridcolumn entries, a default set of columns is generated. Each column in the query is included in the default column list. In addition, a default header for each column is created by replacing any hyphen (-) or underscore (_) characters in the table column name with spaces. The first character and any character after a space is changed to uppercase; all other characters are lowercase.
Note
|
The cfgrid
tag returns data by setting specific form variables in the data submitted to the form's ACTION page, just as a standard HTML form control does. With cfgrid
, the data submitted reflects changes and selections the user has made within the grid. Since this data can vary depending on the tag's SelectMode
attribute, the form variables returned also vary depending on this attribute's setting.
In general, the data returned falls into one of two categories. Data returned which reflects simple selection operations, and more complex data returned from insert, update and delete operations.
If the SelectMode
attribute equals Single
, Column
, or Row
, the data returned by form variables to the cfform
's ACTION page contains information about which cell or cells were selected by the user. In general, this data is made available by ColdFusion in the ACTION page as ColdFusion variables in the form scope using the following naming convention:
form.#GridName#.#ColumnName#.
Specifically, each SelectMode
returns the following form variable(s):
form.#GridName#.#ColumnName# = "SelectedCellValue"
form.#GridName#.#ColumnName# = "ValueOfCellRow1, ValueOfCellRow2, ... ValueOfCellRowN"
form.#GridName#.#Column1Name# ="ValueOfCellInSelectedRow"
form.#GridName#.#Column2Name# = "ValueOfCellInSelectedRow"
form.#GridName#.#ColumnNName# = "ValueOfCellInSelectedRow"
If SelectMode
attribute is Edit
, the grid returns a great deal of data in order to inform the ACTION page of any inserts, updates or deletes that the user has made to the grid. In most cases, you will not need to manually process this data, but can just use the cfgridupdate
tag to automatically gather the data from the various form variables. In fact, cfgridupdate
not only collects the incoming data but can also write the appropriate SQL calls, and updates your data source.
If you are unable to use cfgridupdate
(if, for example, you need to distribute the returned data to more than one data source,) you will need to write code to read the appropriate form variables. In this mode ColdFusion creates three array variables in the form scope for each cfgrid
, named:
form.#GridName#.#ColumnName#
form.#GridName#.original.#ColumnName# form.#GridName#.RowStatus.Action
Each row of the table that contains an update, insert, or deletion has a parallel entry in each of these three arrays. To view all the information for all the changes, you can traverse the three arrays as in the following example.
<CFLOOP INDEX="ColName" LIST="#ColNameList#">
<CFIF IsDefined("form.#GridName#.#ColName#")> <CFOUTPUT><BR>form.#GridName#.#ColName#:<BR></CFOUTPUT> <CFSET Array_New = evaluate("form.#GridName#.#ColName#")> <CFSET Array_Orig = evaluate("form.#GridName#.original.#ColName#")> <CFSET Array_Action = evaluate("form.#GridName#.RowStatus.Action")> <CFIF NOT IsArray(Array_New)> <B>The form variable is not an array!</b><BR> <CFELSE> <CFSET size = ArrayLen(Array_New)> <CFOUTPUT> Result Array Size is #size#.<BR> Contents:<BR> </CFOUTPUT> <CFIF size IS 0> <B>The array is empty.</B><BR> <CFELSE> <TABLE BORDER="yes"> <TR> <TH>Loop Index</TH> <TH>Action</TH> <TH>Old Value</TH> <TH>New Value</TH> </TR> <CFLOOP index="LoopCount" from="1" to=#size#> <CFSET Val_Orig = Array_Orig[#LoopCount#]> <CFSET Val_New = Array_New[#LoopCount#]> <CFSET Val_Action = rray_Action[#LoopCount#]> <CFOUTPUT> <TR> <TD>#LoopCount#</TD> <TD>#Val_Action#</TD> <TD>#Val_Orig#</TD> <TD>#Val_New#</TD> </TR> </CFOUTPUT> </CFLOOP> </TABLE> </CFIF> </CFIF> <CFELSE> <CFOUTPUT>form.#GridName#.#ColName#: NotSet!</cfoutput><BR> </CFIF> </CFLOOP>
When specifying a URL with grid items using the href
attribute, the value of the selectMode
attribute determines whether the appended key value is limited to a single grid item or it extends to a grid column or row. When a user clicks on a linked grid item, a cfgridkey
variable is appended to the URL in the following form:
http://myserver.com?cfgridkey = selection
If the appendKey
attribute is set to No, then no grid values are appended to the URL.
The value of selection is determined by the value of the selectMode
attribute:
selectMode = "Single"
, selection is the value of the column clicked.
selectMode = "Row"
, selection is a comma-separated list of column values in the clicked row, beginning with the value of the first cell in the selected row.
When selectMode = "Column"
, selection is a comma-separated list of row values in the clicked column, beginning with the value of the first cell in the selected column.
<!--- This example shows the cfgrid, cfgridcolumn, cfgridrow,
and cfgridupdate tags in action --->
<!--- use a query to show the useful qualities of cfgrid --->
<!--- If the gridEntered form field has been tripped,
perform the gridupdate on the table specified in the database.
Using the default value keyonly = yes allows us to change only
the information that differs from the previous grid --->
<cfif IsDefined("form.gridEntered") is True>
<cfgridupdate grid = "FirstGrid" dataSource = "cfsnippets"
tableName = "CourseList" keyOnly = "Yes">
</cfif>
<!--- query the database to fill up the grid --->
<cfquery name = "GetCourses" dataSource = "cfsnippets">
SELECT Course_ID, Dept_ID, CorNumber,
CorName, CorLevel, CorDesc
FROM CourseList
ORDER by Dept_ID ASC, CorNumber ASC
</cfquery>
<html>
<head>
<title>
cfgrid Example
</title>
</head>
<body>
<H3>cfgrid Example</H3>
<I>Try adding a course to the database, and then deleting it.</I>
<!--- call the cfform
to allow us to use cfgrid controls --->
<cfform action = "cfgrid.cfm" method = "POST" enableCAB = "Yes">
<!--- We include Course_ID in the cfgrid, but do not allow
for its selection or display --->
<!--- cfgridcolumn tags are used to change the parameters
involved in displaying each data field in the table--->
<cfgrid name = "FirstGrid" width = "450"
query = "GetCourses" insert = "Yes"
delete = "Yes" sort = "Yes"
font = "Tahoma" bold = "No" italic = "No"
appendKey = "No" highlightHref = "No"
gridDataAlign = "LEFT" gridLines = "Yes"
rowHeaders = "Yes" rowHeaderAlign = "LEFT"
rowHeaderItalic = "No" rowHeaderBold = "No"
colHeaders = "Yes" colHeaderAlign = "LEFT"
colHeaderItalic = "No" colHeaderBold = "No"
selectColor = "Red" selectMode = "EDIT"
pictureBar = "No" insertButton = "To insert"
deleteButton = "To delete" sortAscendingButton = "Sort ASC"
sortDescendingButton = "Sort DESC">
<cfgridcolumn name = "Course_ID" dataAlign = "LEFT"
bold = "No" italic = "No"
select = "No" display = "No"
headerBold = "No" headerItalic = "No">
<cfgridcolumn name = "Dept_ID" header = "Department"
headerAlign = "LEFT" dataAlign = "LEFT"
bold = "Yes" italic = "No"
select = "Yes" display = "Yes"
headerBold = "No" headerItalic = "Yes">
<cfgridcolumn name = "CorNumber" header = "Course ##"
headerAlign = "LEFT" dataAlign = "LEFT"
bold = "No" italic = "No"
select = "Yes" display = "Yes"
headerBold = "No" headerItalic = "No">
<cfgridcolumn name = "CorName" header = "Name"
headerAlign = "LEFT" dataAlign = "LEFT"
font = "Times" bold = "No"
italic = "No" select = "Yes"
display = "Yes" headerBold = "No"
headerItalic = "No">
<cfgridcolumn name = "CorLevel" header = "Level"
headerAlign = "LEFT" dataAlign = "LEFT"
bold = "No" italic = "No"
select = "Yes" display = "Yes"
headerBold = "No" headerItalic = "No">
<cfgridcolumn name = "CorDesc" header = "Description"
headerAlign = "LEFT" dataAlign = "LEFT"
bold = "No" italic = "No"
select = "Yes" display = "Yes"
headerBold = "No" headerItalic = "No">
</cfgrid>
...