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

查看:102
本文介绍了如何在数据表中添加多行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

如果你想使用行中的对象添加你应该在datatable init中添加列:

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"}]
}); 

但如果不想这样做,可以使用行中的下一个语法add:

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(); 

查看小提琴更多信息。

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

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