2016-12-05 120 views
0

我使用datatable插件并希望通过ajax刷新表。我读过here以使我的ajax返回表中显示,但它不起作用。这里是我的html代码:通过ajax和json重新加载数据表

<button type="button" onclick="rld()">test</button> 
<table id="sample_1"> 
     <thead> 
      <tr> 
      <th> 1 </th> 
      <th> 2 </th> 
      <th> 3 </th> 
      </tr> 
     </thead> 
</table> 

我的Java功能:

我不能得到的是什么问题。有没有警报,但在控制台我得到TypeError:f是undefined 和TypeError:c是undefined 我不知道为什么导致我的json返回是正确的(在[JSONLint] [3]中检查)。 在此先感谢。

+0

http://stackoverflow.com/a/33651126/244811? – sweaver2112

+0

您使用的是哪个版本的数据表? – Kisaragi

+0

table.api()。ajax.reload(null,false); https://datatables.net/reference/api/ajax.reload() –

回答

0

谢谢大家的关注:) 发现我没有调用数据时返回的json和json字段的名称不等于列数据字段名称。所以做这个调整:

function reload_table(){ 
var table = $('#sample_1').DataTable(); 
    table.destroy(); 
    var table = $('#sample_1').DataTable({ 
     "processing": true, 
     "dataType": 'json', 
     ajax: '<?php echo site_url('admin/relaod_table'); ?>', 
     deferRender: true, 
     columns: [ 
      { data: 'user_firstname' }, 
      { data: 'user_lastname' }, 
      { data: 'user_status' }, 
      { data: 'user_created_date' }, 
     ], 
     dom: 'Bfrtip', 
     buttons: [ 
      { 
       text: 'Reload table', 
       action: function() { 
        table.ajax.reload(); 
       } 
      } 
     ] 
    }); 
}