【问题标题】:Datatables Cannot read property 'fnGetData' of undefined数据表无法读取未定义的属性“fnGetData”
【发布时间】:2015-02-05 10:13:21
【问题描述】:

我在我的项目中使用 jQuery 数据表,现在一切正常。但是我在浏览器控制台中遇到警告错误,我不知道为什么会出现这个错误。但它并没有停止任何功能。

 Uncaught TypeError: Cannot read property 'fnGetData' of undefined

这是在牌桌抽签后出现的错误。

以下是我用于数据表的参数。所以你可以对这个警告错误有所了解。

var initParams = {
        "bDestroy": true,
        "autoWidth": false,
        "bSortClasses": true,
        "iDisplayLength": 15,
        "aLengthMenu": [[10, 15, 30, 50, 100, -1], [10, 15, 30, 50, 100, "All"]],
        "sAjaxSource": defaultReq,
        "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
            oSettings.jqXHR = $.ajax( {
               "dataType": 'json',
               "type": "GET",
               "url": sSource,
               "data": aoData,
               "success": fnCallback,
               "error": function (e) {
                   console.log(e.message);
               }
           });
        },
        "aoColumns": cols,
        "aoColumnDefs": colDef
    };

我正在触发一个 ajax (sAjaxSource) 请求来获取表数据。

任何帮助将不胜感激:)

【问题讨论】:

  • 查看您的代码我找不到任何问题,您可以创建一个小提琴吗?

标签: javascript jquery datatables jquery-datatables


【解决方案1】:

您的初始化代码是正确的。我怀疑您使用的是较新的 DataTables 1.10.x 库(通过存在 autoWidth 选项)。

当您尝试在较新的 1.10.x 版本的 DataTables 的 API 实例上调用旧版本 (1.9.x) 方法 fnGetData() 时,可能会发生错误 Cannot read property 'fnGetData' of undefined,例如:

var data = $('#example').DataTables().fnGetData();

在 1.9.x 和 1.10.x 版本中调用 fnGetData() 的正确方法是

var data = $('#example').dataTable().fnGetData();

更多信息请参见DataTables 1.9 to 1.10 migration reference

【讨论】:

    【解决方案2】:

    使用 rows().nodes() 代替 fnGetData()。

    这样,

    var data = $('#example').DataTables().rows().nodes();
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2017-03-14
      • 2020-06-21
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多