为了账号安全,请及时绑定邮箱和手机立即绑定

通过ajax将数据附加到表中

通过ajax将数据附加到表中

哔哔one 2022-01-20 18:52:02
我在将数据附加到表体时遇到问题。代码$.ajax({  type:'GET',  url:'{{url('dashboard/customer-groups')}}/'+ordID,  success:function(data){    console.log(data);  //screenshot below  }});截屏上面的 ajax 代码返回的数据分为 2 个类别(组和客户)HTML<table id="data-table" class="table table-striped table-bordered">    <thead>    <tr>        <th>#</th>        <th>Name</th> // from customers array        <th>Company</th> // from customers array        <th>Province</th> // from customers array        <th>City</th> // from customers array        <th>Email</th> // from customers array        <th>Industry</th> // from customers array        <th>Group</th> // from group    </tr>    </thead>    <tbody id="table_customer"></tbody></table>任何的想法?
查看完整描述

2 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

.each()您可以使用on success遍历响应数据并将它们附加到表主体中。


演示:


let  data = {customers: [

  {id: 1, name:'John', group_id: 111, industry_id: 'xyz', company: 'abc', province: 'aaa', city: 'vv', email: 'aa@gmail.com'},

  {id: 2, name:'Kate', group_id: 222, industry_id: 'lmn', company: 'abc2', province: 'bbb', city: 'qq', email: 'zz@gmail.com'}

]};


$(data.customers).each(function(_, i){

  var row = `<tr><td></td><td>${i.name}</td><td>${i.company}</td><td>${i.province}</td><td>${i.city}</td><td>${i.email}</td><td>${i.industry_id}</td><td>${i.group_id}</td></tr>`;

  $('#table_customer').append(row);

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="data-table" class="table table-striped table-bordered">

    <thead>

    <tr>

        <th>#</th>

        <th>Name</th> 

        <th>Company</th>

        <th>Province</th>

        <th>City</th>

        <th>Email</th> 

        <th>Industry</th> 

        <th>Group</th> 

    </tr>

    </thead>

    <tbody id="table_customer"></tbody>

</table>


查看完整回答
反对 回复 2022-01-20
?
慕森王

TA贡献1777条经验 获得超3个赞

    $.ajax({

      type:'GET',

      url:'{{url('dashboard/customer-groups')}}/'+ordID,

      success:function(data){

        const tr = $('tr');

        for (var key in data) {

           var obj = data[key];

           var id = "<td>"+obj.id+"</td>";

var group_id = "<td>"+obj.group_id+"</td>";



     // add more columns for other attributes

           var allTds = id+group_id;

         }

        tr.append(allTds);

       $('#data-table').append(tr);

      }

    });


查看完整回答
反对 回复 2022-01-20
  • 2 回答
  • 0 关注
  • 280 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号