当RowFilter不匹配时显示消息
Show message when RowFilter doesn't match
我正在为我的表创建一个带有JavaScript的RowFilter,一切工作正常,但当搜索输入的文本不匹配时,我想显示一条消息(没有发现结果…表内<但我不知道怎么做,这是我的HTML:>
<div class="container">
<input class="form-control" type="text" id="buscar" placeholder="search..." />
<hr />
<div class="row">
<table class="table table-striped" id="Tabla">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Level</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>This is my name</td>
<td>Level Master 45</td>
</tr>
<tr>
<td>2</td>
<td>This is my name number 2</td>
<td>Level Master 1</td>
</tr>
<tr>
<td>3</td>
<td>This is my name number 3</td>
<td>Level Mastermind 4</td>
</tr>
</tbody>
</table>
<hr />
</div>
</div>
这里是JS:
$(document).ready(function () {
RowFilter();
});
function RowFilter() {
var busqueda = document.getElementById('buscar');
var table = document.getElementById("Tabla").tBodies[0];
buscaTabla = function() {
texto = busqueda.value.toLowerCase();
var r = 0;
while (row = table.rows[r++]) {
if (row.innerText.toLowerCase().indexOf(texto) !== -1)
row.style.display = null;
else {
row.style.display = 'none';
}
}
}
busqueda.addEventListener('keyup', buscaTabla);
}
这里是JSFiddle
中的演示工作示例
我试图在js函数的else部分显示警告,但警告显示了很多次(而row = table.rows[r++])。
这是原始JSFiddle的一个分支,当没有搜索结果时显示一条消息。
https://jsfiddle.net/reid_horton/yg98jqcj/首先,将希望显示的元素添加到表上方的HTML(或表内)。
<div id="no-results">
No results!
</div>
设置默认为隐藏
#no-results {
display: none;
}
要检测什么时候没有搜索结果,将循环改为:
var didMatch = false;
var r = 0;
while (row = table.rows[r++]) {
if (row.innerText.toLowerCase().indexOf(texto) !== -1) {
row.style.display = null;
didMatch = true;
} else {
row.style.display = 'none';
}
}
if (!didMatch) {
noResults.style.display = 'block';
} else {
noResults.style.display = 'none';
}
didMatch
变量用于跟踪是否匹配任何结果。当它为真时,隐藏#no-results
,当它为假时,显示它
试试
$(document).ready(function(){
$("#buscar").on("input",function(){
$value = $(this).val();
$("tr").not(":first").hide();
$len = $("td:contains(" + $value + ")").closest("tr").show().length;
if($len < 1)
$(".no").show(1000);
else
$(".no").hide();
})
})
最终代码:
<html>
<title>This is test</title>
<head>
<style>
.no {
border: 1px solid gray;
text-align: center;
background-color: skyblue;
padding: 5px;
color: #fff;
display: none;
}
</style>
</head>
<body>
<div class="container">
<input class="form-control" type="text" id="buscar" placeholder="search..." />
<hr />
<div class="no">No Result</div>
<div class="row">
<table class="table table-striped" id="Tabla">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Level</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>This is my name</td>
<td>Level Master 45</td>
</tr>
<tr>
<td>2</td>
<td>This is my name number 2</td>
<td>Level Master 1</td>
</tr>
<tr>
<td>3</td>
<td>This is my name number 3</td>
<td>Level Mastermind 4</td>
</tr>
</tbody>
</table>
<hr />
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#buscar").on("input",function(){
$value = $(this).val();
$("tr").not(":first").hide();
$len = $("td:contains(" + $value + ")").closest("tr").show().length;
if($len < 1)
$(".no").show(1000);
else
$(".no").hide();
})
})
</script>
</body>
</html>
相关文章:
- 如果下载不成功,如何显示消息
- 如何在 jquery 中的速率按钮下方显示消息状态
- 删除所有可拖动元素后如何显示消息
- php表单验证n在表单上显示消息3秒钟并淡出
- 单击“点赞”按钮时显示消息
- javascript初学者-无法在屏幕上显示消息框
- 在表单提交后显示消息的方法
- 遍历列表,并在jQuery为空时显示消息
- 在jquery幻灯片登录表单上显示消息
- 如果文本筛选器在ng重复中未返回结果,则显示消息
- 以javascript显示消息框
- 在angular 1中,我如何检测angular是否受支持,并在屏幕上显示消息's不受支持
- 浏览器全屏显示,按键时不显示消息
- 选取随机数组元素,将其移除,并在没有更多可用元素时显示消息
- 仅当浏览器不是Google Chrome时显示消息
- 如果所有李都被隐藏,则显示消息
- 在提交表单和显示消息之前,如何检查表单输入是否全是数字
- 如果使用 JavaScript 登录失败,如何在登录页面中显示消息
- j查询验证未显示消息
- 为什么不显示消息