gpt4 book ai didi

php - 如何向 html 表格的列添加搜索和过滤器?

转载 作者:行者123 更新时间:2023-12-01 08:38:02 25 4
gpt4 key购买 nike

我想向表的 State 列添加搜索和过滤功能,最好使用 jquery。

这是该表的代码。

<div id="container">
<div class="table-responsive">
<p>
Search: <input type="text" id="table-filter" value="">
</p>
<table id ="table" class="table table-striped">

<thead>
<tr>
<th> First Name </th>
<th> Last Name </th>
<th> State </th>
<th> Date of Birth</th>
<th> Action </th>

</tr>
</thead>
<?php
if (count($report) === 0) {
echo "<tr>No Reports</tr>";
} else {
for ($i = 0; $i < count($report); $i++) {
echo
"<tr>
<td>{$report[$i]['First_Name']}</td>
<td>{$report[$i]['Last_Name']}</td>
<td>{$report[$i]['State']}</td>
<td>{$report[$i]['Date_of_Birth']}</td>
<td><a class=btn href='read.php?id=".$read['Id']."'>Read</a></td>

</tr>";

}
}
?>
</table>
</div>
</div>

main.js

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

});

我对 jquery 很陌生。任何帮助将不胜感激。

最佳答案

如果您想在特定列中搜索,请尝试以下操作:https://www.w3schools.com/howto/howto_js_filter_table.asp

对于“state”列,您必须将此行:td = tr[i].getElementsByTagName("td")[0]; 更改为 td = tr[i].getElementsByTagName (“td”)[2];

关于php - 如何向 html 表格的列添加搜索和过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51195278/

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