API access from event callbacks undefined
$('#table').on('draw.dt', function (settings, json) {
var api = new $.fn.dataTable.Api(settings),
rows = api.rows({page: 'current'}).nodes(),
last = null;
api.column(1, {page: 'current'}).data().each(function (group, i) {
if (last !== group) {
$(rows).eq(i).before('<tr class="group"><td colspan="6"><strong>Group</strong>: ' + group + '</td></tr>');
last = group;
}
});
});
$('#table').dataTable({ /* ... */ });
In the above code upon initialisation the draw event fires but (new $.fn.dataTable.Api(settings)).column(1, {page:'current'}).data() returns undefined while $(this).DataTable().column(1, {page:'current'}).data() returns expected data.
Is this expected?
Presumably something has not properly initialised yet in the settings
Nope I wouldn't have expected that. As you say, it sounds like the settings object isn't fully initialised or perhaps it isn't in the array of objects (although I thought that was one of the first things to happen).
Thanks for posting this.
Just in case you need a fiddle https://jsfiddle.net/bytestream/vanLyjfo/