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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题