Vikrant Vikrant - 4 years ago 198
Javascript Question

Bind JSON containing a dataset, to dropdownlist using JQUERY

I have a

JSON string
returned by a WCF service, which contains two tables in Data, as a string in it:


"Data": "{"Table":[{"holiday_date":null},{"holiday_date":1900},
"Response": 1000,
"ResponseCode": 1000,
"ResponseMessage": "Success",
"ResponseTime": "18-Mar-2015 15:29:55"

I've tried to bind it using
as below:

function ConsumeData(data) {
var response = $.parseJSON(data);

//code to bind data to ddl
var ddl = D.getElementById('ddlYears');
var opt = D.createElement("option");
opt.text = '--Select--';
opt.value = 0;
for (i = 0; i < response.Table.length; i++) {
opt = D.createElement("option");
opt.text = response.Table[i]['holiday_date'];
opt.value = response.Table[i]['holiday_date'];

How can I achieve the same with

Thank You!

Answer Source

I created solution myself! :)
Issue was with JSON object that contains GetYearsResult as a collection of tables and to find those tables, we have to specify:

  1. data.GetYearsResult.Data and
  2. data.GetYearsResult.MetaData.

Below function accepts json data, dropdown name and binds first table of dataset to dropdownlist and second one to textbox using Jquery.

var result = data.GetYearsResult.Data;
var ddl = D.getElementById('ddlYears');

function ConsumeData(result, ddl) {
   var response = $.parseJSON(result);
   var ddl = $('#ddlYears');
   $.each(response.Table, function (key, value) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download