码迷,mamicode.com
首页 > 其他好文 > 详细

Bootstrap之表格checkbox复选框全选 [转]

时间:2016-09-10 20:41:12      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

转自: http://blog.csdn.net/shangmingchao/article/details/49761315

效果图:

技术分享

技术分享

HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了:

 

  1. <table class="table table-bordered table-hover">  
  2.     <thead>  
  3.         <tr class="success">  
  4.             <th>类别编号</th>  
  5.             <th>类别名称</th>  
  6.             <th>类别组</th>  
  7.             <th>状态</th>  
  8.             <th>说明</th>  
  9.         </tr>  
  10.     </thead>  
  11.     <tbody>  
  12.         <tr>  
  13.             <td>C00001</td>  
  14.             <td>机车</td>  
  15.             <td>机车</td>  
  16.             <td>有效</td>  
  17.             <td>机车头</td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td>C00002</td>  
  21.             <td>车厢</td>  
  22.             <td>机车</td>  
  23.             <td>有效</td>  
  24.             <td>载客车厢</td>  
  25.         </tr>  
  26.     </tbody>  
  27. </table>  

重点是JS的实现。复选框很小,不容易点到,所以点击每一行也可以选中该行,并用高亮一些CSS样式表示。点击复选框所在单元格也能选中复选框。下面是完整代码和注释说明:

 

    1. <!DOCTYPE html>  
    2. <html lang="zh-CN">  
    3.   
    4.     <head>  
    5.         <meta charset="utf-8">  
    6.         <meta http-equiv="X-UA-Compatible" content="IE=edge">  
    7.         <meta name="viewport" content="width=device-width, initial-scale=1">  
    8.         <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->  
    9.         <title>表格</title>  
    10.         <meta name="keywords" content="表格">  
    11.         <meta name="description" content="这真的是一个表格" />  
    12.         <meta name="HandheldFriendly" content="True" />  
    13.         <link rel="shortcut icon" href="img/favicon.ico">  
    14.         <!-- Bootstrap3.3.5 CSS -->  
    15.         <link href="css/bootstrap.min.css" rel="stylesheet">  
    16.   
    17.         <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->  
    18.         <!--[if lt IE 9]>  
    19.             <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>  
    20.             <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>  
    21.         <![endif]-->  
    22.     </head>  
    23.   
    24.     <body>  
    25.         <div class="panel-group">  
    26.             <div class="panel panel-primary">  
    27.                 <div class="panel-heading">  
    28.                     列表  
    29.                 </div>  
    30.                 <div class="panel-body">  
    31.                     <div class="list-op" id="list_op">  
    32.                         <button type="button" class="btn btn-default btn-sm">  
    33.                             <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增  
    34.                         </button>  
    35.                         <button type="button" class="btn btn-default btn-sm">  
    36.                             <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改  
    37.                         </button>  
    38.                         <button type="button" class="btn btn-default btn-sm">  
    39.                             <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除  
    40.                         </button>  
    41.                     </div>  
    42.                 </div>  
    43.                 <table class="table table-bordered table-hover">  
    44.                     <thead>  
    45.                         <tr class="success">  
    46.                             <th>类别编号</th>  
    47.                             <th>类别名称</th>  
    48.                             <th>类别组</th>  
    49.                             <th>状态</th>  
    50.                             <th>说明</th>  
    51.                         </tr>  
    52.                     </thead>  
    53.                     <tbody>  
    54.                         <tr>  
    55.                             <td>C00001</td>  
    56.                             <td>机车</td>  
    57.                             <td>机车</td>  
    58.                             <td>有效</td>  
    59.                             <td>机车头</td>  
    60.                         </tr>  
    61.                         <tr>  
    62.                             <td>C00002</td>  
    63.                             <td>车厢</td>  
    64.                             <td>机车</td>  
    65.                             <td>有效</td>  
    66.                             <td>载客车厢</td>  
    67.                         </tr>  
    68.                     </tbody>  
    69.                 </table>  
    70.                 <div class="panel-footer">  
    71.                     <nav>  
    72.                         <ul class="pagination pagination-sm">  
    73.                             <li class="disabled">  
    74.                                 <href="#" aria-label="Previous">  
    75.                                     <span aria-hidden="true"</span>  
    76.                                 </a>  
    77.                             </li>  
    78.                             <li class="active"><href="#">1</a></li>  
    79.                             <li><href="#">2</a></li>  
    80.                             <li><href="#">3</a></li>  
    81.                             <li><href="#">4</a></li>  
    82.                             <li><href="#">5</a></li>  
    83.                             <li>  
    84.                                 <href="#" aria-label="Next">  
    85.                                     <span aria-hidden="true"</span>  
    86.                                 </a>  
    87.                             </li>  
    88.                         </ul>  
    89.                     </nav>  
    90.                 </div><!-- end of panel-footer -->  
    91.             </div><!-- end of panel -->  
    92.         </div>  
    93.         <!-- jQuery1.11.3 (necessary for Bo otstrap‘s JavaScript plugins) -->  
    94.         <script src="js/jquery-1.11.3.min.js "></script>  
    95.         <!-- Include all compiled plugins (below), or include individual files as needed -->  
    96.         <script src="js/bootstrap.min.js "></script>  
    97.         <script>  
    98.         $(function(){  
    99.             function initTableCheckbox() {  
    100.                 var $thr = $(‘table thead tr‘);  
    101.                 var $checkAllTh = $(‘<th><input type="checkbox" id="checkAll" name="checkAll" /></th>‘);  
    102.                 /*将全选/反选复选框添加到表头最前,即增加一列*/  
    103.                 $thr.prepend($checkAllTh);  
    104.                 /*“全选/反选”复选框*/  
    105.                 var $checkAll = $thr.find(‘input‘);  
    106.                 $checkAll.click(function(event){  
    107.                     /*将所有行的选中状态设成全选框的选中状态*/  
    108.                     $tbr.find(‘input‘).prop(‘checked‘,$(this).prop(‘checked‘));  
    109.                     /*并调整所有选中行的CSS样式*/  
    110.                     if ($(this).prop(‘checked‘)) {  
    111.                         $tbr.find(‘input‘).parent().parent().addClass(‘warning‘);  
    112.                     } else{  
    113.                         $tbr.find(‘input‘).parent().parent().removeClass(‘warning‘);  
    114.                     }  
    115.                     /*阻止向上冒泡,以防再次触发点击操作*/  
    116.                     event.stopPropagation();  
    117.                 });  
    118.                 /*点击全选框所在单元格时也触发全选框的点击操作*/  
    119.                 $checkAllTh.click(function(){  
    120.                     $(this).find(‘input‘).click();  
    121.                 });  
    122.                 var $tbr = $(‘table tbody tr‘);  
    123.                 var $checkItemTd = $(‘<td><input type="checkbox" name="checkItem" /></td>‘);  
    124.                 /*每一行都在最前面插入一个选中复选框的单元格*/  
    125.                 $tbr.prepend($checkItemTd);  
    126.                 /*点击每一行的选中复选框时*/  
    127.                 $tbr.find(‘input‘).click(function(event){  
    128.                     /*调整选中行的CSS样式*/  
    129.                     $(this).parent().parent().toggleClass(‘warning‘);  
    130.                     /*如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态*/  
    131.                     $checkAll.prop(‘checked‘,$tbr.find(‘input:checked‘).length == $tbr.length ? true : false);  
    132.                     /*阻止向上冒泡,以防再次触发点击操作*/  
    133.                     event.stopPropagation();  
    134.                 });  
    135.                 /*点击每一行时也触发该行的选中操作*/  
    136.                 $tbr.click(function(){  
    137.                     $(this).find(‘input‘).click();  
    138.                 });  
    139.             }  
    140.             initTableCheckbox();  
    141.         });  
    142.         </script>  
    143.     </body>  
    144.   
    145. </html

Bootstrap之表格checkbox复选框全选 [转]

标签:

原文地址:http://www.cnblogs.com/jimcsharp/p/5860129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!