mercredi 26 août 2015

Populate Array with "Select All" Checkbox

For each record in my <table> I have a checkbox. When this checkbox is clicked, an ID value is sent to an array for the record that is checked:

function setIds(){
var recordIds = []; //array to store IDs
$(".row-check").change(function(e){
    var checkedRecord = dataTable.getRecord(e.currentTarget.id); //gets ID of record object
    var id = checkedRecord.toJSON().id;
    if(this.checked){
        recordIds.push(id);
    } else { // Return array record ID that was unchecked
        recordIds.splice( $.inArray(id, recordIds), 1 );
    }
    console.log(recordIds); 
});
}

I also have a checkbox in my table header that checks all the records on the table:

function selectAll(){
   $('input[name="all"]').click(function () {
       $('input[name="row-check"]').prop('checked', this.checked);
   });
}

Here's how the table looks (cuz I like to doodle) enter image description here

However the .change() function does not pick up the records selected when I select the "select all" checkbox. I've tried placing the .row-check .change() method within my selectAll function, but no luck. How can I send all my IDs to an array when the "select all" checkbox is checked?




Aucun commentaire:

Enregistrer un commentaire