javascript - Jquery Datatables 在 json 表中添加新行

标签 javascript jquery json datatable

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

我正在尝试在 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();
  });
});

最佳答案

就像您在 documentation 中看到的那样您需要更改此行:

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

至:

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

updated fiddle .

关于javascript - Jquery Datatables 在 json 表中添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42486162/

相关文章:

javascript - 制作对象数组时的封装问题

javascript - 行数可变时的动态数据表

json - 无法让 Json 在 ag Grid 中显示只是说正在加载

javascript - Vue 自定义元素与 vee-validate ValidationObserver 组件发生错误

javascript - 默认选项卡导航在 Mozilla 中不起作用

javascript - 将已选中复选框的表行提取到对象数组中

java - GSON 没有正确解析嵌套的 JSON 对象

javascript - 将 flex 元素堆叠到 flex 容器的底部

javascript - 等待 Json 调用在 javascript 中完成

javascript - Promise.map 中的异步方法