(JavaScript) How Can I do drop down list sorting?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhavanirayala
    New Member
    • Jul 2007
    • 25

    (JavaScript) How Can I do drop down list sorting?

    Hi,

    I have drop down list which is populating form the xml by using AJAX.
    After getting the text in that opetion I want to do sorting in java script.
    How can I do?
    please help me.

    Regards,
    Bhavani
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    the best way would be to put the optionvalues and texts into an js-array ... sort this now ... and after this is sorted ... populate your menulist with the sorted values

    kind regards

    Comment

    • bhavanirayala
      New Member
      • Jul 2007
      • 25

      #3
      Originally posted by gits
      hi ...

      the best way would be to put the optionvalues and texts into an js-array ... sort this now ... and after this is sorted ... populate your menulist with the sorted values

      kind regards
      Hi gits,

      i have the xml like[code=xml]
      <LETTER file="ATM_Lette r.doc" text="ATM Letter"/>
      <LETTER file="Collatera l_Correction_Le tter_and_CCS.do c" text="Collatera l Correction Letter and CCS"/>
      ............... ......
      ..............[/code]
      ............... .......

      I am populating the xml values in menu options like following.
      var menuOption = new Option(text , file, false, false);

      How can I convert this Option to Array?
      Last edited by pbmods; Sep 11 '07, 01:13 PM. Reason: Added CODE tags.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Bhavanirayala.

        Please use CODE tags when posting source code:

        &#91;CODE=xm l]
        XML code goes here.
        &#91;/CODE]

        This is not the first time you've been asked.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          you don't have to convert this ... or whatever. simply put them into a list that is indexed with the key you want to sort:

          [CODE=javascript]var list = {};
          var sort_list = [];

          // everytime you have retrieved a new option
          // you do:
          list[text] = menuOption;

          // where menuOption is the ref to the new Option() you already have
          // AND !!!
          sort_list.push( text);
          [/CODE]

          now you may simply sort the sort_list with the array sort() method.
          after that ... you loop through the sorted list, and get the corresponding option from our 'option'-list:

          [CODE=javascript]for (var i = 0; i < sort_list.lengh t; i++) {
          var item = sort_list[i];
          var option_to_add = list[item];
          // add the option here to your menulist :)
          }
          [/CODE]
          kind regards

          Comment

          Working...