【问题标题】:Datatables param 0 row 0 column 0 error数据表参数 0 行 0 列 0 错误
【发布时间】:2016-01-26 21:04:02
【问题描述】:

我的问题是参数 0 行 0 列 0 datables.net/tn/4 的数据错误。我正在使用 ajax 来检索我的数据并填充表格。

这里是ajax

        $.ajax({
        url: 'DAL/WebService1.asmx/FabGuide',
        method: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            $("#datatable").dataTable({
                data: data,
                columns: [
                    { 'data:': 'accountCode' },
                    { 'data:': 'accountValue' },
                    { 'data:': 'description' },
                    { 'data:': 'manufacturer' },
                ]
            });
        }
    });

这是桌子

                        <table id="datatable">
                            <thead>
                                <tr>
                                    <th>accountCode</th>
                                    <th>accountValue</th>
                                    <th>description</th>
                                    <th>manufacturer</th>
                                </tr>
                            </thead>
                        </table>

这是我的数组的样子

也许我只是看了很久 谢谢

【问题讨论】:

  • { 'data:': 'manufacturer' },//&lt;-- remove comma 后面多了一个逗号。这可能是问题吗?
  • 不,我也这么认为

标签: jquery datatables datatables-1.10


【解决方案1】:

你可以试试这个方法:

$.ajax({
    url: 'DAL/WebService1.asmx/FabGuide',
    method: 'post',
    dataType: 'json',
    success: function (data) {
        var arrData = [];
        data.forEach(function(item){
            var aux = [];
          aux.push(item.accountCode);
          aux.push(item.accountValue);
          aux.push(item.description);
          aux.push(item.manufacturer);
          arrData.push(aux);
        });
        $("#datatable").dataTable({
            data: arrData,
            columns: [
                { title: 'accountCode' },
                { title: 'accountValue' },
                { title: 'description' },
                { title: 'manufacturer' },
            ]
        });
    }
});

data 属性接收一个数组元素数组;查看文档:https://www.datatables.net/examples/data_sources/js_array.html

【讨论】:

    【解决方案2】:

    您的初始化代码中有错字 - 没有选项 data:,应该改为 data

    正确代码如下:

    // ... skipped ...
    
    $("#datatable").dataTable({
       data: data,
       columns: [
          { 'data': 'accountCode' },
          { 'data': 'accountValue' },
          { 'data': 'description' },
          { 'data': 'manufacturer' }
       ]
    });
    
    // ... skipped ...
    

    【讨论】:

    • ???我猜你的意思是dataSrc : '',那将是正确的答案。公认的答案,将所有数据推送到另一个数组只是 llame :)
    • @davidkonrad,不是真的,dataSrc: '' 不需要,因为 OP 使用 JavaScript 对象作为数据源。
    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2014-09-16
    • 2017-07-19
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多