Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datatables multiple checkbox

I found the next code for generating multiple checkboxes inside datatable and its works great:

https://jsfiddle.net/snqw56dw/

var table = $('#example').DataTable({
      'ajax': 'https://api.myjson.com/bins/1us28',
      'columnDefs': [
         {
            'targets': 0,
            'checkboxes': {
               'selectRow': true
            }
         }
      ],
      'select': {
         'style': 'multi'
      },
      'order': [[1, 'asc']]
});

But when I try to change the code to work with static table data (changed that data will not come from Ajax) its stopped working..

Here is my code:

https://jsfiddle.net/snqw56dw/3158/

var table = $('#example').DataTable({     
      'columnDefs': [
         {
            'targets': 0,
            'checkboxes': {
               'selectRow': true
            }
         }
      ],
      'select': {
         'style': 'multi'
      },
      'order': [[1, 'asc']]
});

I would love to get your help and understand what I'm doing wrong there..

Thanks

like image 593
user2678018 Avatar asked Jun 19 '18 10:06

user2678018


People also ask

How to add checkboxes in DataTables using jQuery?

In your jQuery DataTables initialization code add checkboxes option for the column where you want the checkboxes to appear using either columns or columnDefs options. If you’re using Select extension, you may want to enable multiple row selection using select.style option.

How to center checkbox in the cell in DataTables?

To center checkbox in the cell, internal DataTables CSS class dt-body-center is used. Option render is used to prepare the checkbox control for being displayed in each cell of the first column. We use a trick with $ ('<div/>').text (data).html () to encode HTML entities that could be present in the data.

What is the use of checkbox in jQuery?

Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table. Example below shows a table powered by jQuery DataTables with Select and Checkbox extensions using client-side processing mode where data is received from the server via Ajax request.

Is it possible to select multiple values in a DataTable?

As with the other DataTables examples, this is advantageous as an input since we can make use of the features offered by DataTables. Note that the multiple option must be set to true for the datatable to allow multiple selection.


2 Answers

You need to have unique data in the column containing checkboxes - 1, 2, 3, etc.

See updated example for code and demonstration.

like image 135
Gyrocode.com Avatar answered Oct 17 '22 01:10

Gyrocode.com


As per DataTable Document:

DataTables will automatically add it for you (note that this will work for Ajax and Javascript loaded data as well as for server-side processing).

You can read full document here: https://www.ksia.or.kr/plugin/DataTables-1.10.15/examples/server_side/ids.html

For static record, you can do it this way: https://jsfiddle.net/cwvr7kba/1/

$(document).ready(function() {
    var table = $('#example').DataTable({
        'columnDefs': [{
            'targets': 0,
            'checkboxes': {
                'selectRow': true
            }
        }],
        'select': {
            'style': 'multi'
        },
        'fnCreatedRow': function(nRow, aData, iDataIndex) {
            $(nRow).attr('data-id', aData.DT_RowId); // or whatever you choose to set as the id
            $(nRow).attr('id', 'id_' + aData.DT_RowId); // or whatever you choose to set as the id
        },
        'order': [
            [1, 'asc']
        ]
    });
    // Handle form submission event 
    $('#frm-example').on('submit', function(e) {
        var form = this;


        var rows = $(table.rows({
            selected: true
        }).$('input[type="checkbox"]').map(function() {
            return $(this).prop("checked") ? $(this).closest('tr').attr('data-id') : null;
        }));
        //console.log(table.column(0).checkboxes.selected())
        // Iterate over all selected checkboxes
        rows_selected = [];
        $.each(rows, function(index, rowId) {
            console.log(rowId)
            // Create a hidden element 
            rows_selected.push(rowId);
            $(form).append(
                $('<input>')
                .attr('type', 'hidden')
                .attr('name', 'id[]')
                .val(rowId)
            );
        });

        // FOR DEMONSTRATION ONLY
        // The code below is not needed in production

        // Output form data to a console     
        $('#example-console-rows').text(rows_selected.join(","));

        // Output form data to a console     
        $('#example-console-form').text($(form).serialize());

        // Remove added elements
        $('input[name="id\[\]"]', form).remove();

        // Prevent actual form submission
        e.preventDefault();
    });
});
like image 3
Bhumi Shah Avatar answered Oct 17 '22 01:10

Bhumi Shah