Get Checkbox in Current Row

Get Checkbox in Current Row

Davey JonesDavey Jones Posts: 7Questions: 3Answers: 0

I am iterating through my datatable using

table.rows().every(function (rowIdx, tableLoop, rowLoop) {
});

and I want to see if the checkbox in the row is checked. The checkbox was created using render, but the rest of the table was populated through AJAX. I can use var data = this.data(); to get the rest of the data elements, but I'm having trouble getting that checkbox.

Here is where I am setting up the datatable:

$('#myTable').DataTable({
        ajax: {
            type: 'GET',
            url: '/myController/myMethod',
            data: {
                argument1: obj.Var1,
                argument2: obj.Var2,
                argument3: obj.Var3
            },
            dataSrc: '',
            error: function (xhr, status, error) {
                alert("error occurred");
            }
        },
        columns: [
            {
                targets: 0,
                data: 'Checked',
                searchable: false,
                orderable: false,
                width: '1%',
                className: 'dt-center',
                render: function (data, type, full, meta) {
                    if (data){
                        return '<input type="checkbox" checked>';
                    }
                    else {
                        return '<input type="checkbox">';
                    }
                },
                createdCell: function (td, cellData, rowData, row, col) {
                    $(td).prop("scope", "row");
                }
            },
            {
                targets: 1,
                data: 'someData1'
            },
            {
                targets: 2,
                data: 'someData2'
            },

        ],
        order: []
    });

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited March 2017 Answer ✓

    Here is one option you can try:

     var data = table.rows().nodes();
     $.each(data, function (index, value) {
       console.log($(this).find('input').prop('checked'));
     });
    

    EDIT: Sorry noticed you had a specific function noted. This might work for you:

     table.rows().every(function (rowIdx, tableLoop, rowLoop) {
          var data = this.node();
          console.log($(data).find('input').prop('checked'));
    });
    

    Kevin

  • Davey JonesDavey Jones Posts: 7Questions: 3Answers: 0

    That did the trick. Thanks Kevin. I wasn't aware of the .node() method, and I was originally thinking I had to do something like row().cell(0) or something to that effect. I ended up going with your second method:

    var node = this.node();
    $(node).find('input').prop('checked');
    
This discussion has been closed.