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

Select dropdown list show last record

Engaged ,
Aug 07, 2013 Aug 07, 2013

Copy link to clipboard

Copied

I have following select contorl on the form and it has a record set the code like following,


<select id="MySelect">
       <cfoutput query="spList">
         <option value="#spList.MyID#" <cfif (isDefined("spList.MyID") AND spList.MyID EQ spList.MyID)

>selected="selected"</cfif>>#spList.MyNUMBER# - #spList.<MyNAME#</option>
       </cfoutput>
     </select>

The form show lasr record of the record set on the list.

I would like to know are there any way to let the dropdown list show the first record when the form showup.

Your help and information is great appreciated,

Regards,

Iccsi,

Views

548

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 ,
Aug 07, 2013 Aug 07, 2013

Copy link to clipboard

Copied

You should read the HTML spec for how HTML elements work. Option: http://www.w3.org/html/wg/drafts/html/master/forms.html#the-option-element

So what you do is to set the "selected" attribute of the option you want the Select to have selected. You already seem to have some logic to output the selected="selected" text, but I guess if it's not working, your logic is bung. Superficially, it looks correct though.


What does the actual resultant mark-up look like?  Looking at that might cast somer light on the scene.

--

Adam

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
Engaged ,
Aug 08, 2013 Aug 08, 2013

Copy link to clipboard

Copied

LATEST

<option value="#spList.MyID#" <cfif (isDefined("spList.MyID") AND spList.MyID EQ spList.MyID)>selected="selected"</cfif>>#spList.MyNUMBER# - #spList.<MyNAME#</option>

There's two fundamental things wrong here.

Firstly you refer to #spList.MyID#.  Then you say isDefined("spList.MyID").  It must be defined, otherwise the code would error when you referred to it the first time.

Secondly, you then say "spList.MyID EQ spList.MyID"  - uh, that's like saying if A EQ A - it's always true.  I'm assuming you have some other value set somewhere for the ID of one particular row in your query you want to have selected.  That's what you should use instead.

This should end up looking something more like:

<option value="#spList.MyID#" <cfif isDefined("someOtherID") AND spList.MyID EQ someOtherID>selected="selected"</cfif>>#spList.MyNUMBER# - #spList.<MyNAME#</option>

Although a further improvement - isDefined() is generally considered less than optimal these days, and it's better to use structKeyExists() instead.  e.g.

<option value="#spList.MyID#" <cfif structKeyExists(variables, "someOtherID") AND spList.MyID EQ someOtherID>selected="selected"</cfif>>#spList.MyNUMBER# - #spList.<MyNAME#</option>

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