js實(shí)現(xiàn)表格篩選功能

本應(yīng)用就兩個(gè)主要實(shí)現(xiàn):
1.表格的id 和 class之間的命名關(guān)系
請(qǐng)看圖: 將組名和個(gè)人信息聯(lián)表格聯(lián)系起來(lái),這樣會(huì)很好的操作表格

HTML代碼:
<tr class="parent" id="row_01"><td colspan="3">前臺(tái)設(shè)計(jì)組</td></tr> <tr class="child_row_01"><td>張三</td><td>男</td><td>浙江寧波</td></tr> <tr class="child_row_01"><td>李四</td><td>男</td><td>浙江寧波</td></tr> <tr class="child_row_01"><td>胡歌</td><td>男</td><td>浙江寧波</td></tr> <tr class="parent" id="row_02"><td colspan="3">前臺(tái)開(kāi)發(fā)組</td></tr> <tr class="child_row_02"><td>李三</td><td>男</td><td>浙江寧波</td></tr> <tr class="child_row_02"><td>張無(wú)忌</td><td>男</td><td>浙江寧波</td></tr> <tr class="child_row_02"><td>孔子</td><td>男</td><td>浙江寧波</td></tr>
2.就是篩選功能的使用:使用filter聯(lián)合contains將輸入框的字加入contains進(jìn)行篩選
javascript代碼:
//設(shè)置列表查詢(xún)
$("#filterName").keyup(function () {
$("table tbody tr").stop().hide() //將tbody中的tr都隱藏
.filter(":contains('"+($(this).val())+"')").show(); //,將符合條件的篩選出來(lái)
});
下面是完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表格應(yīng)用</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
border: 1px solid #000;
margin:50px auto;
width: 340px;
padding: 10px 10px;
}
.box table{
margin: auto;
}
.box .box-top{
width: 303px;
margin: 5px auto;
}
.box table tr td,th{
padding: 5px 30px;
text-align: center;
}
.box table .parent{
background: lightgray;
}
.selected{
background: gray !important;
}
.selectHeight{
background: darkseagreen !important;
}
</style>
</head>
<body>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function () {
//默認(rèn)讓王五選中
$("tr:contains('王五')").addClass("selectHeight")
//點(diǎn)擊讓其展示出列表 默認(rèn)讓其都隱藏
$(".box tr.parent").click(function () {
$(this)
.toggleClass("selected")
.siblings(".child_"+this.id).stop().toggle();
}).click();//此行代碼表示要立即執(zhí)行
//設(shè)置列表查詢(xún)
$("#filterName").keyup(function () {
$("table tbody tr").stop().hide() //將tbody中的tr都隱藏
.filter(":contains('"+($(this).val())+"')").show(); //,將符合條件的篩選出來(lái)
});
});
</script>
<div class="box">
<div class="box-top">
<span>篩選:</span><input type="text" id="filterName">
</div>
<table>
<thead>
<tr>
<th>姓名</th>
<th>性別</th>
<th>暫住地</th>
</tr>
</thead>
<tbody>
<tr class="parent" id="row_01"><td colspan="3">前臺(tái)設(shè)計(jì)組</td></tr>
<tr class="child_row_01"><td>張三</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_01"><td>李四</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_01"><td>胡歌</td><td>男</td><td>浙江寧波</td></tr>
<tr class="parent" id="row_02"><td colspan="3">前臺(tái)開(kāi)發(fā)組</td></tr>
<tr class="child_row_02"><td>李三</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_02"><td>張無(wú)忌</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_02"><td>孔子</td><td>男</td><td>浙江寧波</td></tr>
<tr class="parent" id="row_03"><td colspan="3">后臺(tái)設(shè)計(jì)組</td></tr>
<tr class="child_row_03"><td>王五</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_03"><td>單志永</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_03"><td>劉粒粒</td><td>男</td><td>浙江寧波</td></tr>
</tbody>
</table>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對(duì)象
- 基于JavaScript實(shí)現(xiàn)前端數(shù)據(jù)多條件篩選功能
- JS實(shí)現(xiàn)table表格內(nèi)針對(duì)某列內(nèi)容進(jìn)行即時(shí)搜索篩選功能
- Angularjs實(shí)現(xiàn)帶查找篩選功能的select下拉框示例代碼
- vuejs通過(guò)filterBy、orderBy實(shí)現(xiàn)搜索篩選、降序排序數(shù)據(jù)
- Vue.js實(shí)現(xiàn)多條件篩選、搜索、排序及分頁(yè)的表格功能
- JS實(shí)現(xiàn)商品篩選功能
- Jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對(duì)象,Map()方法詳解以及數(shù)組中查詢(xún)某值是否存在
- angularjs 實(shí)現(xiàn)帶查找篩選功能的select下拉框?qū)嵗?/a>
- js實(shí)現(xiàn)篩選功能
相關(guān)文章
jQuery實(shí)現(xiàn)點(diǎn)擊后高亮背景固定顯示的菜單效果【附demo源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)點(diǎn)擊后高亮背景固定顯示的菜單效果,可實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊菜單項(xiàng)后呈現(xiàn)出鼠標(biāo)滑過(guò)一樣的背景高亮顯示效果,同時(shí)該顯示效果固定不變,需要的朋友可以參考下2016-09-09
多個(gè)datatable共存造成多個(gè)表格的checkbox都被選中
所以當(dāng)有多個(gè)datatable 引用到一個(gè)頁(yè)面中的時(shí)候,全選事件會(huì)匹配全部的datatable,所以造成全部多個(gè)表格的checkbox被都被選中2013-07-07
Jquery公告滾動(dòng)+AJAX后臺(tái)得到數(shù)據(jù)
ajax得到值,用JQUERY綁定給對(duì)應(yīng)的UL.利用JQUERY的動(dòng)畫(huà)來(lái)實(shí)現(xiàn)他們的滾動(dòng)公告。2011-04-04
jquery easyui validatebox remote的使用詳解
下面小編就為大家?guī)?lái)一篇jquery easyui validatebox remote的使用詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
jquery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)排序Li或Table
這篇文章主要介紹了使用jquery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)排序Li或Table,需要的朋友可以參考下2014-05-05
jquery ajax 簡(jiǎn)單范例(界面+后臺(tái))
jquery ajax示例包含界面及后臺(tái)部分,經(jīng)測(cè)試效果還不錯(cuò),喜歡的朋友可以拿去用,或二次修改2013-11-11
基于jquery編寫(xiě)的橫向自適應(yīng)幻燈片切換特效的實(shí)例代碼
全屏自適應(yīng)jquery焦點(diǎn)圖切換特效,在IE6這個(gè)瀏覽器兼容性問(wèn)題上得到了和諧,兼容IE6,適用瀏覽器:IE6、IE7、IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗.相關(guān)代碼2013-08-08

