Skip navigation

Chaining select lists

Feb 20, 2012 2:45 AM

  Latest reply: BKBK, Mar 6, 2012 5:40 AM
Replies
  • Currently Being Moderated
    Mar 6, 2012 12:56 AM   in reply to biene22

    So long as we understand that what we now have is experimental. It goes against the whole point of binding, which is to enable you to separate business code (like queries) from presentation code.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 5:01 AM   in reply to biene22

    What do you mean by 'binding doesn't work anymore'. If you get the select lists, then binding is working.

     

    I've just run a quick test. It works, at least on my system. What's your ColdFusion version?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 5:40 AM   in reply to biene22

    biene22 wrote:

     

    When I change  the department, values of selectlist "employee" are always the same. They does not depend on the selected value of department.

     

    ColdFusion 9 is as up to date as you can be. You could have experimented a bit with the above code. Binding to department is just a minor variation. For example,

     

    selectEmployee.cfm

    <!--- The employee ID (emp_id) is expected to come in as a URL parameter. The bind requires it when the page loads, so a default emp_id, 7, is set. --->

    <cfparam name="url.emp_id" default="6">

     

    <cfquery name = "select_user" dataSource = "cfdocexamples">

        SELECT department, FirstName || ' ' || LastName as emp_name

        FROM employees

        WHERE emp_id = #url.emp_id#

    </cfquery>

     

    <cfform name="empForm">

    Department: <cfselect  name="dept" bind="cfc:Employee.getDepartments()" bindonload="true" selected="#select_user.department#"></cfselect>

    <br>

    Employee:   <cfselect  name="employee" bind="cfc:Employee.getEmployees({dept})" bindonload="true" selected="#select_user.emp_name#" ></cfselect>

    <br><br>

    <cfinput name="sbmt" type="submit" value="Submit">

    </cfform>

     

    Employee.cfc

    <cfcomponent>

    <cffunction name="getDepartments" access="remote" output="false" returntype="array">

    <cfset var departments = queryNew("","")>

    <cfset var arr = arrayNew(2)>

     

    <cfquery name = "getDept" dataSource = "cfdocexamples">

        SELECT distinct(department)

        FROM employees

    </cfquery>

     

    <cfloop query="getDept">

    <cfset arr[currentrow][1]=department><!---option values in the select list--->

    <cfset arr[currentrow][2]=department><!---displayed values in the select list --->

    </cfloop>

     

    <cfreturn arr>

    </cffunction>

     

    <cffunction name="getEmployees" access="remote" output="false" returntype="any">

    <cfargument name="dept" type="string" required="true">

    <cfset var employees = queryNew("","")>

    <cfset var arr = arrayNew(2)>

     

    <cfquery name = "employees" dataSource = "cfdocexamples">

        SELECT emp_id,  FirstName || ' ' || LastName as emp_name

        FROM Employees

       WHERE department = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.dept#">

    </cfquery>

     

    <cfloop query="employees">

    <cfset arr[currentrow][1]=emp_id><!---option values in the select list--->

    <cfset arr[currentrow][2]=emp_name><!---displayed values in the select list --->

    </cfloop>

     

    <cfreturn arr>

    </cffunction>

    </cfcomponent>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points