Data sources: Convert DOM to Object
Data sources: Convert DOM to Object
I'm using the latest datatables 1.10.6
So I try to load data by Ajax, but data is taken not from json, but from dom (because I have very complicated html inside of cells and it's easier to generate them in views).
I've defined 'ajax' option as function where in 'success' event handler I do the following:
var newRows = $res.find("tr.row_data");
newRows.each(function() {
var _this = $(this);
var newRowObj = {
"DT_RowId": _this.attr("id"),
"DT_RowClass": _this.attr("class")
};
var tds = _this.children();
for (var i = 0; i < tds.length; i++) {
newRowObj[columns[i].data] = $(tds[i]).html();
}
resdata.data.push(newRowObj);
});
callback(resdata);
columns are defined as
$(table + ' > thead > tr.heading > th').each(function () {
columns.push({'data': $(this).data('name')});
});
datatable is created with this parameter like
"dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>f>r><'table-scrollable't><'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>", // datatable layout
"stateSave": true, // save datatable state(pagination, sort, etc) in cookie.
"serverSide": serverSide,
"deferRender": true,
"orderClasses": false,
"ajax": function (data, callback, settings) {
//omitted
},
"columnDefs": [
{
// define columns sorting options(by default all columns are sortable extept the first checkbox column)
'orderable': true
}
],
"lengthMenu": [
[10, 20, 50, 100, 150, -1],
[10, 20, 50, 100, 150, "All"] // change per page values here
],
"pageLength": 50, // default record count per page
"order": [
[1, "asc"]
] // set first column as a default sort by asc
so
in the debugger I see that I try to call callback with the correct data
{ .... data: [{DT_RowClass: .. , DT_RowId: ... actions: ... check: ... etc} ]
but I still getting error Requested unknown parameter '0' for row '0' inside of _fnGetCellData
And also I see that datatable settings contain correct name for each column. i.e first column is
name: "check", orderable: true, sCellType: "td", sClass: "", sContentPadding: ""sDefaultContent: null, sName: "check" and so on
What's the problem?
This question has an accepted answers - jump to answer
Answers
My fault - I'm using Metronic with it's wrapper around datatables, so I'm passing columns definition not to datatables itself, but to Metronic's Datatable wrapper....