DataTables change checkboxes over all pages

↘锁芯ラ 提交于 2019-12-31 01:33:28

问题


I'm using DataTables to list which "events" are shown on each page of my web application.

For each page I have a column and each event is a row with checkboxes per page. (To give you an idea of what it looks like: http://i.stack.imgur.com/6QhsJ.png)

When I click on a page it should check all checkboxes, however the checkboxes are only checked on the current page of the DataTable.

Any help is appreciated!

Edit: here is a JSFiddle for my problem (https://jsfiddle.net/2n3dyLhh/2/)

HTML

<table id="eventsTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Checkbox<input type="checkbox" id="checkall"/></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td><input type="checkbox" id="checkbox1"/></td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td><input type="checkbox" id="checkbox2"/></td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td><input type="checkbox" id="checkbox3"/></td>
        </tr>
        <tr>
            <td>Cedric Kelly</td>
            <td><input type="checkbox" id="checkbox4"/></td>
        </tr>
        <tr>
            <td>Airi Satou</td>
            <td><input type="checkbox" id="checkbox5"/></td>
        </tr>
        <tr>
            <td>Brielle Williamson</td>
            <td><input type="checkbox" id="checkbox6"/></td>
        </tr>
        <tr>
            <td>Herrod Chandler</td>
            <td><input type="checkbox" id="checkbox7"/></td>
        </tr>
        <tr>
            <td>Rhona Davidson</td>
            <td><input type="checkbox" id="checkbox8"/></td>
        </tr>
        <tr>
            <td>Colleen Hurst</td>
            <td><input type="checkbox" id="checkbox9"/></td>
        </tr>
        <tr>
            <td>Sonya Frost</td>
            <td><input type="checkbox" id="checkbox10"/></td>
        </tr>
        <tr>
            <td>Jena Gaines</td>
            <td><input type="checkbox" id="checkbox11"/></td>
        </tr>
        <tr>
            <td>Quinn Flynn</td>
            <td><input type="checkbox" id="checkbox12"/></td>
        </tr>
    </tbody>
</table>

JavaScript

$(document).ready(function() {
    $.extend($.fn.dataTable.defaults, {
        "columns": [null, { "orderable": false }]
    });
    $('#eventsTable').DataTable();
});

$("#checkall").on('click', function() {
    if (this.checked) {
        for(var i = 1; i <= 12; i++) {
            var id = "#checkbox" + i;
            $(id).prop('checked', true);
        }
    } else {
        for(var i = 1; i <= 12; i++) {
            var id = "#checkbox" + i;
            $(id).prop('checked', false);
        }
    }
});

回答1:


Your click handler should be changed to:

$("#checkall").on('click', function () {
    $('#eventsTable').DataTable()
        .column(1)
        .nodes()
        .to$()
        .find('input[type=checkbox]')
        .prop('checked', this.checked);
});

See this example for code and demonstration.

Consider using jQuery DataTables Checkboxes for easier handling of checkboxes in a table powered by jQuery DataTables.




回答2:


$("#chbox").click(function () {
    //chbox is main checkbox  
    var rows,checked;  
    var rows = $("#viewlist").dataTable().$('tr', {"filter": "applied"});// viewlist is
    checked = $(this).prop('checked');
    $.each(rows, function () {
        var checkbox = $($(this).find('td').eq(0)).find('input').prop('checked', checked);
    });
});


来源:https://stackoverflow.com/questions/30328556/datatables-change-checkboxes-over-all-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!