Javascript 数据表类型错误:c 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29893207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatables TypeError: c is undefined
提问by Ali Mahmoudi
I try to use jQuery DataTables but I get the error
我尝试使用 jQuery DataTables,但出现错误
TypeError: c is undefined
类型错误:c 未定义
I don't know what is wrong with my code as I can see the JSON correctly retrieve and is in the correct format too but I don't know what is wrong with it that I get the above error.
我不知道我的代码有什么问题,因为我可以看到 JSON 正确检索并且格式也正确,但是我不知道出现上述错误是什么问题。
My JSON :
我的 JSON :
{"Data":[{"LOGIN":10184},{"LOGIN":10214},{"LOGIN":10180},{"LOGIN":10187},{"LOGIN":10179},{"LOGIN":10280},{"LOGIN":201},{"LOGIN":10238},{"LOGIN":10296},{"LOGIN":10312}]}
and my DataTables code:
和我的数据表代码:
$(document).ready(function() {
$('#tablename').dataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"type": "POST",
"url": "https://test.com/api/db/select",
"data": function ( json ) { return JSON.stringify( { "Sql": 12 } );},
"contentType": "application/json; charset=utf-8",
"dataType": "json",
"processData": true,
beforeSend : function(xhr){
var access_token = sessionStorage.getItem('access_token');
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
}
},
"dataSrc": "Data",
"columns": [
{ "data": "LOGIN" }
]
} );
} );
采纳答案by davidkonrad
dataSrcis a special dataTables ajax option, that should be included inside the ajax object :
dataSrc是一个特殊的 dataTables ajax 选项,应该包含在 ajax 对象中:
"ajax": {
"dataSrc": "Data", //<--- place dataSrc here instead
"type": "POST",
...
}
You have placed it outside ajax, and by that dataTables have no idea what source to use (besides blindly trying the ajax response) or where LOGINbelongs.
您已将它放在 ajax 之外,因此 dataTables 不知道使用什么来源(除了盲目尝试 ajax 响应)或LOGIN属于哪里。
回答by Chinthaka Dinadasa
Check whether you have added
检查您是否已添加
<thead></thead>
<tbody></tbody>
I've resolved this problem by adding those.
我已经通过添加这些解决了这个问题。
So basically the structure must be like:
所以基本上结构必须是这样的:
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
回答by Raju Singh
Sometime, This type issue arrives by fixing mismatched / unequal columns with HTML and datatables columns.
有时,这种类型的问题是通过使用 HTML 和数据表列修复不匹配/不相等的列来实现的。
"columns": [
null,
null,
null,
{"orderable": false, "width":"2%"},
],
Above javascript defined 4 columns and HTML having 5 columns
上面的 javascript 定义了 4 列,HTML 定义了 5 列
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
</tr>
Hence you will have to fix / equal both side HTMl and Datatable configuration.
因此,您将必须修复/相等双方的 HTMl 和 Datatable 配置。
"columns": [
null,
null,
null,
null, //Added New
{"orderable": false, "width":"2%"},
],
回答by Qasim Ali
in my case, i had to remove colspan attribute from a th inside thead and get rid of the error ;(
就我而言,我不得不从 thead 内部的 th 中删除 colspan 属性并消除错误;(
回答by hobbydev
In my case, missing aaData in sever side return result.
就我而言,在服务器端返回结果中缺少 aaData。
//Javascript
$('#table').DataTable({
sAjaxSource: '/load',
aoColumns: [
...
],
});
//server side(in Rails)
render json: {'aaData'=>data}
回答by MrMojoRisin
In my case, I got the same error because I used the ajax.dataSrc( data )function to manipulate the data. But after that i forgot to return the data.
就我而言,我遇到了同样的错误,因为我使用了ajax.dataSrc( data )函数来操作数据。但在那之后我忘了返回数据。
"dataSrc": function ( json ) {
for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
//somethings...
}
return json.data;// I forgot this line, then i got the error "TypeError: c is undefined"
}
After a few minutes, I checked the documentation of the ajax.dataSrc function and I noticed that I did not have the return:
几分钟后,我查看了 ajax.dataSrc 函数的文档,发现没有返回:
Returns: array. Array of data that DataTables is to use to draw the table
返回:数组。DataTables 用于绘制表格的数据数组
I hope you do not have the same distraction...
我希望你没有同样的分心......
回答by Tomanek1
In my case, my front-end: ajax: { dataSrc: "data" }was OK, but on server-side I returned object where first letter was Upper return Json(new { Data = model });
就我而言,我的前端:ajax: { dataSrc: "data" }还可以,但在服务器端我返回了第一个字母为 Upper 的对象return Json(new { Data = model });
回答by EvilDr
Another day, another solution - this time caused by a style sheet!
改天,另一种解决方案 - 这次是由样式表引起的!
After spending hours reducing a gigantic web page to the raw charts code, I found that this error shows (for pie charts) when CSS rules for fonts in the stylesheet contain the calcfunction.
在花费数小时将一个巨大的网页简化为原始图表代码后,我发现当样式表中字体的 CSS 规则包含该calc函数时,会显示此错误(对于饼图)。
In our style sheet, this line of code:
在我们的样式表中,这行代码:
html {
font-size: calc(12px + 5%);
}
...broke the chart. We needed this because our webfont wasn't resizing smoothly and needed a size slightly larger than 12px but smaller than 13px, and this trick forced a better resize.
...打破了图表。我们需要这个是因为我们的 webfont 不能平滑地调整大小,需要一个略大于 12px 但小于 13px 的大小,而这个技巧迫使更好地调整大小。
Overwriting the style rule on the chart widget directly solved the issue:
直接覆盖图表小部件上的样式规则解决了问题:
CSS
CSS
html {
font-size: calc(12px + 5%);
}
.widget {
font-size: 12px /* Replace the above rule */;
}
JS
JS
var GoogleChart1 = new google.visualization.PieChart(document.getElementById('Chart1'));
HTML
HTML
<div id="Chart1" class="widget"></div>
回答by Navin
In my case thand td, count is not equal due do colspan, removed colspan and it worked.
在我的情况下th和td,由于 do colspan , count 不相等,删除了 colspan 并且它起作用了。

