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

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

HTML代碼:
<tr class="parent" id="row_01"><td colspan="3">前臺設(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">前臺開發(fā)組</td></tr> <tr class="child_row_02"><td>李三</td><td>男</td><td>浙江寧波</td></tr> <tr class="child_row_02"><td>張無忌</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è)置列表查詢
$("#filterName").keyup(function () {
$("table tbody tr").stop().hide() //將tbody中的tr都隱藏
.filter(":contains('"+($(this).val())+"')").show(); //,將符合條件的篩選出來
});
下面是完整代碼:
<!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è)置列表查詢
$("#filterName").keyup(function () {
$("table tbody tr").stop().hide() //將tbody中的tr都隱藏
.filter(":contains('"+($(this).val())+"')").show(); //,將符合條件的篩選出來
});
});
</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">前臺設(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">前臺開發(fā)組</td></tr>
<tr class="child_row_02"><td>李三</td><td>男</td><td>浙江寧波</td></tr>
<tr class="child_row_02"><td>張無忌</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">后臺設(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>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對象
- 基于JavaScript實(shí)現(xiàn)前端數(shù)據(jù)多條件篩選功能
- JS實(shí)現(xiàn)table表格內(nèi)針對某列內(nèi)容進(jìn)行即時(shí)搜索篩選功能
- Angularjs實(shí)現(xiàn)帶查找篩選功能的select下拉框示例代碼
- vuejs通過filterBy、orderBy實(shí)現(xiàn)搜索篩選、降序排序數(shù)據(jù)
- Vue.js實(shí)現(xiàn)多條件篩選、搜索、排序及分頁的表格功能
- JS實(shí)現(xiàn)商品篩選功能
- Jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對象,Map()方法詳解以及數(shù)組中查詢某值是否存在
- angularjs 實(shí)現(xiàn)帶查找篩選功能的select下拉框?qū)嵗?/a>
- js實(shí)現(xiàn)篩選功能
相關(guān)文章
jquery實(shí)現(xiàn)鼠標(biāo)滑過后動(dòng)態(tài)圖片提示效果實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)鼠標(biāo)滑過后動(dòng)態(tài)圖片提示效果,涉及jquery鼠標(biāo)事件及頁面元素的動(dòng)態(tài)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Jquery Easyui驗(yàn)證組件ValidateBox使用詳解(20)
這篇文章主要為大家詳細(xì)介紹了Jquery Easyui自定義下拉框組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
jQuery中animate的幾種用法與注意事項(xiàng)
在今天之前,我對jQuery的中的animate()方法還停留在最常見的用法,查了一下手冊,發(fā)現(xiàn)在1.8的時(shí)候增加了幾個(gè)比較有用的回調(diào)函數(shù),想著就來總結(jié)下jQuery中animate的幾種用法與注意事項(xiàng),方便自己或者大家有需要的時(shí)候參考借鑒,下面來一起看看吧。2016-12-12
Jquery 動(dòng)態(tài)添加按鈕實(shí)現(xiàn)代碼
在測量中 面是有不同的點(diǎn)組成,在輸入的時(shí)候 需要?jiǎng)討B(tài)的添加 不同點(diǎn)的坐標(biāo)值2010-05-05
jquery實(shí)現(xiàn)的省市區(qū)三級聯(lián)動(dòng)
在做項(xiàng)目的時(shí)候,我們經(jīng)常需要用到地址之類的省市區(qū)三級聯(lián)動(dòng)的,今天就給大家分享一個(gè)非常簡潔的省市區(qū)三級聯(lián)動(dòng)的代碼,基于jQuery,附上GIT地址,有需要的小伙伴可以直接拿走2015-04-04

