2

My JSON looks like this:-

{
"tblDetails": [{
    "id": 1,
    "rowIndex": 1,
    "colIndex": 1
}, 
{
    "id": 2,
    "rowIndex": 1,
    "colIndex": 2
}, 
{
    "id": 3,
    "rowIndex": 1,
    "colIndex": 4
}, 
{
    "id": 4,
    "rowIndex": 1,
    "colIndex": 6
}
]}

And HTML Structure as below:-

<table id="tblLayout"><tbody> </tbody></table>

I am using jQuery ajax to fetch JSON data.

 $.ajax(
 { 
   url : "/hello/data.json",
   type: "GET",
   success: function(data)
   {
       var tbl  = data.tblDetails;
       tbl.forEach(item)
       {
       var html = item.id;
       $('#tblLayout> tbody > tr:eq('+(item.rowIndex)+') td:eq('+(item.colIndex)+')').append(html);
       }

   },
   error:function(e)
   {

   }
 })

Now how do I append the html at the specified row-col in the table tblLayout.

2 Answers 2

1

I am not sure if this is the exact solution but this is what I did for the required layout.

I replaced table with ul as:-

<ul id="tblLayout"></ul>

jQuery ajax

$.ajax(
{ 
url : "/hello/data.json",
type: "GET",
success: function(data)
{
   var tbl  = data.tblDetails;
   tbl.forEach(item)
   {
   var html ="<li class='seat style='position:absolute;top:" + (item.rowIndex * 50) + "px;left:" + (item.colIndex * 50) + "px;'>" + item.id+ "</li>";

   $('#tblLayout>).append(html);

   }
   },
  error:function(e)
  {

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

Comments

0

You get data from "data.json" in JSON format. Try this:

  $.ajax({
url: "data.json",
type: "GET",
success: function (data) {
  var tbl = JSON.parse(data).tblDetails;
  $.each(tbl, function (k, v) {
    var html = v.id;
    $('#tblLayout> tbody > tr:eq(' + (v.rowIndex) + ') td:eq(' + (v.colIndex) + ')').append(html);
  });

},
error: function (e) {

}

});

6 Comments

It would be perfect if you could post the example into an (JSFiddle)[jsfiddle.net] to show it working
@Igor Danylov... I checked with this...but it doesn't rendered
@Chetan Are you want rewrite cell or add "id" to exists text?
@IgorDanylov.. on page load or intially all I have is empty table <table id='tblLayout'> </table>
@IgorDanylov..What you think about my ans
|

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.