My application needs to populate html selects with options from custom queries. I was trying to put the logic into a generic custom tag to do this so that my view is cleaner and more readable. I'm new to cfml.
Here is my code:
<!--- ```````````````````````````````````````````````````````````````````````````` --->
<!--- This tag will populate any SELECT with the OPTIONs pulled from the database. --->
<!--- The attributes of this tag are --->
<!--- data_source: The datasource for the query --->
<!--- table_source: The table used for the query --->
<!--- option_value_source: The DB cell to use for the value of the option. --->
<!--- option_inner_source: The DB cell to use for the value of the inner text. --->
<!--- ____________________________________________________________________________ --->
<cfquery name="option_query" datasource="#Attributes.data_source#">
SELECT * FROM #Attributes.table_source#
</cfquery>
<cfoutput query="option_query">
<option value="#Attributes.option_value_source#"> <!--- The cell to be used for the value of the option --->
#Attributes.option_inner_source# <!--- The cell to be used for the inner text of the option --->
</option>
</cfoutput>
I'm calling that with:
<label>
Class Name
<select name="className" id="className">
<cf_getOptions data_source="codify" table_source="classes" option_value_source="classes.id" option_inner_source="classes.name">
</select>
</label>
The result is that I get a SELECT populated with the word class.name for each result.
What is the most cfml way to do this (I assume people wouldn't normally use custom tags for this)? Is it possible to rewrite this code to get the correct result?
You're close (and good on you for using custom tags, btw).
You need to tell CF you want the column value from the query, not just the value of the passed-in attribute. So it'd be this:
<cfoutput query="option_query">
<option value="#option_query[Attributes.option_value_source][currentRow]#">
#option_query[Attributes.option_inner_source][currentRow]#
</option>
</cfoutput>
One can only use a direct reference to a column name the way you originally had, but you need an indirect reference here, which means the code gets a bit more cumbersome.