<cfselect> binding

Adding options to a binded cfselect.

<cfselect class=“input” name=“device” display=“device” value=“id” bind=“cfc:inventory.getDevices()” bindonload=“true” />

<cffunction name=“getDevices” access=“remote” returntype=“query”>

    &lt;cfquery name="devices" datasource="intranet"&gt;
    	select * from devices d
    &lt;/cfquery&gt;
    
    &lt;cfreturn devices&gt;

</cffunction>

i would like to put a Please Select Device… option on the top of this select list. Anyone know how to do this?

Thanks in advance

Want to add one more thing… If I make a selection in a select field, how can I deselect it? Is that possible?

I don’t use the bind to do it.


<cfquery name="devices" datasource="intranet">
select * from devices d
</cfquery>


<cfselect class="input" name="device" display="device" >
<option value="">Select one</option>
<cfoutput query="devices">
  <option value="#id#">#id#</option>
</cfoutput>
</cfselect>

I wonder if you could put the bind on the options rather than
the select? Or could you possibly run javascript after the build and insert a row option?


document.formname.device.options[0]=new Option("Select", "", true, false)

I have to use the bind. I have othe select that get their info based off of this bind. option does not have the bind attribute… So I don’t thik that would work.

I think you have to do it yourself. The most common way I’ve seen is to add it to your query via a UNION.

<cfquery name=“devices” datasource=“intranet”>
SELECT 2 AS SortBy, ID FROM devices
UNION ALL
SELECT 1 AS SortBy, ‘Please Select Device’ AS ID
ORDER BY SortBy
</cfquery>

If you read the documentation there is an attribute called QueryPosition.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_14.html

If you populate the options list with a query and use HTML option child tags to specify additional entries, this attribute determines the location of the items from the query relative to the items from the option tags:

* above: puts the query items above the options items.
* below: puts the query items below the options items.

Try this:


<cfselect bind... queryPosition="below">
<option value="">Select one</option>
</cfselect>

Want to add one more thing… If I make a selection in a select field, how can I deselect it? Is that possible?

To deselect it, select something else, or the “select one” option. if you have an item that is bound to the select, it will reset also. Example you could have a select of states that when selected will alter a list of cities. When you change states the cities list also changes.

If you read the documentation there is an attribute called QueryPosition…

… which doesn’t work with bind’s. A bind replaces the existing elements, so all items to be listed must already be in the result returned from the cfc query.

Yes, I knew that the queryposition attribute would not work. I guess I will have to use javascript and a reset but to clear the field… :frowning:

Just saw this:

<cfquery name=“devices” datasource=“intranet”>
SELECT 2 AS SortBy, ID FROM devices
UNION ALL
SELECT 1 AS SortBy, ‘Please Select Device’ AS ID
ORDER BY SortBy
</cfquery>

I’ll give this a try.

I guess I will have to use javascript and a reset but to clear the field…

I’m not sure how else you’d do it. But what’s wrong with javascript? :slight_smile: IIRC, deselecting everything’s a one-liner:

document.getElementById('idOfYourList').selectedIndex = -1;

Is this list bound to anything else (ie other fields)?

oops, QueryPosition dies with bind. I had used it when calling a cfm which invoked the cfc and that worked because it passed the full select string.

But you can add this below your query in the CFC to add a row


	<cfset var q = "">
	
	
	<cfquery name="q_devices" datasource="intranet">
           select * from devices d
         </cfquery>


	
	[B]<cfset temp = QueryAddRow(q_devices)> 
	<cfset temp = QuerySetCell(q_devices, "ID", "select one", 1)> [/B]


	<cfreturn q>

<cfset temp = QueryAddRow(q_devices)>

Should be
<cfset temp = QueryAddRow(q_devices, 1)>

But you can add this below your query in the CFC to add a row

Unfortunately, that will overwrite the existing value in row 1 (and add a blank record to the end of the query).

Dang it. I forget I have null default records which it overwrote.