繁体   English   中英

jQuery Datatables在JSON表中添加新行

[英]Jquery Datatables add new row in json table

我正在使用: https ://datatables.net的jquery.dataTables.js

我正在尝试在json表中添加新行。

我无法解决这个问题,为什么不呢?

jsfiddle: http : //jsfiddle.net/f7debwj2/17/

HTML:

<table id="example" class="display" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th>order</th>
      <th>name</th>
      <th>country</th>
    </tr>
  </thead>
</table>

<button id="addRow">Add New Row</button>
<table id="newRow">
  <tbody>
    <tr>
      <td>Line 2
        <input type="hidden" value="2" /> </td>
      <td>DVap
        <input type="hidden" value="DVap" /> </td>
      <td>
        <input type="text" value="22" /> </td>
    </tr>
  </tbody>
</table>

jQuery的:

$(document).ready(function() {
  var dt = $('#example').dataTable();
  dt.fnDestroy();
});

$(document).ready(function() {
  var url = 'http://www.json-generator.com/api/json/get/clmDuyndua?indent=2';
  var table = $('#example').DataTable({
    ajax: url,
    rowReorder: {
      dataSrc: 'order',
    },
    columns: [{
      data: 'order'
    }, {
      data: 'name'
    }, {
      data: 'place'
    }]
  });
  // add row
  $('#addRow').click(function() {
    //t.row.add( [1,2,3] ).draw();
    var rowHtml = $("#newRow").find("tr")[0].outerHTML
    console.log(rowHtml);
    dt.row.add($(rowHtml)).draw();
  });
});

就像您在文档中看到的一样,您需要更改此行:

dt.row.add($(rowHtml)).draw();

至:

table.row.add($(rowHtml)).draw();

更新的小提琴

您的实际错误是未捕获的ReferenceError:未定义dt

只需在按钮单击事件中更改table.row.add($(rowHtml))。draw()而不是dt.row.add($(rowHtml))。draw()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM