DataTables - Using Column Names Instead of Indexes

2019-06-25 12:16发布

问题:

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?

回答1:

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.net/BYcsk/

EDIT: You don't need to convert. You can achieve this by specifying mData property for columns. The error is coming 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.net/BYcsk/3/