1

I am trying to get a checkbox column added to the jQuery datable, however am unable to do so.

My data table is initialised with a JSON object data and I want to add a column of checkboxes before the data. The data table shows the data but not check box column. The relevant code is as follows

HTML

<table id="mytable" class="table table-striped table-bordered" cellspacing="0" width="100%">


    <thead>
            <tr>
                <th>Request ID</th>
                <th>Request Date</th>
                <th>Leave Type</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Status</th>
            </tr>
        </thead>
    </table>

JAVASCRIPT CODE

msg = $.parseJSON(msg);
            console.log(msg);
            $('#mytable').DataTable({
                data: msg,
                columns: [
                    { data: 'RequestID' },
                    { data: 'RequestDate' },
                    { data: 'LeaveType' },
                    { data: 'LeaveStart' },
                    { data: 'LeaveEnd' },
                    { data: 'Status' }
                ],
                "columnDefs": [ {
                  "targets": 0,
                  "searchable": false,
                  "data": null,
                  "defaultContent": "<button>Click!</button>"
                }]
            });

PHP CODE TO GET DATA FROM MYSQL DATABASE

$result = $conn->query($sql);
//$result = $result->num_rows;

if($result->num_rows >0){
   $res = array();
   while($row =mysqli_fetch_assoc($result))
    {
       $res[] = $row;
    }
   //$res = array( "data"=>$res);
   $output = json_encode($res);

} else
{
$output = 'fail';
}

echo $output;
die();

I have searched through the forums but all of the results i get are for ajax sourced data and not ones populated like I do.

Regards,

1

1 Answer 1

1

You can try this:
HTML

<table id="mytable" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th><input type="checkbox" name= "check_all" id="check_all" value="1" /></th>
            <th>Request ID</th>
            <th>Request Date</th>
            <th>Leave Type</th>
            <th>Start Date</th>
            <th>End Date</th>
            <th>Status</th>
        </tr>
</thead>

Javascript:

msg = $.parseJSON(msg);
$('#mytable').DataTable({
    data: msg,
    columns: [
        { data: 'RequestID' },
        { data: 'RequestDate' },
        { data: 'LeaveType' },
        { data: 'LeaveStart' },
        { data: 'LeaveEnd' },
        { data: 'Status' }
    ],
    "columnDefs": [ {
         "targets": 0,
          "searchable": false,
          "data": "RequestID",
          "render": function ( data, type, full, meta ) {
                    return '<input type="checkbox" name= "check_id[]" data-id="'+data+'" />';
           }, 
    }]
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks works like a charm. I will now be able to get all the check boxes which are selected with the click event.!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.