检查DataTable中的所有复选框,包括隐藏的行

Far*_*had 6 datatable jquery webforms

我正在尝试创建一个检查DataTable中所有复选框的函数,包括隐藏的行.这是"复选框"列的html代码:

<div class="usersTable" id="userTable">
    <table cellpadding="0" cellspacing="0" id="customersList" >
        <thead>
            <tr>
                <th><input type="checkbox" name="selectall" id="selectall" class="selectall"/></th>
                <th width="200">val1</th>
                <th width="80px">val2</th>
                <th width="70px">val3</th>
                <th width="450">val4</th>
                <th width="60px">val5</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

提交按钮:

<input type='button' value='select all' id='selectallboxes' name='selectallboxes' />
Run Code Online (Sandbox Code Playgroud)

并且JQuery代码不起作用:

$(function () {         
    otable = $('#customersList').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "aLengthMenu" : [ [10,20,50,100,1000], [10,20,50,100,1000] ],
        "iDisplayLength": 100,
        "bProcessing": true,
        "bServerSide": true,
        "aaSorting":[],         
        "iDisplayStart": 0,
        "sAjaxSource": "filename",
        ....

$("#selectallboxes").click ( function () {
        alert(dt.fnGetNodes().length + ' is total number')
        var selected = new Array();
        $('input', dt.fnGetNodes()).each( function() {
                $(this).attr('checked','checked');
                selected.push($(this).val());                       
        } );
         // convert to a string
        var mystring = selected.length;
        alert(mystring);
})
Run Code Online (Sandbox Code Playgroud)

dar*_*ags 10

尝试:

$("#selectallboxes").click(function () {
    var selected = new Array();
    $(otable.fnGetNodes()).find(':checkbox').each(function () {
        $this = $(this);
        $this.attr('checked', 'checked');
        selected.push($this.val());
    });
    // convert to a string
    var mystring = selected.join();
    alert(mystring);
});
Run Code Online (Sandbox Code Playgroud)

.length给你数组的长度.我曾经join()把数组加入一个字符串.DataTable .fnGetNodes()为您提供表中的所有行,包括隐藏的行.

  • 从DataTables v1.10开始,`$(otable.fnGetNodes())`现在应该是:`$(otable.rows().nodes())` (4认同)
  • +1这段代码$(otable.fnGetNodes()).find(':checkbox').每个,你节省我的一天 (3认同)

Dav*_*ker 0

好的,这应该就是您想要的,这将找到所有当前页面<tr>并使用 dataTables _ AP​​I 循环遍历它们。您可以更改过滤器以满足您的需要,以根据需要选择不同的行,这全部记录在数据表文档中。

$("#selectallboxes").click ( function () 
{
    var selected = new Array();

    // Use the _ function instead to filter the rows to the visible
    var data = oTable._('tr', {"filter":"applied"});

    $.each(data, function(key, index)
    {
        var input = $(this).find('input[type="checkbox"]');

        input.attr('checked', 'checked');

        selected.push(input.val());
    });

    // convert to a string
    var mystring = selected.length;

    alert(mystring);
});
Run Code Online (Sandbox Code Playgroud)