0

I have to create a grid with undefined columns which is based on Json data,

[{
    Name: "John",
    Designation: "Analyst",
    Age: 25, 
    Department: "IT"
},
{
    Name: "Matthew",
    Designation: "Manager", 
    Age: 38, 
    Department: "Accounts"
},
{
    Name: "Emma",
    Designation: "Senior Manager",
    Age: 40,
    Department: "HR"
}];

My desired output is as below:

As header:

Name | Designation | Age | Department

As datarow:

John | Analyst | 25 | IT

Please, anyone help me how I have to start and how to do it.

4 Answers 4

0

Duplicate Question How to put JSON data in html / javascript grid table [if you check before posting]
this is datatables --http://datatables.net/release-datatables/examples/data_sources/js_array.html
this is gridtable JS --http://backgridjs.com/#examples

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

Comments

0

here i have iterated and displayed the data in table format is this is what you are looking for Fiddle Demo

$table = "<table id = 'resultTable' border=1>";
$.each(data[0] , function(key, value){
   $table += "<td>"+key+"</td>";
});
$table += "<tr>";

for (var i = 0; i < data.length; i++) {

    $table += "<tr>";

    var it = data[i];



    $table += "<td>" + data[i].Name + "</td>";

    $table += "<td>" + data[i].Designation + "</td>";

    $table += "<td>" + data[i].Age + "</td>";

    $table += "<td>" + data[i].Department + "</td>";

    //alert(items[i].duration);
    $table += "</tr>";

}

$table += "</table>";

$('body').append($table);

Comments

0

The for...in loop will exactly do what you need.

Here is what the Mozilla Developer Network writes about it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

While inside the "for...in loop it's crucial that you test if the property has the specified property and that this property isn't inherited. (hasOwnProperty)

Working example: http://jsbin.com/ecoqax/1/edit

I'm using jQuery here:

var $theader = $("thead"),
    $tbody = $("tbody"),
    hasHeader = false,
    html = "";

  // data is the converted JSON data
  for (var i = 0; i < data.length; i++) {
    // the current person
    var person = data[i];

    html += '<tr>';

    // iterating over every property of the person object
    for (var prop in person) {
      if (person.hasOwnProperty(prop)) {
        // insert the header once
        if (hasHeader === false) {
          $theader.append($('<th>'+prop+'</th>'));
        }
        html += '<td>'+person[prop]+'</td>';
      }
    }

    // after the first iteration the header is added
    hasHeader = true;
    html += '</tr>';
  }

  $tbody.append($(html));

Comments

0
$.each(Jsonstring,function(){
  alert(JsonString.Name);
  alert(JsonString.Designation)
  alert(JsonString.Age)
  alert(JsonString.Department)
});

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.