0

This is HTML markup:

<table id="prodTable" class="table table-bordered" style="color:white">
  <thead class="thead">
    <tr class="font-weight-bold">
      <th>No.</th>
      <th>Name</th>
      <th>Category</th>
      <th>Slug</th>
      <th>Price</th>
      <th>GST Price</th>
      <th>Keywords</th>
      <th>Specification</th>
      <th>Description</th>
      <th>Attributes</th>
      <th>Available Stock</th>
      <th>Payment Method</th>
      <th>Approval Status</th>
      <th>Approve</th>
      <th>Deal of the Day</th>
      <th>Edit & Delete</th>
    </tr>
  </thead>
  <tbody class="tbody">
     <tr>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td><lable><input type="checkbox" id="approveProd" name="approval" value="approval">Approve</label></td>
       <td><label><input type="checkbox" id="DealoftheDay" name="dealoftheday" value="dealoftheday">Deal of the Day</label></td>
       <td><button class="btn btn-outline-info" type="button"><i class="fa fa-pencil-square"></i></button> <button class="btn btn-outline-danger" type="button"><i class="fa fa-trash"></i></button></td>
    </tr>
  </tbody>
</table>

and my js is

function getProductsData() {
    var data = "";
    var type = "application/x-www-form-urlencoded";
    var url = "get_product_data";
    var asyn = "true";
    var method = "POST";
    var respCallback = function(resp){
        var proddata = JSON.parse(resp);
        var table = document.getElementById('prodTable');
        console.log(table);
        for (var i = 0; i < proddata.length; i++) {
            console.log(proddata[i]);
        };
    }
    var res=serverRequest(data,method,url,asyn,type,respCallback);
}

the getProductsData() is a onload function written in the body and am getting the response as JSON.

I need to know how to populate the JSON response into the table. Am new to js and i dont need jquery code. Because am learning js.

Thanks in advance.

4
  • If you are using just JS, then a good approach would be to render the table from JS. Check this answer: stackoverflow.com/questions/16126357/… Commented Nov 7, 2019 at 7:25
  • Can you show us the short raw result of the proddata Commented Nov 7, 2019 at 7:26
  • NVM my first comment, Akash link will help you Commented Nov 7, 2019 at 7:28
  • Possible duplicate of Create HTML table using Javascript Commented Nov 7, 2019 at 7:29

1 Answer 1

1

Here is the short example which will help you, how to append the json data in HTML Table.

Example :-

        function append_json_data(data){
            var table = document.getElementById('prodTable');
            data.forEach((object) => {
                var tr = document.createElement('tr');
                tr.innerHTML = '<td>' + object.No + '</td>' +
                '<td>' + object.Name + '</td>' +
                '<td>' + object.Category + '</td>' +
                '<td>' + object.Slug + '</td>';
                '<td>' + object.GST_Price + '</td>';
                table.appendChild(tr);
            });
        }

And further you can apply the td tag for other elements as well, As I am showing you how to append the data to HTML you can add the tag data after 'GST_Price' same as above.

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

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.