• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Dynamically setting an option as selected in cfselect

Explorer ,
Feb 01, 2013 Feb 01, 2013

Copy link to clipboard

Copied

Hi,

I have a cfselect that is defined as such:

 

<cfselect name="ScheduledFollowup" >

<option value=""></option>

<cfloop index="fol" from="0" to="50">

  <option value="<cfoutput>#fol#</cfoutput>>

   <cfoutput>#fol#</cfoutput>

  </option>

</cfloop>

</cfselect>

What I need to do is insert a "selected" attribute on the option that matches a value retrieved in an earlier query.  I have tried imbedding a <cfif> statement but that doesn't work.

Any ideas?

Views

929

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Feb 02, 2013 Feb 02, 2013

OK. This code worked.

  <td valign="top">

<cfselect name="ScheduledFollowup" selected="#SelectVisit.FollowUpMonths#">

  <option value=""></option>

  <cfloop index="fol" from="0" to="50">

   <cfif fol EQ SelectVisit.FollowupMonths>

    <option value="<cfoutput>#fol#</cfoutput>" selected >

   <cfelse>

    <option value="<cfoutput>#fol#</cfoutput>" >

   </cfif>

    <cfoutput>#fol#</cfoutput>

   </option>

  </cfloop>

</cfselect>

  </td>

Votes

Translate

Translate
LEGEND ,
Feb 01, 2013 Feb 01, 2013

Copy link to clipboard

Copied

<cfselect> has a selected attribute.  It always works when  I use it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 01, 2013 Feb 01, 2013

Copy link to clipboard

Copied

I tried that. The documentation states that it only works when the cfselect is populated from a query.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2013 Feb 01, 2013

Copy link to clipboard

Copied

You could always fake a query for the #fol#.

<cfscript>

tmp = QueryNew("field1","{datatype}");

tmp2 = QueryAddRows(tmp,x); <!--- x being the number of rows --->

tmp3 = QuerySetCell(tmp,"field1","{value}",1);

tmp3 = QuerySetCell(tmp,"field1","{value}",2);

...

</cfscript>

<cfdump var="#tmp#">

<cfoutput query="tmp">

#field1#<br />

</cfoutput>

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 01, 2013 Feb 01, 2013

Copy link to clipboard

Copied

I have tried this without success:

<tr id="ScheduledFollowupRow">

  <td valign="top">Scheduled Follow Up<br/>(Number of Months)</td>

  <td valign="top">

<cfset Selected = "">

<cfselect name="ScheduledFollowup" >

  <option value=""></option>

  <cfloop index="fol" from="0" to="50">

   <cfif fol EQ #SelectVisit.FollowupMonths#>

    <cfset Selected = " selected">

   </cfif>

   <option value="<cfoutput>#fol# #Selected#</cfoutput>">

    <cfoutput>#fol#</cfoutput>

   </option>

  </cfloop>

</cfselect>

  </td>

</tr>

What am I doing wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 02, 2013 Feb 02, 2013

Copy link to clipboard

Copied

LATEST

OK. This code worked.

  <td valign="top">

<cfselect name="ScheduledFollowup" selected="#SelectVisit.FollowUpMonths#">

  <option value=""></option>

  <cfloop index="fol" from="0" to="50">

   <cfif fol EQ SelectVisit.FollowupMonths>

    <option value="<cfoutput>#fol#</cfoutput>" selected >

   <cfelse>

    <option value="<cfoutput>#fol#</cfoutput>" >

   </cfif>

    <cfoutput>#fol#</cfoutput>

   </option>

  </cfloop>

</cfselect>

  </td>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation