简体   繁体   English

如何使用jQuery将JSON数组中的数据转换为表格式?

[英]How to get data from json array into table format using jquery?

I want to get data into table from json array i have data in console see below but I'm no table to get data into table. 我想从json数组中获取数据到表中,我在控制台中看到了数据,如下所示,但我没有表可以将数据获取到表中。

Data is displaying in console screenshot 数据显示在控制台屏幕截图中

在此处输入图片说明

What I tried:- 我试过的

$.ajax({
                       url:'/admin/checkavailability',
                       type:'POST',
                       data: { fromdate, enddate, productoptionId },
                       success: function (d) {
                           console.log(d);
                           if (d != null) {                        


                               for (var i = 0; i < d.length; i++) {
                                   tr = $('<tr/>');
                                   tr.append("<td>" + d[i].Date + "</td>");
                                   tr.append("<td>" + d[i].RetailPrice + "</td>");
                                   tr.append("<td>" + d[i].Price + "</td>");
                                   $('table#tblbindavailabledates').append(tr);
                               }

                               $('#myModal').modal('show');
                               //$(d)

                           }
                       }

Use $.each rather than using for loop. 使用$ .each而不是使用for循环。 And try to extract data by d.ProductOptionAvailability. 并尝试通过d.ProductOptionAvailability提取数据。 if it doesn't work then use d.ProductOptionAvailabilies.ProductOptionAvailability. 如果不起作用,则使用d.ProductOptionAvailabilies.ProductOptionAvailability。

  $.ajax({
          url:'/admin/checkavailability',
          type:'POST',
           data: { fromdate, enddate, productoptionId },
           success: function (d) {
                 if (d != null) {   
                 var content = '' ;
                 $.each(d.ProductOptionAvailability, function (i, obj) { 
                 // if d.ProductOptionAvailability doesn't work then use d.ProductOptionAvailabilies.ProductOptionAvailability
                 var content = '<tr>' ;
                 content += '<td  >' + obj.Date +'</td><td  >' + obj.RetailPrice+'</td><td  >' + obj.Price+'</td><td  >' + obj.Quantity+'</td><td  >' + obj.Status+'</td>';
                 content += '</tr>';
                  $('table#tblbindavailabledates').append(content);
                 });
              }             
           $('#myModal').modal('show');

         }
       });

I bet the problem is in this line 我敢打赌,问题出在这一行

$('t#tblbindavailabledates').append(tr);

There is no native HTML element by the tag <t> . 标签<t>没有本地HTML元素。 mybe you meant to say: 我的意思是说:

$('table#tblbindavailabledates').append(tr);
// or simply
$('#tblbindavailabledates').append(tr);

Also, the following line 另外,以下行

 ...
 data: { fromdate, enddate, productoptionId },
 ....

It should be something like: 应该是这样的:

data: { fromdate:fromdate, enddate: enddate, productoptionId: productoptionId },

or if you meant it to be an array: 或者如果您想将其作为数组:

 data: [ fromdate, enddate, productoptionId ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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