Tuesday, February 9, 2010

ColdFusion 9: CFSELECT + BIND + Selected Attribute Work Together Now?

In case you have been living under a rock like me, it seems the selected attribute works with bound cfselect lists.


Form

<cfparam name="form.foodID" default="0">

<cfform method="post" action="#CGI.SCRIPT_NAME#">
    Favorite Food: 
                <cfselect name="foodID"
                    value="foodID"
                    display="FoodName"
                    selected="#form.foodID#"
                    bind="cfc:Food.getFood()"
                    bindOnLoad="true"
                />
    <cfinput type="submit" name="submitMe" />
</cfform>

Food.cfc
<cfcomponent output="false">
   
   <cffunction name="getFood" access="remote" output="false" returntype="query">
      <cfset var q    = QueryNew("") />
      <cfset queryAddColumn(q, "FoodID", "integer", listToArray("0,1,2,3")) />
      <cfset queryAddColumn(q, "FoodName", "varchar", listToArray("-Pick Something-,Burger,Pizza,Lasagna")) />
      <cfreturn q />      
   </cffunction>
   
</cfcomponent>

3 comments:

Zarko February 9, 2010 at 4:24 AM  

Wow! Didn't try it, but I lost my hair trying on CF8. Thanks for the tip!

BigFeetBigShoes,  October 27, 2010 at 6:32 AM  

Thanks for posting this, its good to know its working in 9. Unfortunately we are stuck on 8 for a couple more months. Is there a simple interim solution for the bug in 8?

cfSearching October 27, 2010 at 8:27 AM  

Not a bug. But not supported either. Though I agree it should be ;)

Unfortunately, I do not know of any one-liners. But here are two options:

http://www.dansshorts.com/post/cfselect-binding-and-selectedvalues
http://www.coldfusionjedi.com/index.cfm/2007/8/7/Selecting-default-items-using-ColdFusion-8s-AJAX-Controls

-Leigh

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep