pedritin
pedritin

Reputation: 47

jquery datatables fnGetNodes doesn't work properly

I'm using jQuery DataTables and I have a datatable with a checkbox on the first column by each row, it means that it's like a checkboxColumn. Then, I want to get all checked rows, but the "fnGetNodes" function doesn't return all rows, it just returns the visible rows. The datatable reflects user rolls (Company access, Area access, Department Access, etc.), when I open a dialog to modify one of them (Ex: Area Access) and pick I.T., I close the dialog and go back to datatable in question (user rolls table). When I try to get all checked roll (in case I've deleted another one), fnGetNodes returns just visible rows, not all rows. Here's code lines:

data = [];    
$($('table', "div#example").dataTable({ bRetrieve: true }).fnGetNodes()).find(":checkbox:checked").each(function (i, o) {
      data.push($(o).val());
});

Any suggestion will be appreciated.

Upvotes: 0

Views: 3355

Answers (1)

dcodesmith
dcodesmith

Reputation: 9614

Ok this is what I have and I think is a lil neat.

where .cb is the class assigned to all checkboxes.

$('body').on('click', 'table tr .cb', function(){
    var value = $(this).val(),
        isChecked = $(this).is(':checked'),
        index = arr.indexOf(value);

    if (isChecked && index === -1) { // add to array if it doesn't exist in there already
        arr.push(value);
    } else if (!isChecked && index !== -1){ // if value exists in array and checkbox in unchecked, remove it from the array
        arr.splice(index, 1);
    }
});

DEMO

Upvotes: 1

Related Questions