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/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467865a4628995.html
评论列表(0条)