如何将json数组响应附加到引导表

时间:2019-01-26 12:24:16

标签: jquery ajax laravel

我对代码有疑问,我在多数组json响应中已经有一个结果,唯一的问题是将结果追加到表上。

在我的回答中,我想将select_order_details附加到表中,以及如何使用foreach显示所有结果并将结果附加到表中。 response result

我的HTML:

<table class="table table-striped table-bordered" style="width:100%;">
<thead>
    <tr font-size: 14px; ">
        <th scope="col">Menu Image</th>
        <th scope="col">Menu Name</th>
        <th scope="col">Quantity</th>
        <th scope="col">Subtotal</th>
    </tr>
</thead>
<tbody style=" font-size:14px;">
    <tr>
        <td></td>
    </tr>
</tbody>

我的Ajax功能:

    $('button#gather_customer_order').on('click',function()
  {
      var order_id = $(this).attr('data-order-id');
      var customer_id = $(this).attr('data-customer-id');



      $.ajax({
          url:'/customer_detail_ordering_logic',
          type:'GET',
          data:{order_id:order_id,customer_id:customer_id},
          success:function(response){

              var response_customer_id = response[0].customer_details_id[0].customer_id;
              var response_order_id = response[0].customer_details_id[0].order_id;

                $.ajax({
                    url:'/fetch_detail_order_monitor',
                    type:'GET',
                    data:{response_order_id:response_order_id,response_customer_id:response_customer_id},
                    success:function(res){


                       console.log(res);

                   var select_order_details = response[0].select_order_details[0];


                   $.each(select_order_details, function(index, el) {
                      //For example
                      console.log(index + el)

                  })



                    }
                })

          }
      })

  });

响应结果: response result

2 个答案:

答案 0 :(得分:0)

首先您需要为结果元素添加idclass,在这种情况下,您应该这样做:

<tbody style=" font-size:14px;">
<tr id="result">

</tr>

然后您可以使用$.each就像foreach一样,

success:function(res){
    $.each(res, function(r){
       $("#result").append("<td>"+res[r].name+"</td><td>"+res[r].family+"</td><td>"+res[r].age+"</td>");
    });
});

这应该对您有用(我不知道您的答复到底是什么,这就是为什么我使用'name,family,age',您应该对从管理员那里得到的答复执行此操作。

答案 1 :(得分:0)

var response=JSON.parse(res); var html=''; 
for(var i=0; i<response.length; i++){
    html+="<tr><td>"+response[i].attribute+"</td>"+<td>"+response[i].attribute2+"</td>+ <td>"+response[i].attribute3+"</td>+<td>"+response[i].attribute4+"</td></tr>"; } $('.table').append(html);