javascript - 用于表的 Jquery 下拉过滤器
问题描述
我正在使用我在此处也找到的代码,该代码根据下拉选择过滤表。我不精通 JQuery/Javascript。我的问题是如何使用此代码段“显示所有”数据:
落下:
- 显示所有
- 完毕
- 进行中
- 还没开始
$(document).ready(function($) {
$('#selectField').change(function() {
var selection = $(this).val();
var dataset = $('table').find('tr');
dataset.show();
dataset.filter(function(index, item) {
return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1;
}).hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id='selectField'>
<option value=" "> Show All </option>
<option value="Done"> Done </option>
<option value="On-going"> On-going</option>
<option value="Not yet started"> Not yet started </option>
</select>
<table border="2">
<tr>
<td>Done</td>
</tr>
<tr>
<td>On-going</td>
</tr>
<tr>
<td>Done</td>
</tr>
<tr>
<td>Not yet started</td>
</tr>
<tr>
<td>Not yet started</td>
</tr>
</table>
解决方案
您可以检查下拉列表的值,然后过滤它
喜欢
$(document).ready(function($) {
$('#selectField').change(function() {
var selection = $(this).val();
var dataset = $('table').find('tr');
var unSelectedData;
dataset.show();
if (selection !== ' ') {
var unSelectedData = dataset.filter(function(index, item) {
return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1;
})
}
if (unSelectedData) {
unSelectedData.hide();
}
});
});
$(document).ready(function($) {
$('#selectField').change(function() {
var selection = $(this).val();
var dataset = $('table').find('tr');
var unSelectedData;
dataset.show();
if (selection !== ' ') {
var unSelectedData= dataset.filter(function(index, item) {
return $(item).find('td:nth-child(1)').text().split(',').indexOf(selection) === -1;
})
}
if (unSelectedData) {
unSelectedData.hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id='selectField'>
<option value=" "> Show All </option>
<option value="Done"> Done </option>
<option value="On-going"> On-going</option>
<option value="Not yet started"> Not yet started </option>
</select>
<table border="2">
<tr>
<td>Done</td>
</tr>
<tr>
<td>On-going</td>
</tr>
<tr>
<td>Done</td>
</tr>
<tr>
<td>Not yet started</td>
</tr>
<tr>
<td>Not yet started</td>
</tr>
</table>
推荐阅读
- javascript - 从存档(jar)中的文件导入
- python - 如何使用 Python 在 iframe 中切换 Selenium 焦点
- postgresql - PosgreSQL 阻止进入某些“表”模式
- reactjs - Websocket、状态管理和更新时的空值
- ios - 如何以编程方式将不同的图像添加到 XCode 中的第二个场景?
- java - Java 和 squid 代理主机名解析
- grafana - 将单独的信号流合并到一个表中,并为每个时间间隔的每个信号提供一个聚合值
- javascript - 如何在本地主机上测试 matrix.org?
- python - 常规与使用 For 循环时的 Numpy 数组乘法差异
- powershell - PowerBI Invoke-PowerBIRestMethod 提取异常跟踪