以下是 jQuery表格行全选反选单选代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery表格行全选反选单选代码</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container" style="margin-top:150px;">
<table class="table table-bordered table-striped">
<thead>
<th style="width:20px;"><input type="checkbox"></th>
<th>编程语言</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>C</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Java</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>PHP</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>JavaScript</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>C++</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Python</td>
</tr>
</tbody>
</table>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/jquery.tableCheckbox.js"></script>
<script>
$('table').tableCheckbox({ /* options */ });
</script>
</div>
</body>
</html>
JS代码(jquery.tableCheckbox.min.js):
!function(e){
e.fn.tableCheckbox=function(t){
var n={
config:e.extend({
selectedRowClass:"warning",checkboxSelector:'td:first-of-type input[type="checkbox"],th:first-of-type input[type="checkbox"]',isChecked:function(e){
return e.is(":checked")}
}
,t),registry:{
shiftKeyIsPressed:!1}
,helpers:{
selection:window.getSelection?window.getSelection():document.selection?document.selection:null,removeTextSelection:function(){
n.helpers.selection&&(n.helpers.selection.empty?n.helpers.selection.empty():n.helpers.selection.removeAllRanges())}
,hasSelection:function(){
return!!n.helpers.selection&&n.helpers.selection.toString().length}
}
}
;
return e(document).on("keydown.tsc keyup.tsc",function(e){
n.registry.shiftKeyIsPressed=e.shiftKey}
),this.each(function(){
var t=e(this),c=t.find("thead tr "+n.config.checkboxSelector),i=t.find("tr "+n.config.checkboxSelector).not(c),o=[];
c.on("change",function(e){
i.prop("checked",n.config.isChecked(c)).trigger("change")}
),i.each(function(){
var t=e(this),c=t.parents("tr");
t.on("change",function(e,i){
n.helpers.removeTextSelection(),!i&&n.registry.shiftKeyIsPressed&&o.length&&($inbetween=o.index()<c.index()?c.prevUntil(o):c.nextUntil(o),$inbetween.find(n.config.checkboxSelector).prop("checked",n.config.isChecked(t)).trigger("change",[!0])),o=c,c.toggleClass(n.config.selectedRowClass,n.config.isChecked(t))}
),c.on("click",function(i){
n.helpers.hasSelection()||(e.data(c,"tc-timeout")&&window.clearTimeout(e.data(c,"tc-timeout")),e.data(c,"tc-timeout",window.setTimeout(function(){
n.helpers.hasSelection()||e(i.target).is("a,input,button")||e(i.target).parents("a,input,button").length||t.prop("checked",!n.config.isChecked(t)).trigger("change")}
,50)))}
)}
)}
)}
}
(jQuery);