jQuery数据表和标题复选框

时间:2013-04-17 18:20:59

标签: jquery checkbox datatable

我正在使用jQuery Datatable,我希望有一列复选框,标题应包含select all复选框。我在datatable官方网站上没有找到任何关于此的细节。

有人可以帮忙吗?

感谢。

2 个答案:

答案 0 :(得分:4)

一种方法是使用数据表的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); 
     }
 );

答案 1 :(得分:0)

你可以拥有如下所示的行

<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>