user3766930
user3766930

Reputation: 5839

How can I turn json data into html table in jquery?

I have the following code in jQuery:

                    $.ajax({
                        url: './getJson.php', 
                        type: "POST",
                        data: {
                            email: email
                        },
                        dataType:'text',
                        success: function(response)
                        {
                            alert(response)
                        }
                    });

and after running it on my webpage I see a popup with my json data, which has a structure:

[{"number":"1","id":"2","price":"100.70","date":"2015-10-18 03:00:00"},
{"number":"2","id":"2","price":"88.20","date":"2015-10-18 04:00:00"}]

And that happens when user enters my page. Instead I would like to fill a html table with that data, and the layout is already prepared (so far filled with dummy data):

                        <div class="panel-body">
                            <div class="dataTable_wrapper">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th>number</th>
                                            <th>id</th>
                                            <th>price</th>
                                            <th>date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="odd gradeX">
                                            <td>Trident</td>
                                            <td>Internet Explorer 4.0</td>
                                            <td>Win 95+</td>
                                            <td class="center">4</td>
                                        </tr>
                                        <tr class="even gradeC">
                                            <td>Trident</td>
                                            <td>Internet Explorer 5.0</td>
                                            <td>Win 95+</td>
                                            <td class="center">5</td>
                                        </tr>
                                        <tr class="odd gradeA">
                                            <td>Trident</td>
                                            <td>Internet Explorer 5.5</td>
                                            <td>Win 95+</td>
                                            <td class="center">5.5</td>
                                        </tr>

Now my question is - how can I substitute the dummy data with my own, fetched from json and display it always as a nice table to the user? Thanks!

Upvotes: 0

Views: 1126

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115272

  1. Parse the JSON string using $.parseJSON ( or JSON.parse ) or set dataType : 'json'
  2. Iterate over the parsed object using $.each()
  3. Generate tr with contents and append it to the table using appendTo() or append().

var data = '[{"number":"1","id":"2","price":"100.70","date":"2015-10-18 03:00:00"},{"number":"2","id":"2","price":"88.20","date":"2015-10-18 04:00:00"}]';

json = JSON.parse(data);

$.each(json, function(i, v) {
  $('<tr/>', {
    html: [$('<td/>', {
      text: v.number
    }), $('<td/>', {
      text: v.id
    }), $('<td/>', {
      text: v.price
    }), $('<td/>', {
      text: v.date
    })]
  }).appendTo('#dataTables-example tbody')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="panel-body">
  <div class="dataTable_wrapper">
    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
      <thead>
        <tr>
          <th>number</th>
          <th>id</th>
          <th>price</th>
          <th>date</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>

Upvotes: 3

Related Questions