jquery - 使用多个下拉列表过滤 HTML 表格
问题描述
我有两个用于过滤我的 HTML 表格的下拉列表,它们工作正常,但是过滤器不能一起工作。例如,如果我选择存档,数据会被正确过滤,但是当我选择另一个过滤器时,它会取消过滤,然后只过滤另一个过滤器。我怎样才能使两者一起工作或更清洁的方式来实现呢?
$(function() {
$('#archive').change(function() {
if (this.value == "archived") {
$("#filter")
.find("tbody tr")
.hide()
.filter(function() {
return $(this).children('td').eq(3).text().trim() !== '';
}).show();
} else if (this.value == "not-archived") {
$("#filter").find("tbody tr").hide().filter(function() {
return $(this).children('td').eq(3).text().trim() === '';
}).show();
} else {
$("#filter").find("tbody tr").show();
}
});
$('#type').change(function() {
if (this.value == "bookers") {
$("#filter")
.find("tbody tr")
.hide()
.filter(function() {
return $(this).children('td').eq(4).text().trim() === 'True';
}).show();
} else if (this.value == "basics") {
$("#filter").find("tbody tr").hide().filter(function() {
return $(this).children('td').eq(4).text().trim() === 'False';
}).show();
} else {
$("#filter").find("tbody tr").show();
}
});
// show bookers by default
$("#filter")
.find("tbody tr")
.hide()
.filter(function() {
return $(this).children('td').eq(4).text().trim() === 'True';
}).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="type">Full Entry</label>
<select id="type" name="type">
<option value="all">All</option>
<option value="bookers" selected>Bookers</option>
<option value="basics">Basics</option>
</select>
<label for="archive">Filter Archive</label>
<select id="archive" name="archive">
<option value="all">All</option>
<option value="archived">Archived</option>
<option value="not-archived">Not Archived</option>
</select>
<table class="table" style="width: 30%" id="filter">
<thead>
<tr>
<th>Ref</th>
<th>Edit</th>
<th>Name</th>
<th>Archived</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<th>
<td>a</td>
<td>b</td>
<td>c</td>
<td>03/04/12</td>
<td>True</td>
</th>
<th>
<td>a</td>
<td>b</td>
<td>c</td>
<td></td>
<td>True</td>
</th>
<th>
<td>a</td>
<td>b</td>
<td>c</td>
<td>10/10/12</td>
<td>False</td>
</th>
</tbody>
</table>
解决方案
好的,我对此进行了盲编码,但是您可以像下面这样处理它吗?
一种一次性过滤项目的方法。
<label for="type">Full Entry</label>
<select id="type" name="type" class="filter-field">
<option value="all">All</option>
<option value="bookers" selected>Bookers</option>
<option value="basics">Basics</option>
</select>
<label for="archive">Filter Archive</label>
<select id="archive" name="archive" class="filter-field">
<option value="all">All</option>
<option value="archived">Archived</option>
<option value="not-archived">Not Archived</option>
</select>
$(function () {
$('.filter-field').change(function () {
//get the values
var _archived = $("#archived").val();
var _type = $("#type").val();
if(typeof _type == "undefined") _type = "bookers"; //show bookers by default
$("#filter")
.find("tbody tr")
.hide()
.filter(function () {
var result = $(this).children('td');
if(_archived == 'archived') {
result = result.eq(3).text().trim() !== '';
} else if (_archived=='not-archived') {
result = result.eq(3).text().trim() === '';
}
if(_type=='bookers') {
result = result.eq(4).text().trim() === 'True';
} else if(_type=='basics' {
result = result.eq(4).text().trim() === 'False';
}
return result;
}).show();
});
});
推荐阅读
- python - 如何使用 if-else 从一个函数到另一个函数
- python - 如何迭代每个`numpy.float16`
- sql - SQL。从前一行中查找前一个值。滞后()?
- flutter - Flutter 应用程序未通过 MethodChannel 接收来自 Android 的消息
- python - 部署到 Azure 功能后,在 python 中找不到请求模块
- nestjs - AWS ALB + ECS + gRPC + NestJS,'14 不可用:尝试连接 http1.x 服务器'
- dialogflow-es - 撤消操作 Google 迁移
- python - Kedro install 安装失败,但后来几次尝试就成功了
- opencv - 如何在 EmguCV 中执行 Levenberg-Marquardt 优化以进行相机自动校准?
- woocommerce - 基于产品的固定数量仅值无限制-Woocommerce