Results 1 to 2 of 2

Thread: Sorting a <select> list box (/w htmlOptionElement(s))

  1. #1

    Thread Starter
    Hyperactive Member Working.Net's Avatar
    Join Date
    Aug 2010
    Posts
    389

    Sorting a <select> list box (/w htmlOptionElement(s))

    Hi,

    I am modifying a bit of existing JavaScript that populates a <Select> list box. The reason is, that I would like the list box to be sorted by the text rather than the values. The "results" variable is populated with an array of htmlOptionElement(s), something I am a complete noob in. Based on a suggestion from the other side of the planet (Australia, this is significant because I am hoping to get an answer today and not on Monday ) I added the function(objA, objB) shown below to the result.sort method. It is not working and I am pretty sure it is because the "sort property" (MH_NO) is incorrect. I think it is supposed to be like the field name stored in the htmlOptionElement but I am completely unsure about that and I could not find any further info on it. Do you guys know what this "sort property" is and if it is a field name how I can extract that field name so I can see what it is supposed to be?

    Thanks in advance for your help

    Code:
    function OnReadyStateChange() //Search for SN34N15
            {
                var ready = queryReqHandler.readyState;
                if (ready == READY_STATE_COMPLETE)
                {
                    results = queryReqHandler.responseText.parseJSON();
                    results.sort(function (objA, objB) {
                        if (objA.MH_NO < objB.MH_NO) {
                            return -1; //Less than 
                        } else if (objA.MH_NO > objB.MH_NO) {
                            return 1; //Greater than 
                        } else {
                            return 0; //Equal 
                        }
                    });
    
                    var resultSelect = document.getElementById("resultSelect");
                    resultSelect.options.length = 0;
                    for (var i = 0; i < results.length; i++) {
                        resultSelect.options[i] = new Option(results[i].displayValue, i, false, false);
    
                    OnResultChange();
                    document.getElementById("executeBtn").disabled = false;
                    document.getElementById("busyImg").src = NOT_BUSY_IMAGE;
                    queryReqHandler = null;
                }
            }

    As I stand here, on the fringes of my understanding, and look out over the
    vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Sorting a <select> list box (/w htmlOptionElement(s))

    Just a guess:
    Code:
    results.sort(function (objA, objB) {
    	return objA.label < objB.label ? -1 : objA.label > objB.label ? 1 : 0;
    });
    Last edited by penagate; Feb 5th, 2012 at 12:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width