DataTable正在工作,但在JS函数中访问它时会收到错误

问题描述 投票:0回答:2

我实例化了一个数据表,它工作正常。

但是我在JS函数中访问它时收到错误。错误是:

未捕获的TypeError:$(...)。dataTable不是函数

我正在使用的代码是:


$( document ).ready(function() {
    //datatable instantiation
    $('#table-servicos').DataTable({
      "order": [[ 0, "desc" ]]
    });
});

function test() {
  //checkin:1407 Uncaught TypeError: $(...).dataTable is not a function    
  $('#table-servicos').dataTable().order([2, 'desc']).draw();    
}
javascript jquery datatables
2个回答
1
投票

你的代码是正确的,但有一个错字。它的DataTable不是dataTable。更新代码,它应该按预期工作。这是每个开发人员叹息的非常疏忽!

$( document ).ready(function() {
//datatable instantiation
    $('#table-servicos').DataTable({
      "order": [[ 0, "desc" ]]
    });
});

function test() {
  $('#table-servicos').DataTable().order([2, 'desc']).draw();    
}

0
投票

在表初始化之后,您必须在表上使用DataTable,使用大写D.

$('#table-servicos').DataTable().order([2, 'desc']).draw();  
© www.soinside.com 2019 - 2024. All rights reserved.