简体   繁体   English

DataTables更改所有页面上的复选框

[英]DataTables change checkboxes over all pages

I'm using DataTables to list which "events" are shown on each page of my web application. 我正在使用DataTables列出我的Web应用程序的每个页面上显示的“事件”。

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 ) (为了让您了解它的样子: 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. 当我点击一个页面时,它应该检查所有复选框,但是只在DataTable的当前页面上选中复选框。

Any help is appreciated! 任何帮助表示赞赏!

Edit: here is a JSFiddle for my problem ( https://jsfiddle.net/2n3dyLhh/2/ ) 编辑:这是我的问题的JSFiddle( https://jsfiddle.net/2n3dyLhh/2/

HTML 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 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);
        }
    }
});

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. 考虑使用jQuery DataTables复选框,以便更轻松地处理由jQuery DataTables提供支持的表中的复选框。

$("#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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM