datatable checkbox checked true or false code example

Example 1: datatables boolean column checkbox

var simple_checkbox = function ( data, type, full, meta ) {
    var is_checked = data == true ? "checked" : "";
    return '<input type="checkbox" class="checkbox" ' +
        is_checked + ' />';
}

var setup_datatable = function () {
    $('#data-table').DataTable({
        "columns": [
            { "data": "id", "className": "text-center"},
            { "data": "keywords"},
            { "data": "platform"},
            { "data": "is_active", "render": simple_checkbox},
            { "data": "is_terminated", "render": simple_checkbox}
        ],
        "ajax": "/data"
    }); // DataTable

}

Example 2: datatables boolean column checkbox

// Handle form submission event
$('#frm-example').on('submit', function(e){
   var form = this;

   // Iterate over all checkboxes in the table
   table.$('input[type="checkbox"]').each(function(){
      // If checkbox doesn't exist in DOM
      if(!$.contains(document, this)){
         // If checkbox is checked
         if(this.checked){
            // Create a hidden element
            $(form).append(
               $('<input>')
                  .attr('type', 'hidden')
                  .attr('name', this.name)
                  .val(this.value)
            );
         }
      }
   });
});

Tags:

Misc Example