为什么在DataTable()上调用ajax.reload()会返回undefined?

时间:2017-06-20 01:12:01

标签: jquery ajax datatables

到目前为止我已经做到了这一点。完全装载后,桌子显示完美。但是当我添加t.ajax.reload()时,它会显示TypeError: t.ajax is undefined

$.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings)
{
  return {
    "iStart": oSettings._iDisplayStart,
    "iEnd": oSettings.fnDisplayEnd(),
    "iLength": oSettings._iDisplayLength,
    "iTotal": oSettings.fnRecordsTotal(),
    "iFilteredTotal": oSettings.fnRecordsDisplay(),
    "iPage": Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
    "iTotalPages": Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
  };
};

var t = $("#mytable").dataTable({
  initComplete: function() {
    var api = this.api();
    $('#mytable_filter input')
    .off('.DT')
    .on('keyup.DT', function(e) {
      if (e.keyCode == 13) {
        api.search(this.value).draw();
      }
    });
  },
  oLanguage: {
    sProcessing: "loading..."
  },
  processing: true,
  serverSide: true,
  ajax: {"url": "data/json", "type": "POST"},
  columns: [
    {
      "data": "id",
      "orderable": false
    },
        {"data": "nama"},
        {"data": "stok"},
        {"data": "satuan"},
    {
      "data" : "action",
      "orderable": false,
      "className" : "text-center"
    }
  ],
  order: [[0, 'desc']],
  rowCallback: function(row, data, iDisplayIndex) {
    var info = this.fnPagingInfo();
    var page = info.iPage;
    var length = info.iLength;
    var index = page * length + (iDisplayIndex + 1);
    $('td:eq(0)', row).html(index);
  }
});

setInterval( function () {
  t.ajax.reload();
}, 3000 );

任何建议都会被贬低。提前谢谢!

1 个答案:

答案 0 :(得分:2)

来自DataTables documentation

  

请注意$( selector ).DataTable()$( selector ).dataTable()之间的区别。前者返回DataTables API实例,后者返回jQuery对象。将一个api()方法添加到jQuery对象中,以便您可以轻松访问API,但jQuery对象可用于操作表节点,就像使用任何其他jQuery实例一样(例如使用addClass()等。 )。

因此:

var t = $("#mytable").DataTable({...}); // capital D