DataTablesSrc icon indicating copy to clipboard operation
DataTablesSrc copied to clipboard

API access from event callbacks undefined

Open bytestream opened this issue 5 years ago • 2 comments

$('#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

bytestream avatar Jun 26 '20 18:06 bytestream

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.

DataTables avatar Jun 29 '20 09:06 DataTables

Just in case you need a fiddle https://jsfiddle.net/bytestream/vanLyjfo/

bytestream avatar Jun 30 '20 12:06 bytestream