dongshao1156 2018-07-05 15:38
浏览 91
已采纳

如何将搜索和过滤器添加到html表的列?

I want to add a search and filter feature to the State column of the table, preferably using jquery.

Here's the code for the table.

<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)
    });
  });

});

I am very new to jquery. Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dtml3340 2018-07-05 17:02
    关注

    if you want to search in specific column try this: https://www.w3schools.com/howto/howto_js_filter_table.asp

    for column 'state' you must change this line: td = tr[i].getElementsByTagName("td")[0]; to td = tr[i].getElementsByTagName("td")[2];

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址