2 Replies Latest reply on Aug 8, 2013 1:35 AM by duncancumming

    Select dropdown list show last record

    iccsi Level 1

      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,

        • 1. Re: Select dropdown list show last record
          Adam Cameron. Level 5

          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

          • 2. Re: Select dropdown list show last record
            duncancumming Level 3

            <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>

            1 person found this helpful