admin管理员组文章数量:1430474
I have a DataTables setup as follows.
var pageData = [
{
"id":"2",
"slug":"about\/history",
"title":"History",
"last_updated":"2013-04-21 09:50:41"
},
{
"id":"3",
"slug":"about",
"title":"About",
"last_updated":"2013-04-21 10:42:22"
}
];
$(function () {
$("#pageList").dataTable({
"aaData" : pageData,
"aoColumns" : [
{
"sTitle" : "slug"
},
{
"sTitle" : "title"
},
{
"sTitle" : "last_updated"
},
{
"sTitle" : "id"
}
]
});
});
Now, when I run this, I get the following error alert
DataTables warning (table id = 'pageList'):
Requested unknown parameter '0' from the data source for row 0
And I assume it is because datatables using indexes instead of column names to access data from pageData
. I thought sTitle
will do the work, but it doesn't. Now, I can't find an appropriate option to specify column names to datatable other than sName
which is used only when sending data to server.
I feel that the solution will be a simple one which I overlooked. Well, what am I missing here?
I have a DataTables setup as follows.
var pageData = [
{
"id":"2",
"slug":"about\/history",
"title":"History",
"last_updated":"2013-04-21 09:50:41"
},
{
"id":"3",
"slug":"about",
"title":"About",
"last_updated":"2013-04-21 10:42:22"
}
];
$(function () {
$("#pageList").dataTable({
"aaData" : pageData,
"aoColumns" : [
{
"sTitle" : "slug"
},
{
"sTitle" : "title"
},
{
"sTitle" : "last_updated"
},
{
"sTitle" : "id"
}
]
});
});
Now, when I run this, I get the following error alert
DataTables warning (table id = 'pageList'):
Requested unknown parameter '0' from the data source for row 0
And I assume it is because datatables using indexes instead of column names to access data from pageData
. I thought sTitle
will do the work, but it doesn't. Now, I can't find an appropriate option to specify column names to datatable other than sName
which is used only when sending data to server.
I feel that the solution will be a simple one which I overlooked. Well, what am I missing here?
Share Improve this question asked Apr 21, 2013 at 14:00 JomoosJomoos 13.1k10 gold badges56 silver badges92 bronze badges1 Answer
Reset to default 3jQuery datatable accepts data as array of arrays. So you have to convert your data from array of objects to array of arrays.
var pageData = [{
"id": "2",
"slug": "about\/history",
"title": "History",
"last_updated": "2013-04-21 09:50:41"
},
{
"id": "3",
"slug": "about",
"title": "About",
"last_updated": "2013-04-21 10:42:22"
}];
var aaPageData = [];
for (var i = 0; i < pageData.length; i++) {
var item = pageData[i];
aaPageData[i] = [item.slug, item.title, item.last_updated, item.id];
}
$(document).ready(function () {
$("#table").dataTable({
"aaData": aaPageData,
"aoColumns": [{
"sTitle": "slug",
}, {
"sTitle": "title"
}, {
"sTitle": "last_updated"
}, {
"sTitle": "id"
}]
});
});
demo : http://jsfiddle/BYcsk/
EDIT: You don't need to convert. You can achieve this by specifying mData
property for columns. The error is ing as you haven't given it.
var pageData = [{
"id": "2",
"slug": "about\/history",
"title": "History",
"last_updated": "2013-04-21 09:50:41"
},
{
"id": "3",
"slug": "about",
"title": "About",
"last_updated": "2013-04-21 10:42:22"
}];
$(document).ready(function () {
$("#table").dataTable({
"aaData": pageData,
"aoColumns": [{
"sTitle": "slug",
"mData": "slug"
}, {
"sTitle": "title",
"mData": "title"
}, {
"sTitle": "last_updated",
"mData": "last_updated"
}, {
"sTitle": "id",
"mData": "id"
}]
});
});
demo : http://jsfiddle/BYcsk/3/
本文标签: javascriptDataTablesUsing Column Names Instead of IndexesStack Overflow
版权声明:本文标题:javascript - DataTables - Using Column Names Instead of Indexes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467865a2659611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论