Link to home
Start Free TrialLog in
Avatar of CFbubu
CFbubu

asked on

Coldfusion + Javascript Question about Lists

Hi Folks,

I have a query below that outputs a list of cities.

<UL id="list">
<cfoutput query="getresults" group="City_ID" >

<LI><a href="somepage.cfm?City_ID=#City_ID#">#Cityname#</a></LI>

 </cfoutput>

</UL>

[RESULT]

Amstad
Alron
Adderson
Belleview
Banneris
Brentwood
Charstone
Cimbran
..
...
...
Zanker


I am wondering if if were possible to use javascript combined such that the output will look like that below:

[Desired Output]
A
B
C
..
..
Z

The desired output above shows the list like a simple alphabatized list, and when the user clicks on the letter A, the list will 'expand' and will show all the cities starting with 'A' as seen below:

A
Amstad
Alron
Adderson

B
C
D
.
..
Z

I have searched high and low around the internet and saw someone use a javascript code like such below to make an alphabatized list.....but I am not familiar with javascript and cannot seem to get it to work. In addition, I am hoping to make it work as an 'expanding' list as examplified above.

It would be awesome if someone could help me make this a reality! Thanks so much!

[Javascript code below]


var list = { letters: [] };    //object to collect the li elements and a list of initial letters
$("#list").children("li").each(function(){
    var itmLetter = $(this).text().substring(0,1).toUpperCase();
    if (!(itmLetter in list)) {
        list[itmLetter] = [];
        list.letters.push(itmLetter);
    }
    list[itmLetter].push($(this));    //add li element to the letter's array in the list object
});
 
list.letters.sort();    //sort all available letters to iterate over them
$.each(list.letters, function(i, letter){
    list[letter].sort(function(a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());    //sort li elements of one letter
    });
    var ul = $("<ul/>");    //create new dom element and add li elements
    $.each(list[letter], function(idx, itm){
        ul.append(itm);
    });
    $("#list").append($("<li/>").append($("<a/>").attr("name", letter.toLowerCase()).addClass("title").html(letter)).append(ul));    //add the list to a new li and to #list ul
});
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFbubu
CFbubu

ASKER

OMG.....you guys are ALL awesome! Thanks so very much for your generosity in helping with my question :)

I wish I could award everyone here who posted solutions all the full award points!

I think all 3 posted solutions were very viable, and I will be implementing all your different suggestions. It will take a little time to decide which would suit my style and purpose best, but I am thrilled at the options given here.

And _agx_ , why does it not surprise me that you are always there to help me out. I wish I had a way to contact you ...and no, I would not harass you with my issues:P I think I have an interesting proposition that may turn up in the near future, and I am hoping that I could tell you more about it...:) Anyways, no worries if you so decide for our contact to remain here. I am just glad to have you around!

Thank you everyone!