利用Bootstrap实现表格复选框checkbox全选
作者:shangmingchao 时间:2024-10-28 09:00:47
首先来看看实现的效果图:
HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了:
<table class="table table-bordered table-hover">
<thead>
<tr class="success">
<th>类别编号</th>
<th>类别名称</th>
<th>类别组</th>
<th>状态</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>C00001</td>
<td>机车</td>
<td>机车</td>
<td>有效</td>
<td>机车头</td>
</tr>
<tr>
<td>C00002</td>
<td>车厢</td>
<td>机车</td>
<td>有效</td>
<td>载客车厢</td>
</tr>
</tbody>
</table>
重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。
下面是完整代码和注释说明:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>表格</title>
<meta name="keywords" content="表格">
<meta name="description" content="这真的是一个表格" />
<meta name="HandheldFriendly" content="True" />
<link rel="shortcut icon" href="img/favicon.ico">
<!-- Bootstrap3.3.5 CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="panel-group">
<div class="panel panel-primary">
<div class="panel-heading">
列表
</div>
<div class="panel-body">
<div class="list-op" id="list_op">
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
</button>
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
</button>
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
</button>
</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr class="success">
<th>类别编号</th>
<th>类别名称</th>
<th>类别组</th>
<th>状态</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>C00001</td>
<td>机车</td>
<td>机车</td>
<td>有效</td>
<td>机车头</td>
</tr>
<tr>
<td>C00002</td>
<td>车厢</td>
<td>机车</td>
<td>有效</td>
<td>载客车厢</td>
</tr>
</tbody>
</table>
<div class="panel-footer">
<nav>
<ul class="pagination pagination-sm">
<li class="disabled">
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div><!-- end of panel-footer -->
</div><!-- end of panel -->
</div>
<!-- jQuery1.11.3 (necessary for Bo otstrap's JavaScript plugins) -->
<script src="js/jquery-1.11.3.min.js "></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js "></script>
<script>
$(function(){
function initTableCheckbox() {
var $thr = $('table thead tr');
var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
/*将全选/反选复选框添加到表头最前,即增加一列*/
$thr.prepend($checkAllTh);
/*“全选/反选”复选框*/
var $checkAll = $thr.find('input');
$checkAll.click(function(event){
/*将所有行的选中状态设成全选框的选中状态*/
$tbr.find('input').prop('checked',$(this).prop('checked'));
/*并调整所有选中行的CSS样式*/
if ($(this).prop('checked')) {
$tbr.find('input').parent().parent().addClass('warning');
} else{
$tbr.find('input').parent().parent().removeClass('warning');
}
/*阻止向上冒泡,以防再次触发点击操作*/
event.stopPropagation();
});
/*点击全选框所在单元格时也触发全选框的点击操作*/
$checkAllTh.click(function(){
$(this).find('input').click();
});
var $tbr = $('table tbody tr');
var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>');
/*每一行都在最前面插入一个选中复选框的单元格*/
$tbr.prepend($checkItemTd);
/*点击每一行的选中复选框时*/
$tbr.find('input').click(function(event){
/*调整选中行的CSS样式*/
$(this).parent().parent().toggleClass('warning');
/*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/
$checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
/*阻止向上冒泡,以防再次触发点击操作*/
event.stopPropagation();
});
/*点击每一行时也触发该行的选中操作*/
$tbr.click(function(){
$(this).find('input').click();
});
}
initTableCheckbox();
});
</script>
</body>
</html>
来源:http://blog.csdn.net/shangmingchao/article/details/49761315
标签:bootstrap,复选框,全选,表格
0
投稿
猜你喜欢
组件:Adodb.Stream 用法介绍
2008-10-09 12:39:00
Bresenham图形算法JavaScript版本
2010-01-25 12:09:00
禁止背景图在网页中平铺
2011-04-29 14:10:00
JavaScript Memoization
2008-05-01 12:48:00
MySQL 复制详解及简单实例
2024-01-15 12:50:28
ASP分页类(支持多风格变换)
2011-04-08 10:39:00
通过T-SQL语句实现数据库备份与还原的代码
2011-12-01 08:02:15
Python多线程获取返回值代码实例
2023-03-25 17:42:27
Python2和Python3的共存和切换使用
2022-12-26 13:05:20
潜谈产品设计中的可用性和可访问性
2009-01-18 12:47:00
TensorFlow Autodiff自动微分详解
2021-06-02 10:33:02
Python中元组的概念及应用小结
2022-03-28 15:01:05
如何使数据库中取出的数据保持原有格式
2008-11-27 16:16:00
SQL Server并行操作优化避免并行操作被抑制而影响SQL的执行效率
2024-01-23 10:55:50
python实现学生通讯录管理系统
2023-05-06 22:00:05
巧用mysql提示符prompt清晰管理数据库的方法
2024-01-24 14:05:07
50个常用sql语句 网上流行的学生选课表的例子
2024-01-24 10:12:23
Python连接数据库并批量插入包含日期记录的操作
2024-01-12 21:07:03
vs10安装之后一些列问题
2024-01-29 11:59:48
Python pandas中apply函数简介以及用法详解
2022-05-11 03:44:47