gpt4 book ai didi

jquery - 如果使用jquery过滤功能表tbody中没有记录,如何显示消息 "no record found."

转载 作者:行者123 更新时间:2023-12-01 08:34:15 24 4
gpt4 key购买 nike

我想向消息显示“未找到记录”。如果搜索后表中没有结果。这是jsfiddle https://jsfiddle.net/jclaude/yxs8v1a9/2/

HTML

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>

<h2>Filterable Table</h2>
<p>Type something in the input field to search the table for first names, last names or emails:</p>
<input id="myInput" type="text" placeholder="Search..">
<br><br>

<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary@mail.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july@greatstuff.com</td>
</tr>
<tr>
<td>Anja</td>
<td>Ravendale</td>
<td>a_r@test.com</td>
</tr>
</tbody>
</table>

<p>Note that we start the search in tbody, to prevent filtering the table headers.</p>

</body>
</html>

jQuery

$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});

CSS

table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}

我尝试在 jquery 中搜索此过滤功能,但不幸的是我没有找到任何解决方案/类似于我的问题。我还查看文档 https://api.jquery.com/filter/

最佳答案

尝试下面的代码

$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr:not('.no-records')").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
var trSel = $("#myTable tr:not('.no-records'):visible")
// Check for number of rows & append no records found row
if(trSel.length == 0){
$("#myTable").html('<tr class="no-records"><td colspan="3">No record found.</td></tr>')
}
else{
$('.no-records').remove()
}

});
});

我们在这里所做的是检查是否有任何可见的行,如果没有,那么我们正在附加一个自定义行,其中表示没有记录,否则我们删除该自定义行。 如果您有任何疑问,请告诉我。

Checks the number of visible rows:

var trSel =  $("#myTable tr:not('.no-records'):visible")
trSel.length

Appends a row saying no records:

$("#myTable").html('<tr class="no-records"><td colspan="3">No record found.</td></tr>')

Removes the row which says no records

$('.no-records').remove()

关于jquery - 如果使用jquery过滤功能表tbody中没有记录,如何显示消息 "no record found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58557275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com