javascript - DataTables - Using Column Names Instead of Indexes - Stack Overflow

I have a DataTables setup as follows.var pageData = [{"id":"2","slug":&qu

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

jQuery 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信