How To Get Reference to Datatables Using Jquery This
How To Get Reference to Datatables Using Jquery This

I am getting the error: Uncaught TypeError: this.rows is not a function
. I have a similar action for more than one datatable and am trying to create just one reference to handle interactions with them. For example:
$('.dataTable').DataTable()
// Actions to take when row is selected/deselected
.on( 'select', function ( e, dt, type, indexes ) {
var selectedRowCount = this.rows('.selected').count();
if (selectedRowCount > 0) {
$(this).parents('.card').find('.selectedRowCount').html('( ' + selectedRowCount + ' tracks selected )');
} else {
$(this).parents('.card').find('.selectedRowCount').html('');
}
})
.on( 'deselect', function ( e, dt, type, indexes ) {
var selectedRowCount = this.rows('.selected').count();
if (selectedRowCount > 0) {
$(this).parents('.card').find('.selectedRowCount').html('( ' + selectedRowCount + ' tracks selected )');
} else {
$(this).parents('.card').find('.selectedRowCount').html('');
}
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Ok, I think I solved it by writing the reference to it like this:
var selectedRowCount = $(this).DataTable().rows('.selected').count();
So, I assume that is the correct way to do it.
That's one way, probably the best is to use
dt
, the second parameter - see here,Colin
Thanks for the reply. I made that change. Is there a better way to also make the reference when adding this code to the above:
You could use a global variable, perhaps, something like:
though yours would be better if there were multiple tables on the page,
Colin
I have used and do use global variable such as you made mention of, but in this case, I do have multiple tables on the page, so unless I discover a better way, I guess I will stay with
$(this).DataTable()
.