我已经使用
https://datatables.net/reference/api/rows.add%28%29链接工作,但数据显示为[object,object].如何显示对象到字符串.我使用JSON.stringify(obj)它也不工作.
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",120" }] ).draw(); });
解决方法
我在这个
FIDDLE中创造了两个样本.
如果要使用行中的对象添加,则应在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"}] });
但是如果您不想这样做,可以使用行中的下一个语法添加:
JS
table4.rows.add( [[ "Tiger Nixon","System Architect","$3,120" ],["Tiger Nixon",120" ]] ).draw();
看看fiddle它更多信息.