如何在数据表中添加多行 jquery [英] How to add multiple rows in datatables jquery

查看:19
本文介绍了如何在数据表中添加多行 jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过 https://datatables.net/reference/api/rows.add%28%29 链接有效,但数据显示表为 [object,object].如何将对象显示为字符串.我使用了 JSON.stringify(obj) 它也不起作用.

I have used https://datatables.net/reference/api/rows.add%28%29 link working but the data showing the table as [object,object]. How to show the object to string. i have used JSON.stringify(obj) its also not working.

HTML

<table id="exampleTable">
 <thead>
  <tr>
   <th>Year</th>
   <th>Month</th>
   <th>Savings</th>
  </tr>
 </thead>
 <tbody>
   <tr>
    <td>2012</td>
    <td>January</td>
    <td>$100</td>
   </tr>
   <tr>
    <td>2012</td>
    <td>February</td>
    <td>$80</td>
   </tr>
 </table>

JS

$('#addRows').click(); 

var table3 = $('#exampleTable').DataTable(); 

$('#addRows').on( 'click', function () { 
    table3.row.add(
       [ { "Year": "Tiger Nixon", "Month": "System Architect", "Savings": "$3,120" },
         {"Year": "Tiger Nixon", "Month": "System Architect", "Savings": "$3,120" }]
    ).draw(); 
});

推荐答案

我在这个 FIDDLE 中创建了两个示例一>.

I created two samples in this FIDDLE.

如果你想在行添加中使用对象,你应该在你的数据表初始化中添加列:

If you want to use objects in rows add you should add columns in your datatable init:

JS

var table3 = $('#exampleTable').DataTable({
    data:[{ "Year": "2012", "Month": "January", "Savings": "$100" },
      { "Year": "2012", "Month": "February", "Savings": "$80" }],
    columns:[{data: 'Year'},
        {data: "Month"},
        {data: "Savings"}]
}); 

但如果您不想这样做,您可以在行添加中使用下一个语法:

but if you don't want to do this you can use next syntax in rows add:

JS

table4.rows.add(
   [[ "Tiger Nixon", "System Architect","$3,120" ],
     ["Tiger Nixon", "System Architect", "$3,120" ]]
).draw(); 

看看 fiddle 内容更丰富.

这篇关于如何在数据表中添加多行 jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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