cfselect

Description

Constructs a drop-down list box form control. Used in a cfform tag. You can populate the list from a query, or by using the HTML option tag.

Category

Forms tags

Syntax

<cfselect  
    name="name"     bind="bind expression" 
    bindAttribute="attribute name" 
    bindOnLoad="yes|no" 
    display="text"     editable="yes|no" 
    enabled="yes|no" 
    group="query column name"     height="number of pixels" 
    id="HTML id" 
    label="label"     message="text"     multiple="yes|no" 
    onBindError="JavaScript function name" 
    onChange="JavaScript or ActionScript" 
    onClick="JavaScript function name" 
    onError="JavaScript" 
    onKeyDown="JavaScript or ActionScript" 
    onKeyUp="JavaScript or ActionScript" 
    onMouseDown="JavaScript or ActionScript" 
    onMouseUp="JavaScript or ActionScript" 
    query="query name"     queryPosition="above|below" 
    required="yes|no" 
    selected="value or list"     size="integer"     sourceForTooltip="URL" 
    style="style specification"     tooltip="text" 
    value="text"     visible="yes|no" 
    width="number of pixels"> 
 
zero or more HTML option tags 

</cfselect>

Some attributes apply to only specific display formats. For details see the Attributes table.

Note: You can specify this tag’s attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag’s attribute names as structure keys.

History

ColdFusion 8:

  • Added support for binding in HTML format forms, including the bind, bindAttribute, and bindOnLoad, and onBindError attributes.

  • Added support for tool tips in HTML format forms, including the sourceForTooltip attribute.

ColdFusion MX 7:

  • When populating a cfselect tag with a query when the database field has spaces instead of a value, the error processing of the cfform tag required field is not triggered as it was in ColdFusion MX 6.1.

  • Added support for specifying multiple values to the selected attribute.

  • Deprecated the passthrough attribute. The tag now supports all HTML select tag attributes directly.

  • Added on-prefixed attributes.

  • Added enabled, group, height, label, queryPosition, tooltip, visible, and width attributes.

Attributes

The following table lists attributes that ColdFusion uses directly. The tag also supports all HTML select tag attributes that are not on this list, and passes them directly to the browser.

Note: Attributes that are marked as Flash only are not handled by the skins provided with ColdFusion. They are, however, included in the generated XML.

Attribute

Req/Opt;

Format

Default

Description

name

Required;

All

 

Name of the select form element.

bind

Optional;

HTML

 

A bind expression that dynamically sets an attribute of the control. For details, see Usage.

bindAttribute

Optional;

HTML

Populate the options

Specifies the HTML tag attribute whose value is set by the bind attribute. You can only specify attributes in the browser’s HTML DOM tree, not ColdFusion-specific attributes.

Ignored if there is no bind attribute.

bindOnLoad

Optional;

HTML

no

A Boolean value that specifies whether to execute the bind attribute expression when first loading the form. Ignored if there is no bind attribute.

display

Optional;

All

Value of value attribute

Query column to use for the display label of each list element. Used with the query attribute.

editable

Optional; Flash

no

Boolean value specifying whether you can edit the contents of the control.

enabled

Optional; Flash

yes

Boolean value specifying whether the control is enabled. A disabled control appears in light gray. The inverse of the disabled attribute.

group

Optional; HTML and XML

 

Query column to use to group the items in the drop-down list into a two-level hierarchical list.

height

Optional; Flash

 

The height of the control, in pixels.

id

Optional;

HTML

Value of name attribute

The HTML ID of the control.

label

Optional;

Flash and XML

 

Label to put next to the control on a Flash or XML-format form.

message

Optional;

All

 

Message to display if required="yes" and no selection is made.

multiple

Optional;

All

no

  • yes: allows selecting multiple elements in drop-down list.

  • no

onBindError

Optional;

HTML

See Description

The name of a JavaScript function to execute if evaluating a bind expression results in an error. The function must take two attributes: an HTTP status code and a message. (The status code is ‑1 if the error is not an HTTP error.)

