【问题标题】:jQuery datatable and Header CheckboxjQuery 数据表和标题复选框
【发布时间】:2013-04-17 18:20:58
【问题描述】:

我正在使用 jQuery 数据表,我希望有一列复选框,并且标题应包含一个全选复选框。我在数据表官方网站上没有找到任何关于此的详细信息。

有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 欢迎来到 StackOverflow。你有一些你尝试过的代码示例吗?

标签: jquery checkbox datatable


【解决方案1】:

一种方法是使用数据表的 aoColumns 属性,并将“sTitle”属性的标记呈现为 html 字符串。

http://www.datatables.net/usage/columns

//On datatable init the options would look something like this
"aoColumns": [{   "sTitle": "<input type='checkbox' id='selectAll'></input>"}]

然后您可以在创建数据表后将处理程序连接到标题复选框以选中/取消选中所有复选框;

比如:

$("#selectAll").toggle(function () { 
       $("checkboxSelector", dataTable.fnGetNodes()).attr("checked", true); }
     , function () { 
         $("checkboxSelector", dataTable.fnGetNodes()).attr("checked", false); 
     }
 );

【讨论】:

  • Cdlong,你能解释一下这段代码吗?那会很有帮助。
【解决方案2】:

你可以有如下一行

<tr>
<th><input type="checkbox" id="selectall"/></th>
<th>Cell phone</th>
<th>Rating</th>

和 jquery 连接起来

<SCRIPT language="javascript">
$(function(){

// add multiple select / deselect functionality
$("#selectall").click(function () {
      $('.case').attr('checked', this.checked);
});

// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){

    if($(".case").length == $(".case:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }

    });
   });
  </SCRIPT>

【讨论】:

  • 点击这个文本框后获取所有记录怎么样?
猜你喜欢
  • 1970-01-01
  • 2012-02-26
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2012-10-01
  • 2018-05-29
  • 2012-10-17
相关资源
最近更新 更多