CFSelect bind for Search or Edit on same form

Creating a data based derived dropdown list bound to a record for Search or Edit form is sometimes needed. For search you may want to return All as your first item in the dropdown, but for edit mode you likely want to return the actual value you searched for or the actual value already existing in the database. Imagine you are maintaining a Store list for a retail sales company and need to search/Edit/Copy/Update records using one form.

This is a solution I came up with using a CFSELECT element bound to a CFC. Note brackets have been removed.

form.cfm

//set a default to return all results. get_storelist('#storenum#') is what passes ALL or a selected store number
cfparam name="storenum" default="All"
cfform name=myform
label for="storenum" //a label
cfselect name="mylist" bind="CFC:get_storelist('#storenum#')" bindonload="yes" value="storenum" display="storeloc" /cfselect
...
/cfform

CFC

cffunction name="get_storelist" access="remote" returntype="query" hint="Get a list of stores"
cfargument name="store_passed" default="All"
cfset var qs = ""

cfquery datasource="#REQUEST.Source#" name="qs"
select '#store_passed#' as storenum, ''as storeloc, 1 as sortby
UNION
select distinct storenum, storenum+city+state as storeloc, 2 as sortby from store_table
order by sortby, storenum
/cfquery

cfreturn qs

/cffunction

Cup size   
Select size then click on coffee cup.
This entry was posted in Code, Coldfusion, Snippets, SQL and tagged , . Bookmark the permalink.