If you omit this attribute, and specified a global error handler (by using the ColdFusion.setGlobalErrorHandler function), it displays the error message; otherwise a default error pop-up appears.

onChange

Optional;

All

 

JavaScript (HTML/XML) or ActionScript (Flash) to run when the control changes due to user action.

onClick

Optional; HTML and XML

 

JavaScript to run when the user clicks the control.

onError

Optional;

HTML and XML

 

Custom JavaScript function to execute if validation fails.

onKeyDown

Optional;

All

 

JavaScript (HTML/XML) or ActionScript (Flash) to run when the user presses a keyboard key in the control.

onKeyUp

Optional;

All

 

JavaScript (HTML/XML) or ActionScript (Flash) to run when the user releases a keyboard key in the control.

onMouseDown

Optional;

All

 

JavaScript (HTML/XML) or ActionScript (Flash) to run when the user releases a mouse button in the control.

onMouseUp

Optional;

All

 

JavaScript (HTML/XML) or ActionScript (Flash) to run when the user presses a mouse button in the control.

query

Optional;

All

 

Name of query to populate drop-down list.

queryPosition

Optional;

All

above

If you populate the options list with a query and use HTML option child tags to specify additional entries, this attribute determines the location of the items from the query relative to the items from the option tags:

  • above: puts the query items above the options items.

  • below: puts the query items below the options items.

required

Optional;

All

no

Note: This attribute has no effect if you omit the size attribute or set it to 1, because the browser always submits the displayed item. You can work around this issue: format forms by having an initial option tag with value=" " (notice the space character between the quotation marks). This workaround applies only for Flash forms and not for HTML forms or XML forms.

  • yes: a list element must be selected when the form is submitted.

  • no

selected

Optional;

All

 

One or more option values to preselect in the selection list. To specify multiple values, use a comma-delimited list. This attribute applies only if selection list items are generated from a query. The cfform tag preserveData attribute can override this value.

size

Optional;

All

1

Number of entries to display at one time. The default value displays a drop-down list. Any other value displays a list box with size number of entries visible at one time.

sourceForTooltip

Optional; HTML

 

The URL of a page to display as a tool tip. The page can include CFML and HTML markup to control the tip contents and format, and the tip can include images.

If you specify this attribute, an animated icon appears with the text "Loading..." while the tip is being loaded.

style

Optional;

All

 

In HTML or XML format forms, ColdFusion passes the style attribute to the browser or XML.

In Flash format, must be a style specification in CSS format, with the same syntax and contents as used in Flex for the corresponding Flash element.

tooltip

Optional; Flash, HTML

 

Text to display when the mouse pointer hovers over the control. The text can include HTML markup.

Ignored if you specify a sourceForTooltip attribute.

value

Optional;

All

 

Query column to use for the value of each list element. Used with the query attribute.

visible

Optional; Flash

yes

Boolean value that specifies whether to show the control. The display reserves empty space for an invisible control.

width

Optional; Flash

 

The width of the control, in pixels.

Usage

For this tag to work properly. the browser must have JavaScript enabled.

This tag requires an end tag and can include HTML option and optgroup child tags.

To ensure that a selected list box item persists across postbacks, use the cfform tag preserveData attribute with a list generated from a query. (This strategy works only with data that is populated from a query.)

If the cfform preserveData attribute is yes and the form posts back to the same page, and if the control is populated by a query, the posted selections for the cfselect control are used instead of the Selected attribute. For controls that are populated with regular HTML option tags, the developer must dynamically add the Selected attribute to the appropriate option tags.

The group option generates a query by using SQL GROUP BY syntax and places the value column entries from each group in an indented list under the group column’s field value. This option generates an HTML optgroup tag for each entry in the group column.

Close each HTML option tag in the cfselect tag body with a </option> end tag. If you do not do so, and you specify queryPosition="below", the first item from the query might not appear in the list.

The bind attribute lets you set cfselect attributes dynamically. Often, it is used to dynamically create the options list based on values that the user enters in the form. For example, you can use the bind attribute to create a Cities option list based on the user’s selection from a States cfselect control.

