drau89457 2015-01-02 19:37
浏览 35
已采纳

实时过滤表格

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Little Things</title>
<?php echo link_tag('resources/css/style.css'); ?>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
      <script type="text/javascript" src="<?php echo base_url('resources/js/script.js');?>" ></script>
      <script type="text/javascript">var $rows = $('#table tr');
$('#search').keyup(function() {
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

    $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
    }).hide();
});
</script>
</head>
<body>
<div id="container">
        <div id="top">
        <img id="logo" src="<?php echo base_url('resources/images/logo.jpg'); ?>" alt="litLogo"/>
        </div>
        <div id="main">
        <div id='cssmenu'>
<ul>
   <li class='active'><a href="<?php echo base_url('index.php/LittleController/index');?>"><span>Home</span></a></li>
   <li class="element"><a href='#'><span>Shopping Cart</span></a></li>

   <li class="element"><a href='#'><span>Wish List</span></a></li>


   <li class="element"><a href='#'><span>Contact</span></a></li>
    <li id="name">
    <?php 
        if($this->session->userdata('email'))
        {
            echo "<a href='#'><span>Welcome ".$user."!</span></a>";
        } 
        else 
        {
            echo "<a href='#'><span>Welcome Guest!</span></a>";
        }
         ?>
            <?php
        if($this->session->userdata('email'))
        {
            echo '<li class="element"><a href="'.base_url("index.php/LittleController/logout").'"><span>Logout</span></a></li>';
        } 
        else
        {
            echo '<li class="element"><a href="'.base_url("index.php/LittleController/login").'"><span>Login</span></a></li>';
        }
        ?>
    </li>
       <li class="element"><a href="<?php echo base_url('index.php/LittleController/signup');?>"><span>Sign Up</span></a></li>
</ul>
</div>
<div class="tables" id="tables">
<input type="text" class="search" placeholder="Type to search">
    <?php
     $imgDir="../../resources/images/thumbs/";
        echo "<table id='table'>";
        echo "<tbody class='list'>";
        echo "<tr bgcolor='#E60000'>";
        echo "<th>Name</th><th>Product Line</th><th>Product Scale</th><th>Product Description</th><th>Price</th><th>Image</th>";
        echo "</tr>";
        $i=0;
        foreach($Products as $key => $value)
        {
        $i++;
            if($i%2==0)
            {
                echo "<tr bgcolor='#EE4D4D' width='100%'>";
                //echo '<td>'.$value["ID"].'</td>';
                echo "<td class='pName'>".$value["productName"]."</td>";
                echo "<td class='pLine'>".$value["productLine"]."</td>";
                echo "<td class='pScale'>".$value["productScale"]."</td>";
                echo "<td class='pDes'>".$value["productDescription"]."</td>";
                echo "<td class='buyPrice'>".$value["buyPrice"]."</td>";
                //echo '<td>'.$value["QtySold"].'</td>';
                echo "<td class='image'><img src='".$imgDir.$value['image']."' height='100' width='100'/></td>";
                //echo '<td>'.$value["ABV"].'</td>';
                echo "</tr>";
            }
            else
            {
                echo "<tr bgcolor='#F28080' width='100%'>";
                //echo '<td>'.$value["ID"].'</td>';
                echo "<td class='pName'>".$value["productName"]."</td>";
                echo "<td class='pLine'>".$value["productLine"]."</td>";
                echo "<td class='pScale'>".$value["productScale"]."</td>";
                echo "<td class='pDes'>".$value["productDescription"]."</td>";
                echo "<td class='buyPrice'>".$value["buyPrice"]."</td>";
                //echo '<td>'.$value["QtySold"].'</td>';
                echo "<td class='image'><img src='".$imgDir.$value['image']."' height='100' width='100'/></td>";
                //echo '<td>'.$value["ABV"].'</td>';
                echo "</tr>";
            }
        }
        echo "</tbody>";
        echo "</table>";

    ?>
    </div>
    <script type="text/javascript">
    var options = {
  valueNames: [ 'pName', 'pLine', 'pScale', 'pDes', 'buyPrice','image' ]
};

var userList = new List('tables', options);</script>
        </div>
        </div>
</body>

</html>

So this is my code. I am trying to filter the table as I am typing into the text box. Any help appreciated.

I've tried following the tutorials but they do not seem to work at all... any ideas?

I find it difficult to understand and apply to my own website, the code I have for filtering is taken from the internet is well.

  • 写回答

1条回答 默认 最新

  • dongshaidu2456 2015-01-02 22:34
    关注

    As I see you're using jQuery so why not use contains selector? You could use it after keyUp event on select tag:

    $("#search").keyup(function(){
      var search = this.value;
      $('#table tr, #table th').show();
      $('#table td:contains('+search+')').each(function(){
         $(this).parent().hide();
      });
    });
    

    This code will hide any cell parent (tr, th...) which have a child td containing input value . You can use this code and make it fits better your needs.

    Fiddle example: http://jsfiddle.net/uby9quLo/2/

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题