3

I have the following JSON data and I want to populate an html select element from it, please I don't see how to use a for each loop with such a json data. Thanks.

//JSON

[{
    "group": "US (Common)",
    "zones": [{
        "value": "America/Puerto_Rico",
        "name": "Puerto Rico (Atlantic)"
    }, {
        "value": "Pacific/Honolulu",
        "name": "Honolulu (Hawaii)"
    }]
}, {
    "group": "Africa",
    "zones": [{
        "value": "Africa/Tunis",
        "name": "Tunis"
    }, {
        "value": "Africa/Windhoek",
        "name": "Windhoek"
    }]
}]

and the html select element looks like :

<select id="timeZones"></select>

and this is JS code that is not working :

var $select = $('#timeZones');
$.ajax({
    url: 'timezones.json',
    dataType: 'JSON',
    success: function (data) {
        $.each(data, function (i, val) {
            $select.append('<OPTGROUP LABEL="' + val[i].group + '"><OPTION VALUE="' + val[i].zone[i].value + '">' + val[i].zone[i].name + '</OPTION></OPTGROUP>');
        })
    },
    error: function () {
        alert("JSON ERROR");
    }
});
1
  • Where are you getting the json data from? Or what web framework are you using? Commented Oct 31, 2012 at 16:38

3 Answers 3

2

Its easier to do two each functions on nested nodes

 success: function (data) {
    $.each(data, function (i, val) {
        bar = '<OPTGROUP LABEL="' + val.group + '">';      
        $.each(val.zones, function(zoneID,zoneData) {
            bar += '<OPTION VALUE="' + zoneData.value + '">' + zoneData.name + '</OPTION>';  
        });
        bar += '</OPTGROUP>';
        $('#timeZones').append(bar);
   });    

}

here is a jsfiddle of the working solution http://jsfiddle.net/hZsQS/132/

However you may want to look at things like jsrender for templating

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Thanks for the answer but It did not work, my select element is still empty, I tried some debug by doing as following: var $select = $('#timeZones'); $.ajax({ url: 'timezones.json', dataType:'JSON', success: function (data) { $.each(data, function (i, val) { alert(val.group); }); }, error:function(){ alert("JSON ERROR"); } });
i just fixed the syntax AGAIN (extra double quote in front of LABEL), sorry bout that, included a jsfiddle just to verify its up to snuff
really, really OFF day, everything is now fixed in the answer
0

It looks like you'll have to have nested $.each loops; one to loop through the groups and add the <optgroup>, and the inner loop to loop through the zones and add each <option> to the appropriate <optgroup>.

Comments

0

You can use Knockout Framework to map an json String to a select box (very less code).

example: JavaScript:

var json = '{"showFilter":["notStarted","running"],"whatEver":["one","two"]}';
  var model = ko.mapping.fromJSON(json);
  ko.applyBindings(model);

HTML:

<p>Filter: <select data-bind="options: showFilter"></select></p>
<p>WhatEver: <select data-bind="options: whatEver"></select></p>

see also: http://knockoutjs.com/documentation/options-binding.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.