When you use a bind attribute to populate the selection list, the function or URL that returns the selection values must return one of the following:

  • A two-dimensional array, where the first element in each array row is the option value and the second element in the row is the text to display in the option list.

  • If the bind specifies a CFC function, a query, or, if the bind specifies a URL, a JSON representation of a query. The query must include columns whose names are the values of the cfselect tag value and display attributes. Although you can return additional columns, you cannot use them in your client-side code. When you call a CFC function, you do not have to convert the query to JSON format yourself; ColdFusion automatically does the conversion.

    To use this format, specify a value attribute. If you omit the display attribute, you must have only a single column in the query that contains the values; the values are also used as the displayed text.

For detailed information on binding, see Binding data to form fields in the Developing ColdFusion Applications.

For more information, see the cfform tag entry.

Example

Example 1: Without data binding

The following example lets you select one or more employee names from a list of all employees, grouped by departments, and displays the selected names and the employee’s e-mail addresses. It includes an option to get data for all employees.

<!--- Get the employee names from the database. ---> 
<!--- Use SQL to create a Name field with first and last names. ---> 
<cfquery name = "GetAllEmployees" dataSource = "cfdocexamples" 
        cachedwithin="#createTimeSpan(0,1,0,0)#"> 
    SELECT Emp_ID, EMail, Phone, Department, FirstName, LastName,  
        FirstName ||' ' 
        ||lastName as Name 
    FROM Employees  
    GROUP BY Department, Emp_ID, EMail, Phone, FirstName, LastName, FirstName 
</cfquery> 
 
<h2>cfselect Example</h2> 
<!-- The cfif statement is true if the form was submitted. 
    Show the selected names. ---> 
