获取数据表警告:非表节点初始化(TBODY)错误

时间:2019-06-19 15:53:02

标签: javascript php jquery

我仍然无法在1小时内发现错误。 我收到DataTables警告:

  

非表节点初始化(TBODY)。有关此错误的更多信息,请参见http://datatables.net/tn/2

我不知道为什么。我已经一遍又一遍地检查了我的代码以及后颈疼痛。

enter image description here

<table class="table table-bordered table-sm table-hover datatable dt-responsive nowrap">
  <thead>
    <tr>
      <th>Category</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody id="category_data"></tbody>
</table>
show_categories(); //call function show all category

$('#category_data').dataTable();

//function show all category
function show_categories() {
  $.ajax({
    type: 'ajax',
    url: base_url + 'Admin/Categories/fetch_categories',
    async: true,
    dataType: 'json',
    success: function(data) {
      var html = '';
      var i;
      for (i = 0; i < data.length; i++) {
        html += '<tr>' +
          '<td>' + data[i].category + '</td>' +
          '<td style="text-align:right;">' +
          '<button type="button" class="btn btn-xs btn-info" data-category_id="' + data[i].category + '"><em class="fas fa-edit"></em></button> ' +
          '<button type="button" class="btn btn-xs btn-danger" data-category_id="' + data[i].category + '"><em class="fas fa-trash"></em></button>' +
          '</td>' +
          '</tr>';
      }
      $('#category_data').html(html);
    }
  });
}

2 个答案:

答案 0 :(得分:0)

function show_categories(){
        $.ajax({
            type  : 'ajax',
            url: base_url + 'Admin/Categories/fetch_categories',
            async : true,
            dataType : 'json',
            success : function(data){
                var html = '';
                var i;
                for (i = 0; i < data.length; i++) {
                    html += '<tr>' +
                        '<td>' + data[i].category + '</td>' +
                        '<td style="width:80px;">' +
                        '<button type="button" class="btn btn-xs btn-info" data-category_id="' + data[i].category +'"><em class="fas fa-edit"></em></button> '+
                            '<button type="button" class="btn btn-xs btn-danger" data-category_id="' + data[i].category +'"><em class="fas fa-trash"></em></button>'+
                        '</td>' +
                        '</tr>';
                }
                $('#category_showData').html(html);
            }
        });
    }
<table id="category_dataTables" class="table table-bordered table-sm table-hover datatable dt-responsive nowrap">
                    <thead>
                        <tr>
                            <th>Category</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody id="category_showData">
                        
                    </tbody>
                </table>

答案 1 :(得分:0)

我的代码中出现此错误;当已初始化所选节点的DataTable实例时,通过将选项传递给DataTables构造函数对象来触发此操作。 您应该使用like:

$('#example').dataTable( {
paging: false} );

您的代码中只有1次。