简体   繁体   中英

Arrange JSON data in table format

In previous question my problem is resolved but now after that i am trying JSON data store in cookies and then retrieve that other variable and arrange it in table format, but i'm confuse that how i arrange it.

Demo Here

I don't know why you are trying to convert it json and again parsing, but this is the other way to directly get from the array and populate it to table.

$('#btnCart').click(function () {
    //var value = JSON.stringify(objArray);
    var table = "<table><tr><td>Id</td><td>Title</td><td></tr>"; 
     $.each(objArray, function (k, v) {
       table=table+"<tr><td>"+objArray[k].id +"</td><td>"+objArray[k].title +"</td></tr>";
     });
 table=table+"</table>";
 $('#divGrid').empty();
 $('#divGrid').append(table);
});

Updated Fiddle

convert it according to your requirement

js:

function getNews(){
   $.getJSON("services/news_service.php?action=getNews", function(json){

   $.each(json.news, function(){
        var news = '<tr>'+'<td width="52"><span class="news_item_div"> <img src="'+imagePath+this['news_icon']+'" alt="" name="news_icon" width="51" height="51" class="news_icon" id="news_icon" /></span></td>'+
    '<td width="128">' +this['news_headline']+ '</td>'+
  '</tr>';
      $('#news table').append(news);
                   });
   });


 }

html:

<div id="news">
        <table width="200" border="1" cellspacing="2" cellpadding="2"></table>
</div>
 1. error in closing span 


<span id='lblCart'></span>

2. Not properly assigned the values

table=table+"<tr><td>"+v.id +"</td><td>"+v.title +"</td></tr>";

Fiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM