ColdFusion 9.0 Resources |
Building tree controls with the cftree tagContents [Hide]The cftree tag lets you display hierarchical information within a form in a space-saving collapsible tree populated from data source queries. To build a tree control with the cftree tag, you use individual cftreeitem tags to populate the control. You can create trees in three formats:
Create and populate a tree control from a query
Reviewing the codeThe following table describes the highlighted code and its function:
Grouping output from a queryIn a query that you display using a cftree control, to organize your employees by department, separate column names with commas in the cftreeitemvalue attribute. Organize the tree based on ordered results of a query
Reviewing the codeThe following table describes the highlighted code and its function
The cftreeitem comma-separated value, img, and imgopen attributes correspond to the tree level structure. In applet format, if you omit the img attribute, ColdFusion uses the folder image for all levels in the tree; if you omit the imgopen attribute, ColdFusion uses the folder image for all expanded levels in the tree. Flash format ignores the img and imgopen attributes and always uses folders for levels with children and documents for nodes without children. The cftree form variablesThe cftree tag lets you force a user to select an item from the tree control by setting the required attribute to Yes. With or without the required attribute, ColdFusion passes two form variables to the application page specified in the cfformaction attribute:
To return the root part of the path, set the completepath attribute of the cftree tag to Yes; otherwise, the path value starts with the first node. If you specify a root name for a tree item using the queryasroot tag, that value is returned as the root. If you do not specify a root name, ColdFusion returns the query name as the root. If there is no query name, ColdFusion returns the tree name as the root. In the previous example, if the user selects the name "John Allen" in the tree, ColdFusion returns the following form variables: Form.tree1.path = 3\John Allen Form.tree1.node = John Allen The deptquery root does not appear in the path, because the cftree tag does not specify completePath="Yes". You can specify the character used to delimit each element of the path form variable in the cftreedelimiter attribute. The default is a backslash character (\). Input validationAlthough the cftree tag does not include a validate attribute, you can use the required attribute to force a user to select an item from the tree control. In addition, you can use the onValidate attribute to specify your own JavaScript code to perform validation. Structuring tree controlsTree controls built with the cftree tag can be complex. Knowing how to specify the relationship between multiple cftreeitem entries helps you handle the most complex cftree constructs. Creating a one-level tree controlThe following example consists of a single root and some individual items: <cfquery name="deptquery" datasource="cfdocexamples"> SELECT Dept_ID, FirstName || ' ' || LastName AS FullName FROM Employee ORDER BY Dept_ID </cfquery> <cfform name="form1" action="submit.cfm"> <cftree name="tree1"> <cftreeitem value="FullName" query="deptquery" queryasroot="Department"> img="folder,document"> </cftree> <br> <cfinput type="submit" value="Submit"> </cfform> Creating a multilevel tree controlWhen populating a cftree control, you create the multilevel structure of the tree by specifying a parent for each item in the tree. The parent attribute of the cftreeitem tag allows your cftree tag to show relationships between elements in the tree. In this example, every cftreeitem tag, except the top level Divisions, specifies a parent. For example, the cftreeitem tag specifies Divisions as its parent. The following code populates the tree directly, not from a query: <cfform name="form2" action="cfform_submit.cfm"> <cftree name="tree1" hscroll="No" vscroll="No" border="No"> <cftreeitem value="Divisions"> <cftreeitem value="Development" parent="Divisions" img="folder"> <cftreeitem value="Product One" parent="Development" img="document"> <cftreeitem value="Product Two" parent="Development"> <cftreeitem value="GUI" parent="Product Two" img="document"> <cftreeitem value="Kernel" parent="Product Two" img="document"> <cftreeitem value="Product Three" parent="Development" img="document"> <cftreeitem value="QA" parent="Divisions" img="folder"> <cftreeitem value="Product One" parent="QA" img="document"> <cftreeitem value="Product Two" parent="QA" img="document"> <cftreeitem value="Product Three" parent="QA" img="document"> <cftreeitem value="Support" parent="Divisions" img="fixed"> <cftreeitem value="Product Two" parent="Support" img="document"> <cftreeitem value="Sales" parent="Divisions" img="computer"> <cftreeitem value="Marketing" parent="Divisions" img="remote"> <cftreeitem value="Finance" parent="Divisions" img="element"> </cftree> </cfform> Image names in a cftree tagNote: The
information here applies to applet format trees. In Flash, you cannot control
the tree icons. Flash uses open and closed folders and documents
as the icons. In object format, the image information is preserved
in fields in the object structure.
The default image displayed in a tree is a folder. However, you can use the img attribute of the cftreeitem tag to specify a different image. When you use the img attribute, ColdFusion displays the specified image beside the tree items when they are not open. When you use the imgopen attribute, ColdFusion displays the specified image beside the tree items when they are open (expanded). You can specify a built-in ColdFusion image name, the file path to an image file, or the URL of an image of your choice, such as http://localhost/Myapp/Images/Level3.gif. You cannot use a custom image in Flash format. As a general rule, make the height of your custom images less than 20 pixels. When populating a cftree control with data from a cfquery tag, you can use the img attribute of cftreeitem tag to specify images or filenames for each level of the tree as a comma-separated list. The following are the ColdFusion built-in image names:
Note: In applet format, you can also control the tree
appearance by using the cftree tag lookAndFeel attribute
to specify a Windows, Motif, or Metal look.
Embedding URLs in a cftree tagThe href attribute in the cftreeitem tag lets you designate tree items as links. To use this feature in a cftree control, you define the destination of the link in the href attribute of the cftreeitem tag. The URL for the link can be a relative URL or an absolute URL, as in the following examples. Embed links in a cftree control
Reviewing the codeThe following table describes the highlighted code and its function:
Specifying the tree item in the URLWhen a user clicks a tree item to link to a URL, the cftreeItemKey variable, which identifies the selected value, is appended to the URL in the following form: http://myserver.com?CFTREEITEMKEY=selected_item_value_attribute If the value attribute includes spaces, ColdFusion replaces the spaces with plus characters (+). Automatically passing the name of the selected tree item as part of the URL makes it easy to implement a basic “drill down” application that displays additional information based on the selection. For example, if the specified URL is another ColdFusion page, it can access the selected value as the variable URL.CFTREEITEMKEY. To disable this behavior, set the appendkey attribute in the cftree tag to no. |