How to loop through datatables and get all the checkboxes checked value?

How to loop through datatables and get all the checkboxes checked value?

junlijunli Posts: 2Questions: 1Answers: 0

I managed to populate the bool data in datatable with checkbox like below:

function loadDataTable(teamMembers) {

var memberArray = [];

for (var i = 0; i < teamMembers.length; i++) {
    memberArray.push([
        teamMembers[i].LastName,
        teamMembers[i].FirstName,
        teamMembers[i].IsUser,
        teamMembers[i].IsManager,
        teamMembers[i].UserId

    ]);
}


$('#dataTableTeamMembers').dataTable().fnDestroy();
$('#dataTableTeamMembers')
    .DataTable({
        aoColumnDefs: [
            {
                'aTargets': [4]
            },
             {
                 "aTargets": [2],
                 "mRender": function (data, type, full) {
                     if (data) {
                         return '<input type=\"checkbox\" checked value="' + data + '"  >';
                     } else {
                         return '<input type=\"checkbox\"  value="' + data + '" >';
                     }
                 }
             },
             {
                 "aTargets": [3],
                 "mRender": function (data, type, full) {
                     if (data) {
                         return '<input type=\"checkbox\" checked value="' + data + '" >';
                     } else {
                         return '<input type=\"checkbox\"  value="' + data + '" >';
                     }
                 }
             },
            {
                "targets": [4],
                "visible": false
            }
        ],
        data: memberArray,
        bLengthChange: false,
        iDisplayLength: -1,
        stateSave: true,
        bFilter: false,
        paging: false,
        order: [[0, 'asc']]

    });

$('#dataTableTeamMembers').visible = true;

}

what is the best way to get all data with edited checkbox in the whole datatable? Basically I need to pass the edited data back to database again...

Thanks much for your help

This question has accepted answers - jump to:

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    it depends. Are you recording ones that have changed or the ones that are checked?
    In columnDefs your two targets (2 and 3) are identical so you could use targets:[2,3] instead of duplicating code.

    I would probably attach an event change handler that would update the value in the data object associated with the checkbox then set a flag in the data object for changed data.

    then you can grab all of the changed rows and send them back to be saved.

  • allanallan Posts: 63,889Questions: 1Answers: 10,530 Site admin
    Answer ✓

    I completely agree. If you want to send only the changed data, then you'll need an event listener on the checkboxes that will basically add that row to a queue (and object or an array) which you can then send to the server.

    Regards,
    Allan

  • junlijunli Posts: 2Questions: 1Answers: 0

    Thanks so much for your suggestions, allan and bindrid.

This discussion has been closed.