Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single check box selection with jquery datatable

In jquery datatable, single check box should be selected.

This link is working fine.

But above link is using jquery.dataTables 1.10.16 version. And I am using jquery.dataTables 1.9.4.

Can the same functionality as listed in example given above be possible with jquery.dataTables 1.9.4 instead of jquery.dataTables 1.10.16?

like image 587
user752590 Avatar asked May 11 '18 04:05

user752590


People also ask

How to use check box in DataTable?

A column can be shown with a checkbox that reflects the row's selected status simply through the use of the select-checkbox CSS class for that column ( columns. className ). Row selection can be restricted to that column using the select. selector option.

How check checkbox is checked or not in jquery DataTable?

function uncheckRow(trId) {<br> var table = $('#example'). DataTable(); var row = table. row("#"+trId); var rowData = row. data(); var name = rowData[1]; var age = rowData[2]; // need to check if the check box in the 1st column(rowData[0]) is checked or not, If it is checked, i have to uncheck … }

How do I show only 5 entries in DataTables?

There is a option called pageLength . You can set this for show only 5 entries.

How do I select all checkboxes from all pages in jquery DataTable grid?

nodes(); // Check/uncheck checkboxes for all rows in the table $('input[type="checkbox"]', rows). prop('checked', this. checked); }); // Handle click on checkbox to set state of "Select all" control $('#example tbody'). on('change', 'input[type="checkbox"]', function(){ // If checkbox is not checked if(!


2 Answers

In the same page which you give the link, there are many explanation about to using "single check" oparetion.

At the end of the listed attachment, you can see the referanced .js file is

https://cdn.datatables.net/select/1.2.5/js/dataTables.select.min.js

In your page, you should add this file referance after dataTable.js.

I think, the version of jquery is not important. The important file is "dataTables.select.js"!

Secondly, you must update your dataTable maker codes like the sample below;

$(document).ready(function() {
    $('#example').DataTable( {
        columnDefs: [ {
            orderable: false,
            className: 'select-checkbox',
            targets:   0
        } ],
        select: {
            style:    'os',
            selector: 'td:first-child' // this line is the most importan!
        },
        order: [[ 1, 'asc' ]]
    } );
} );

UPDATES :

Why dont you try to write your own selector function?

for example;

$(document).ready(function() {
    $('#example').DataTable( {
            /// put your options here...
        } );

    $('#example').find("tr").click(function(){ CheckTheRow(this); });
} );

function CheckTheRow(tr){
    if($(tr).find("td:first").hasClass("selected")) return;

    // get the pagination row count
    var activePaginationSelected = $("#example_length").find("select").val();

    // show all rows
    $("#example_length").find("select").val(-1).trigger("change");

    // remove the previous selection mark
    $("#example").find("tr").each(function(i,a){
         $(a).find("td:first").removeClass("selected");
         $(a).find("td:first").html("");
     });

     // mark the picked row
     $(tr).find("td:first").addClass("selected");
     $(tr).find("td:first").html("<i class='fa fa-check'></i>");

     // re turn the pagination to first stuation
     $("#example_length").find("select")
                         .val(activePaginationSelected).trigger("change");

}
like image 92
Serhat MERCAN Avatar answered Oct 10 '22 21:10

Serhat MERCAN


Unfortunately, legacy data table does not support or have that select extension.

Workaround:

  1. Create checkbox element inside 'mRender' callback.
  2. Bind action to the checkbox. (This can be done inside the fnRowCallback or outside as in my example in below fiddle

https://jsfiddle.net/Rohith_KP/dwcatt9n/1/

 $(document).ready(function() {
  var userData = [
    ["1", "Email", "Full Name", "Member"],
    ["2", "Email", "Full Name", "Member"]
  ];

  var table = $('#example').DataTable({
    'data': userData,
    'columnDefs': [{
      'targets': 0,
      'className': 'dt-body-center',
      'mRender': function(data, type, full, meta) {
        return '<input type="checkbox" value="' + $('<div/>').text(data).html() + '">';
      }
    }],
    'order': [1, 'asc']
  });

  $('#example tr').click(function() {
    if ($(this).hasClass('row_selected'))
      $(this).removeClass('row_selected');
    else
      $(this).addClass('row_selected');
  });
});

Also, I suggest you to upgrade your datatable version. Then you can use that select extension.

like image 38
Rohith K P Avatar answered Oct 10 '22 19:10

Rohith K P