Adobe ColdFusion 8

Grouping output from a query

In a query that you display using a cftree control, you might want to organize your employees by department. In this case, you separate column names with commas in the cftreeitem value attribute.

Organize the tree based on ordered results of a query

  1. Create a ColdFusion page named tree2.cfm with the following content:
    <!--- CFQUERY with an ORDER BY clause. --->
    <cfquery name="deptquery" datasource="cfdocexamples">
        SELECT Dept_ID, FirstName || ' ' || LastName
        AS FullName
        FROM Employee
        ORDER BY Dept_ID
    </cfquery>
    
    <!--- Build the tree control. --->
    <cfform name="form1" action="submit.cfm">
    
    <cftree name="tree1"
        hscroll="No"
        border="Yes"
        height="350"
        required="Yes">
    
    <cftreeitem value="Dept_ID, FullName"
        query="deptquery"
        queryasroot="Dept_ID"
        img="computer,folder,document"
        imgopen="computer,folder"
        expand="yes">
    
    </cftree>
    <br>
    <br><input type="Submit" value="Submit">
    </cfform>
    

  2. Save the page and view it in your browser. It should look as follows

Reviewing the code

The following table describes the highlighted code and its function

:

Code

Description

ORDER BY Dept_ID

Orders the query results by department.

<cftreeitem value="Dept_ID,FullName"

Populates the tree with the department ID, and under each department, the full name for each employee in the department.

queryasroot="Dept_ID"

Labels the root "Dept_ID".

img="computer,folder,document" imgopen="computer,folder"

Uses the ColdFusion supplied computer image for the root level, folder image for the department IDs, and document for the names, independent of whether any level is expanded (open) or collapsed. The imgopen attribute has only two items, because the employee names can never be open.

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.