利用Bootstrap實(shí)現(xiàn)表格復(fù)選框checkbox全選
首先來看看實(shí)現(xiàn)的效果圖:
HTML中無需添加額外的一列來表示復(fù)選框,而是由JS完成,所以正常的表格布局就行了:
<table class="table table-bordered table-hover"> <thead> <tr class="success"> <th>類別編號(hào)</th> <th>類別名稱</th> <th>類別組</th> <th>狀態(tài)</th> <th>說明</th> </tr> </thead> <tbody> <tr> <td>C00001</td> <td>機(jī)車</td> <td>機(jī)車</td> <td>有效</td> <td>機(jī)車頭</td> </tr> <tr> <td>C00002</td> <td>車廂</td> <td>機(jī)車</td> <td>有效</td> <td>載客車廂</td> </tr> </tbody> </table>
重點(diǎn)是JS的實(shí)現(xiàn)。復(fù)選框很小,不容易點(diǎn)到,所以點(diǎn)擊每一行也可以選中該行,并用高亮一些CSS樣式表示。點(diǎn)擊復(fù)選框所在單元格也能選中復(fù)選框。
下面是完整代碼和注釋說明:
<!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個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! --> <title>表格</title> <meta name="keywords" content="表格"> <meta name="description" content="這真的是一個(gè)表格" /> <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="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="http://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>類別編號(hào)</th> <th>類別名稱</th> <th>類別組</th> <th>狀態(tài)</th> <th>說明</th> </tr> </thead> <tbody> <tr> <td>C00001</td> <td>機(jī)車</td> <td>機(jī)車</td> <td>有效</td> <td>機(jī)車頭</td> </tr> <tr> <td>C00002</td> <td>車廂</td> <td>機(jī)車</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>'); /*將全選/反選復(fù)選框添加到表頭最前,即增加一列*/ $thr.prepend($checkAllTh); /*“全選/反選”復(fù)選框*/ var $checkAll = $thr.find('input'); $checkAll.click(function(event){ /*將所有行的選中狀態(tài)設(shè)成全選框的選中狀態(tài)*/ $tbr.find('input').prop('checked',$(this).prop('checked')); /*并調(diào)整所有選中行的CSS樣式*/ if ($(this).prop('checked')) { $tbr.find('input').parent().parent().addClass('warning'); } else{ $tbr.find('input').parent().parent().removeClass('warning'); } /*阻止向上冒泡,以防再次觸發(fā)點(diǎn)擊操作*/ event.stopPropagation(); }); /*點(diǎn)擊全選框所在單元格時(shí)也觸發(fā)全選框的點(diǎn)擊操作*/ $checkAllTh.click(function(){ $(this).find('input').click(); }); var $tbr = $('table tbody tr'); var $checkItemTd = $('<td><input type="checkbox" name="checkItem" /></td>'); /*每一行都在最前面插入一個(gè)選中復(fù)選框的單元格*/ $tbr.prepend($checkItemTd); /*點(diǎn)擊每一行的選中復(fù)選框時(shí)*/ $tbr.find('input').click(function(event){ /*調(diào)整選中行的CSS樣式*/ $(this).parent().parent().toggleClass('warning'); /*如果已經(jīng)被選中行的行數(shù)等于表格的數(shù)據(jù)行數(shù),將全選框設(shè)為選中狀態(tài),否則設(shè)為未選中狀態(tài)*/ $checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false); /*阻止向上冒泡,以防再次觸發(fā)點(diǎn)擊操作*/ event.stopPropagation(); }); /*點(diǎn)擊每一行時(shí)也觸發(fā)該行的選中操作*/ $tbr.click(function(){ $(this).find('input').click(); }); } initTableCheckbox(); }); </script> </body> </html>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言留言交流。
相關(guān)文章
JavaScript 獲取元素在父節(jié)點(diǎn)中的下標(biāo)(推薦)
jQuery中直接通過$(this).index()即可得到當(dāng)前元素的下標(biāo)。下面通過實(shí)例給大家介紹JavaScript 獲取元素在父節(jié)點(diǎn)中的下標(biāo),需要的朋友參考下吧2017-06-06Three.js實(shí)現(xiàn)臉書元宇宙3D動(dòng)態(tài)Logo效果
本文主要講述通過 Three.js + Blender 技術(shù)棧,實(shí)現(xiàn) Meta 公司炫酷的 3D 動(dòng)態(tài) Logo,內(nèi)容包括基礎(chǔ)模型圓環(huán)、環(huán)面扭結(jié)、管道及模型生成、模型加載、添加動(dòng)畫、添加點(diǎn)擊事件、更換材質(zhì)等2021-11-11下雪了 javascript實(shí)現(xiàn)雪花飛舞
下雪了,這篇文章主要介紹了javascript實(shí)現(xiàn)雪花飛舞,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04JavaScript判斷字符長度、數(shù)字、Email、電話等常用判斷函數(shù)分享
這篇文章主要介紹了JavaScript判斷字符長度、數(shù)字、Email、電話等常用判斷函數(shù)分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04深入理解事件冒泡(Bubble)和事件捕捉(capture)
下面小編就為大家?guī)硪黄钊肜斫馐录芭?Bubble)和事件捕捉(capture)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05js驗(yàn)證真實(shí)姓名與身份證號(hào),手機(jī)號(hào)的簡單實(shí)例
下面小編就為大家?guī)硪黄猨s驗(yàn)證真實(shí)姓名與身份證號(hào),手機(jī)號(hào)的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07