xml, jquery, ajax

I would like to make the dropdown sorted but not sure how to do it right. For those of you already helped me in the past. Please understand I ran with some trouble with your solutions and always get different results.

I want to re-post and hope to get working sample. Thanks


<select></select>

function fnGetBondNameList(bondState) {
            $.ajax({
                url: "http://localhost:5489/BondList.xml",
                success: function (xml) {
                    parseSelectXML(xml,"bondName","Bond",bondState);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert("Status: " + xhr.status);
                    alert("Error: " + thrownError);
                }
            });
    } 

function parseSelectXML(xml, selectid, xmlnode, bondState) {
    var selecthtml = "", selectBondName = "";
    var options = [];
    $(xml).find(xmlnode).each(function () {
        if (bondState == $(this).find('GeneralInformation').find('State').text()) {
            selectBondName = $(this).find('GeneralInformation').find('Name').text();
        }
    });
    $("#" + selectid).prop("disabled", false);
    $('#' + selectid).html(selecthtml);
    $('#' + selectid + ' option:first').attr('selected', 'selected');
}

<?xml version="1.0" encoding="utf-8"?>
<Container>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Zebra</Name>
  </GeneralInformation>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Aardvark</Name>
  </GeneralInformation>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Marmoset</Name>
  </GeneralInformation>
</Container>

Open in new window

LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayur AgarwalCommented:
You can insert text in an array and then sort the array using array.sort() method. Loop over the array items and fill the dropdown.
If possible,share the jsfiddle or complete code of your page as from above code it is very difficult to understand what you want to achieve. Also you have declared selecthtml variable but never fill value in it.
0
Chris StanyonWebDevCommented:
You might want to clarify your needs. Currently your code seems to be missing some parts.

Firstly, it looks like you're trying to find all nodes called "Bond" in you XML - there are none!

Secondly, it looks like you're trying to set the HTML of an element to the value stored in the selecthtml variable. That variable is never set to anything othe than an empty string.

You haven't really said what your Dropdown should contain or what you want it sorted on (text / value etc).
0
ITsolutionWizardAuthor Commented:
use this xml instead please

<Bond>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Zebra</Name>
  </GeneralInformation>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Aardvark</Name>
  </GeneralInformation>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Marmoset</Name>
  </GeneralInformation>
</Bond>

Open in new window

0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Chris StanyonWebDevCommented:
That still doesn't really make a lot of sense. In your code above, you're looping through the <bond> nodes but in the XML you've just provided, there's only one <bond> node. You then go on to 'find' the state. You're not looping through that so it will only find the first one. If it matches the state, you then assign the first 'name' it finds to a variable called selectBondName but then do nothing with it.

You also haven't cleared up exactly what you want adding to your dropdown.

Still need clarity on exactly what you need.
0
ITsolutionWizardAuthor Commented:
Below should be fine.

<Bond>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Zebra</Name>
  </GeneralInformation>
<Bond>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Aardvark</Name>
  </GeneralInformation>
</Bond>
<Bond>
  <GeneralInformation UI="BondInfo">
    <State>CA</State>
    <Name>Marmoset</Name>
  </GeneralInformation>
</Bond> 

Open in new window

0
Chris StanyonWebDevCommented:
OK. We're slowly making progress, but please read through my previous comments - you STILL haven't told us what you want in the Dropdown. You've told us you want it sorting but nothing in your code shows any actual values for the dropdown. I'm guessing that you want to populate the dropdown somehow from your XML file, but we've no idea how.

Please take a few minutes to explain clearly what you're trying to do
0
ITsolutionWizardAuthor Commented:
html: <select id="state"></select>
sort the state in xml
<select id="personName">
<option>Aardvark</option>
<option>Marmoset</option>
<option>Zebra</option>
</select>
0
Chris StanyonWebDevCommented:
OK. Still guessing on parts of this, but here goes:

function parseSelectXML(xml, selectid, xmlnode, bondState) {
    var selecthtml = '';
    var values = new Array();

    $(xml).find(xmlnode).each(function () {
        if (bondState == $(this).find('GeneralInformation').find('State').text()) {
            values.push($(this).find('GeneralInformation').find('Name').text());
        }
    });

    $.each(values.sort(), function(i, v) {
        selecthtml += '<option value="'+ v +'">'+ v +'</option>';
    });

    $('#' + selectid)
        .html(selecthtml)
        .prop('disabled', false);

    $('#' + selectid + ' option:first').attr('selected', 'selected');
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.