<cfif IsDefined("form.employeeid")> 
    <!--- The form was submitted. ---> 
    <h3>You Selected the following employees</h3> 
    <cfif form.employeeid IS ""> 
        <!--- Select All option was selected. Show all employees. ---> 
        <cfoutput query="GetAllEmployees"> 
            #name#<br> 
            Email: #email#<br><br> 
        </cfoutput> 
    <cfelse> 
        <!---  
            Use a query of queries to get the data for the selected users.  
            Form.employeeid is a comma-delimited list of selected employee IDs.  
        ---> 
        <cfquery name = "GetSelectedEmployees" dbtype="query"> 
            SELECT Emp_ID, EMail, Phone, Department, FirstName, LastName, 
                FirstName 
                ||' ' ||lastName as Name 
            FROM GetAllEmployees 
            WHERE Emp_ID in (#form.employeeid#) 
        </cfquery>     
        <!--- Display the names and e-mail addresses from the query. ---> 
        <cfoutput query="GetSelectedEmployees"> 
            #firstName# #lastName#<br> 
            Email: #email#<br> 
            <br> 
        </cfoutput> 
    </cfif>  
</cfif> 
 
<!--- The cfform tag posts back to the current page. ---> 
<h3>Select one or more employees</h3> 
<cfform action = "#CGI.SCRIPT_NAME#"> 
    <!---  
            Use cfselect to present the query's LastName column,  
            grouped by department.  
            Allow Multiple selections. 
        ---> 
    <cfselect  
        name = "employeeid"  
        size = "15"  
        multiple="yes"  
        required = "Yes" 
        message = "Select one or more employee names"  
        query = "GetAllEmployees"  
        group="Department" 
        display ="name"  
        value ="emp_id"  
        queryPosition="Below"> 
        <!--- Add an option to select all employees. ---> 
        <option value = "">Select All</option> 
    </cfselect><br><br> 
    <input type="Submit"> 
</cfform> 

Example 2: With data binding

The following example uses binding to fill in the options list of the Cities control only after the user selects a state. (In this example, only two states, California and New Jersey, have city entries.)

The CFML page is the simplest part of the example. It consists of the following lines:

<html> 
<head> 
</head> 
<body> 
<cfform name="mycfform"> 
    <!---  
        The States selector.  
        The bindonload attribute is required to fill the selector.  
    ---> 
    <cfselect name="state" bind="cfc:bindFcns.getstates()" bindonload="true"> 
        <option name="0">--state--</option> 
    </cfselect> 
    <cfselect name="city" bind="cfc:bindFcns.getcities({state})"> 
        <option name="0">--city--</option> 
    </cfselect> 
</cfform> 
</body> 
</html>

The BinFcns CFC has three functions: getstates, to get the states; getcities, to get the cities; and an internal getXmlData function that parses an XML file to get the state and city information. The following examples shows the CFC:

<cfcomponent> 
    <cffunction name="getXmlData" output="true"> 
        <cfset var xmlData = "">  
        <cffile action="read" file="#expandpath('.')#\states.xml" 
            variable="xmlData"> 
        <cfset xmlData = XmlParse(xmlData)> 
        <cfreturn xmlData> 
    </cffunction>     
 
    <cffunction name="getstates" access="remote"> 
        <cfset state = arraynew(2)> 
        <cfset xmlData = getXmlData()> 
        <cfset numStates = 0> 
        <cfset state[1][1] = "0"> 
        <cfset state[1][2] = "--state--"> 
        <cfset numStates = ArrayLen(xmlData.states.XmlChildren)> 
        <cfloop from="1" to="#numStates#" index="j"> 
            <cfset state[j+1][1] =  
                ltrim(xmlData.states.state[j].XmlAttributes.abr)> 
            <cfset state[j+1][2] = ltrim(xmlData.states.state[j].name.xmlText)> 
        </cfloop> 
        <cfreturn state> 
    </cffunction> 
 
    <cffunction name="getcities" access="remote"> 
        <cfargument name="state" required="yes"> 
        <cfset var city = arraynew(2)> 
        <cfset var xmlData = getXmlData()> 
        <cfset var numStates = 0> 
        <cfset var numCities = 0> 
<cflog text="In getcities"> 
        <cfset city[1][1] = "0"> 
        <cfset city[1][2] = "--city--"> 
        <cftry> 
            <cfset numStates = ArrayLen(xmlData.states.XmlChildren)> 
            <cfloop from="1" to="#numStates#" index="j"> 
                <cfif xmlData.states.state[j].XmlAttributes.abr eq state> 
                    <cfset numCities = 
                        ArrayLen(xmlData.states.state[j].cities.XmlChildren)> 
                    <cfloop from="1" to="#numCities#" index="k"> 
                        <cfset city[k+1][1] =     ltrim(xmlData.states.state[j].cities.city[k].XmlAttributes.name)> 
                        <cfset city[k+1][2] = ltrim(xmlData.states.state[j].cities.city[k].XmlAttributes.name)> 
                    </cfloop> 
                    <cfbreak> 
                </cfif> 
            </cfloop> 
        <cfcatch type="any"> 
            <!--- Do nothing. ---> 
        </cfcatch> 
        </cftry>  
        <cfreturn city> 
    </cffunction> 
     
</cfcomponent>

The states.xml file has the following code. To keep the code short, only two states have cities, and only four states are listed.

<states> 
    <state abr="NJ"> 
        <name>New Jersey</name> 
        <cities> 
            <city name="Edison" /> 
            <city name="Rahway" /> 
            <city name="Atlantic City" /> 
            <city name="Hoboken" /> 
            <city name="Jersey City" /> 
            <city name="Newark" /> 
            <city name="Trenton" /> 
            <city name="Union City" /> 
        </cities> 
    </state> 
    <state abr="CA"> 
        <name>California</name> 
        <cities> 
            <city name="Anaheim" /> 
            <city name="Beverly Hills" /> 
            <city name="Elk Grove" /> 
            <city name="Fairfield" /> 
            <city name="Fremont" /> 
            <city name="Indian Wells" /> 
            <city name="Long Beach" /> 
        </cities> 
    </state> 
    <state abr="ME"> 
        <name>Maine</name> 
    </state> 
    <state abr="MA"> 
        <name>Massachusetts</name> 
    </state> 
</states>