doulu1945 2017-08-10 20:06
浏览 80
已采纳

Javascript,搜索/过滤sql表问题

having issues with filtering my table. I want the entire row to be visible if the search comes up with any matches in its respective cells. Currently it is only showing exact cells.

<input type="text" id="searchText" />
  <script type="text/javascript">
    function contains(text_one, text_two) {
      if (text_one.indexOf(text_two) != -1)
        return true;
    }

    $("#searchText").keyup(function () {
      var searchText = $("#searchText").val().toLowerCase()
      $("tr td").each(function () {
        if (!contains($(this).text().toLowerCase(), searchText))
          $(this).hide();
        else
          $(this).show();

      });;
    });;
  </script>

Im using this format for my MySqli table.

while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)):?>


<table border="1">
<!--<table class="fixed" border="1" cellpadding="1" cellspacing="1"> -->
  <col width="100px" />
  <col width="100px" />
  <col width="100px" />
  <col width="100px" />
  <col width="100px" /><tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['name'];?></td>
    <td><?php echo $row['posted'];?></td>
    <td><?php echo $row['category'];?></td>
    <td><?php echo $row['more'];?></td>
</tr>

<?php endwhile;

?>

</table>
  • 写回答

2条回答 默认 最新

  • dongshi6844 2017-08-10 21:10
    关注

    You should loop over rows and cells separately. In the row loop, hide the row by default. Then when you loop over the cells, show the row if any of the cells matches the search text.

    $("#searchText").keyup(function() {
      var searchText = $("#searchText").val().toLowerCase()
      $("tr").each(function() {
        var row = $(this);
        row.hide();
        $("td", row).each(function() {
          if (contains($(this).text().toLowerCase(), searchText)) {
            row.show();
            return false; // no need to keep looping over cells in this row
          }
        });
      });
    });
    
    function contains(text_one, text_two) {
      if (text_one.indexOf(text_two) != -1)
        return true;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="searchText">
    <table border="1">
      <tr>
        <td>Row 1 Field 1 abc</td>
        <td>Row 1 Field 2 def</td>
        <td>Row 1 Field 3 ghi</td>
      </tr>
      <tr>
        <td>Row 2 Field 1 jkl</td>
        <td>Row 2 Field 2 def</td>
        <td>Row 2 Field 3 pqr</td>
      </tr>
      <tr>
        <td>Row 3 Field 1 abc</td>
        <td>Row 3 Field 2 vwx</td>
        <td>Row 3 Field 3 yz</td>
      </tr>
    </table>

    In $.each, returning false from the iteration function tells jQuery to stop looping.

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

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?