Search

Thursday, December 21, 2006

How to Create a Alpha Range Drop-Down List

Let's say you have a query that returns a bunch of users by last name. Now you want to be able to let the user pick users with last names starting with 'A', 'B' or 'C'. The code below will create a drop down options like 'A-C', 'D-F', 'G-I'...etc.
<!--- Setup the alphabet list --->
<cfset alphaFilterList="a b c d e f g h i j k l m n o p q r s t u v w x y z">

<!--- Loop through the alphabet list --->
<cfloop from=1 to=26 step=1 index="listPosition">
    <!--- Get the current letter in the alphabet --->
    <cfset alpha=ucase(listgetat(alphaFilterList, listPosition, ' '))>

    <!--- Build a list of three letters --->
    <cfif currentItems eq ''>
        <cfset currentItems=alpha>
    <cfelse>
        <cfset currentItems=currentItems & '-' & alpha>
    </cfif>

    <!--- If there are 3 letters in the current list, display them --->
    <cfif listPosition mod 3 eq 0>
        <option value="#currentItems#">#currentItems#</option>
        <cfset currentItems=''>
    <!--- If the loop iteration is at the last position of the alphabet, display the items --->
    <cfelseif listPosition eq 26>
        <option value="#currentItems#">#currentItems#</option>
    </cfif>
</cfloop>
// //]]>