DataTables 错误:从第 0 行的数据源请求未知参数“1”

Posted

技术标签:

【中文标题】DataTables 错误:从第 0 行的数据源请求未知参数“1”【英文标题】:DataTables error: Requested unknown parameter '1' from the data source for row 0 【发布时间】:2014-09-13 12:53:58 【问题描述】:

我正在尝试使用 jQuery / DataTables 创建一个表,并使用 JSON 数据作为源。我已经验证了数据很好。不幸的是,我不断收到此错误:“DataTables 警告(表 id='example'):从第 0 行的数据源请求未知参数 '1'”。我不确定我在这里做错了什么:

JSON

dataSet = [

    "Month": "October",
    "Notices Received": "0",
    "Declined Participation": "0",
    "Selected Field Reviews": "0",
    "Selected File Review": "0",
    "Pending": "0",
    "Pending Previous Year": "0",
    "Controversial": "0",
    "GFP Reviews": "0",
    "NAD Appeals": "0",
    "Mediation Cases": "0",
    "Monthly Cost Savings": "$0.00",
    "Monthly Expenditure": "$0.00"
,

    "Month": "November",
    "Notices Received": "0",
    "Declined Participation": "0",
    "Selected Field Reviews": "0",
    "Selected File Review": "0",
    "Pending": "0",
    "Pending Previous Year": "0",
    "Controversial": "0",
    "GFP Reviews": "0",
    "NAD Appeals": "0",
    "Mediation Cases": "0",
    "Monthly Cost Savings": "$0.00",
    "Monthly Expenditure": "$0.00"
,

    "Month": "December",
    "Notices Received": "0",
    "Declined Participation": "0",
    "Selected Field Reviews": "0",
    "Selected File Review": "0",
    "Pending": "0",
    "Pending Previous Year": "0",
    "Controversial": "0",
    "GFP Reviews": "0",
    "NAD Appeals": "0",
    "Mediation Cases": "0",
    "Monthly Cost Savings": "$0.00",
    "Monthly Expenditure": "$0.00"
];

JS:

$('#theJson').text(dataSet); //just for testing

$('#example').dataTable( 
  "aaData": dataSet,
  "aoColumns": [

         "sTitle": "Month" ,
         "sTitle": "Notices Received" ,
         "sTitle": "Declined Participation" ,
         "sTitle": "Selected Field Reviews",
         "sTitle": "Selected File Reviews",
         "sTitle": "Pending",
         "sTitle": "Pending Previous Year",
         "sTitle": "Controversial",
         "sTitle": "GFP Reviews",
         "sTitle": "NAD Appeals",
         "sTitle": "Mediation Cases",
         "sTitle": "Monthly Cost Savings",
         "sTitle": "Monthly Expenditure"
    ]

 );

html

<table  id="example" border="0" cellspacing="0" cellpadding="0"></table>

我得到的只是错误消息和表头。页脚实际上显示:“显示 4,008 个条目中的 1 到 10 个”,这可能表明它正在查看数据。谢谢!

【问题讨论】:

【参考方案1】:

问题是"aaData": dataSet,接受数组数据,但你还没有转换json数据,

检查一下,

var dataSet = [  //Table Data ,  //Table Data  ,  //Table Data  ];//Wrong Type (Still Json Format)

但数据格式除外

var dataSet = [  [//Table Data ], [ //Table Data ] , [ //Table Data ] ];//Right Type (Now  Array Format)

转换json data to array data

var dataSet=[];
    $.each(o,function(i,k)
          dataSet.push( $.map(o[i], function(el)  return el; ));
    );
console.log(dataSet);

这里是他的演示...Click Here Demo

现在试试吧,

【讨论】:

完美!现在正在工作。我假设“参数'1'”与数据格式有关。谢谢!

以上是关于DataTables 错误:从第 0 行的数据源请求未知参数“1”的主要内容,如果未能解决你的问题,请参考以下文章

数据表警告从第 0 行的数据源请求未知参数“1”

数据表为第 0 行请求未知参数“0”

在 Laravel 项目中使用 DB 方法得到 DataTables 错误

如何提取 DataTables 中所选行的第一列?

datatables jquery - 未捕获的类型错误:无法读取未定义的属性“显示”

使用 Datatables 在 Laravel 中处理大量行的最佳方法是啥?