如何使用jQuery将json数组中的数据转换为表格式? [英] How to get data from json array into table format using jquery?

查看:240
本文介绍了如何使用jQuery将json数组中的数据转换为表格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从json数组中获取数据到表中,我在控制台中有数据,请参见下面,但是我没有表可以将数据获取到表中.

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.

数据显示在控制台屏幕截图中

Data is displaying in console screenshot

我尝试过的操作:-

$.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)

                           }
                       }

推荐答案

使用$ .each而不是使用for循环.并尝试通过d.ProductOptionAvailability提取数据.如果不起作用,请使用d.ProductOptionAvailabilies.ProductOptionAvailability.

Use $.each rather than using for loop. And try to extract data by d.ProductOptionAvailability. if it doesn't work then use 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');

         }
       });

这篇关于如何使用jQuery将json数组中的数据转换为表格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