Mr. C
Mr. C

Reputation: 1710

JQuery fill DropDownList with JSON data

Let's say I have this data returned from a backend service layer:

["2", "3", "7", "14"]

First question. Is this considered valid JSON data? Json.org says it is, but I cannot find any references to it on Google, SO, etc...

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

I want to be able to take these values and add them to an already existing DropDownList object OnLoad using JQuery.

$.getJSON("http://www.example.com/path-to-get-data", function(data) { 
  //iterate through each object and add it to the list.
  //ideally, the key and value (string to display to the end user) would be the same.
});

I took a look at this thread, but it has objects, not just an array. Should I use parseJSON versus getJSON?

Thanks in advance!

Upvotes: 9

Views: 63447

Answers (6)

 var arr = [ 2, 3, 7, 14];
 var dropdown = ('#myselect');
 dropdown.empty();
 dropdown.append($('<option value=''>--Please Select--</option>'));
 $.each(arr, function(index, value) {
 dropdown.append($('<option value='+index+'>'+value+'</option>'));
 });`

Upvotes: 0

Blaise
Blaise

Reputation: 22202

var arr = [ 2, 3, 7, 14];
$.each(arr, function(index, value) {
     ('#myselect').append($('<option>').text(value).attr('value', index));
});

Please also take a look at Mr. C's solution:

$('<option></option>').val(item).html(item)

His way of manipulating options is new to me, and more elegant.

Upvotes: 13

Sushama Pradhan
Sushama Pradhan

Reputation: 1

For me, this one worked.

$("#ddlCaseType").append("<option>" + txt + "</option>");

Upvotes: -4

Deepak Kothari
Deepak Kothari

Reputation: 1753

This is the HiddenField declaration,which is useful to Store The JSON String

   <asp:HiddenField ID="hdnBankStatus" runat="server" />

This is the Dropdownlist declaration

   <select id="optStatus" name="optStatus" runat="server">
        </select> 

These lines will initialize Hiddenfield value with the sorted list(Assume that list contains the key value pairs in sorted order) which is then serialized using JSON serializer

   sl = new SortedList();
   hdnBankStatus.Value = jsonSerialiser.Serialize(sl);

These lines will use Elements of JSON String One by one and populate the dropdown with Values.

   $(document).ready(function () {  

   var myOptions = $.parseJSON($('#hdnBankStatus').val());
        var mySelect = $('#optStatus');
        $.each(myOptions, function (val, text) {
            mySelect.append(
    $('<option></option>').val(text).html(val)
        );
        });
   }

Upvotes: 1

Mr. C
Mr. C

Reputation: 1710

My solution was based off of Blaise's idea.

//populate the Drop Down List
$.getJSON("http://www.example.com/api/service", function (data) {
     $.each(data, function (index, item) {
         $('#dropDownList').append(
              $('<option></option>').val(item).html(item)
          );
     });
 });

Upvotes: 11

Rodion  Shotskiy
Rodion Shotskiy

Reputation: 27

This is not correct JSON, it must be this style:

{"0": "2", "1" : "3", "2" : "7", "3" : "14"}

You could use that sample to procceed you response:

var response = "[2, 3, 7, 14]";
eval("var tmp = " + response); 
console.log(tmp); 

Upvotes: 2

Related Questions