数据表1.10“全部检查”通过jquery [英] Datatables 1.10 "Check all" via jquery

查看:116
本文介绍了数据表1.10“全部检查”通过jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这似乎是原始的,但是我一直在努力实现一整天,也许是因为我不能完全理解如何使用API​​,我使用DataTables 1.10.0,我有一个具有分页功能的表格,每一行都有一个复选框,我需要一个检查所有按钮,这将检查所有页面中的所有复选框,问题是它只检查当前页面中的复选框,并留下其他页面没有选中,这应该是容易的,但我无法想像出来!我发现使用fnGetNodes,似乎已被弃用,并且不被版本1.10使用。



编辑:这是我的标记

 < table class =table table-striped table-bordered table-hoverid =numbers_table> 
< thead>
< tr>
< th< input type =checkboxid =checkalltitle =全选onClick =toggle(this)/>< / th>
< th> Number< / th>
< th>公司< / th>
< th>标签< / th>
< / tr>
< / thead>
< tbody>
<%_.each(array,function(value){%>
< tr>
< td>< input type ='checkbox'name ='numbers [ ]'value ='<%= value.id%>'/>< / td>
< td><%= value.number%>< / td>
< td><%= value.company%>< / td>
< td><%= value.tags%>< / td>
< / tr>
<%})%>
< / tbody>
< / table>

< button type =buttonclass =btn btn-primaryid =checkall2> SELECT ALL< / button>

< script>

$(document).ready(function(){

$('#numbers_table')。dataTable({
//bPaginate:false,
aoColumnDefs:[
{bSortable:false,aTargets:[0]}
]
});

$ #checkall2)。click(function(){//一个带有checkall2的按钮作为其id
$(':checkbox')。prop('checked',true); //所有的复选框,你可以缩小更好的选择器
});
});
< / script>


解决方案

尝试此代码:

  var table = $('#numbers_table')。DataTable(); 

var cells = table
.cells(:checkbox)
.nodes();

$(cells).prop('checked',true);

来源。


I know this might seem primitive, but I've been trying to implement it for a whole day, maybe because I can't fully comprehend how to use the API, I'm using DataTables 1.10.0, I have a table with pagination feature, each row has one checkbox in it, I need to have a "check all button" that would check all the checkboxes in all pages, the problem is that it only checks the checkboxes in the current page, and leaves the other pages unchecked, this is supposed to be easy, but I couldn't figure it out ! the answers I found use "fnGetNodes" which seems to be deprecated and not used by version 1.10

Edit: this is my markup

        <table class="table table-striped table-bordered table-hover" id="numbers_table">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="checkall" title="Select all" onClick="toggle(this)"/></th>
                        <th>Number</th>
                        <th>Company</th>
                        <th>Tags</th>
                    </tr>
                </thead>
                <tbody>
                    <% _.each(array, function (value) { %>
                    <tr>
                        <td><input type='checkbox' name='numbers[]' value='<%=value.id%>'/></td>
                        <td><%= value.number %></td>
                        <td><%= value.company %></td>
                        <td><%= value.tags %></td>
                    </tr>
                    <% }) %>
                </tbody>
            </table>

    <button type="button" class="btn btn-primary" id="checkall2">SELECT ALL</button>

<script>

$(document).ready(function() {

    $('#numbers_table').dataTable({
        //"bPaginate": false,
        "aoColumnDefs": [
      { "bSortable": false, "aTargets": [ 0 ] }
    ] 
        });

    $("#checkall2").click(function() { // a button with checkall2 as its id
        $(':checkbox').prop('checked', true); // all checkboxes, you can narrow with a better selector
    });
});
</script>

解决方案

Try this code:

var table = $('#numbers_table').DataTable();

var cells = table
    .cells( ":checkbox" )
    .nodes();

$( cells ).prop('checked', true);

Source.

这篇关于数据表1.10“全部检查”通过jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