Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle checkbox click event in datatables

I have a boolean field like a flag to state that row is deleted or not in the table. It displayed using checkbox, so if the data has been deleted, the checkbox value is true and vice versa.

Below is the code to display the table:

<table id="tblEvent" class="display" cellspacing="0" style="width: 100%">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(model => model.PIC)</th>
            <th>@Html.DisplayNameFor(model => model.Name)</th>
            <th>@Html.DisplayNameFor(model => model.StartDate)</th>
            <th>@Html.DisplayNameFor(model => model.Status)</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.PIC)</td>
                <td>@Html.DisplayFor(modelItem => item.Name)</td>
                <td>@Html.DisplayFor(modelItem => item.StartDate)</td>
                <td>@Html.EditorFor(modelItem => item.Status)</td>
            </tr>
        }
    </tbody>
</table>

$(document).ready(function () {
    $("#tblEvent").dataTable({
        "order": [[1, "desc"]]
    });
});

In the table I can click the checkbox but I don't know how to handle the click event because I'm using datatables to display the data. How to handle checkbox event using datatable?

like image 723
Willy Avatar asked May 13 '15 09:05

Willy


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.

Which event is triggered when the user clicks in a check box?

The Click event occurs when the user presses and then releases a mouse button over an object.

How can I check all checkboxes in a DataTable?

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').

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 … }


2 Answers

I guess you are using a paginated table, and are facing the problem that your click handler not are working when you change pages?

The solution suggested in comments would work on a 1-page dataTable, but is useless if you change pages or the dataTable otherwise is redrawn :

$('#tblEvent input[type="checkbox"]').click(function() {
    console.log('suggested-in-comment', 'click');
});

...Only works on the first page. You must use a delegated event to be sure that the checkboxes always is bound to the click-handler :

$('#tblEvent').on('click', 'input[type="checkbox"]', function() {
    console.log('best', 'click');
});    

demo with both / proof of concept -> http://jsfiddle.net/o4mhqpr3/

like image 112
davidkonrad Avatar answered Oct 02 '22 20:10

davidkonrad


If you are using datatables with selection capabilities you can use listeners (select and deselect events).

table.on( 'deselect', function ( e, dt, type, indexes ) {});
table.on( 'select', function ( e, dt, type, indexes ) {});
like image 27
Tamás Mazuk Avatar answered Oct 02 '22 20:10

Tamás Mazuk