【问题标题】:More elegant way to filter out table rows using jQuery?使用 jQuery 过滤表行的更优雅的方法?
【发布时间】:2012-05-03 03:41:19
【问题描述】:

我想让我的表格过滤器更优雅。基本上,该表将在底部有链接,用于根据表中的属性隐藏和显示行。它以我想要的方式工作,但我觉得我可以使用更少的 jQuery 行编写......有人有什么想法吗?

这是 jsfiddle:http://jsfiddle.net/vC88q/1/

还有代码:

<!--*******************************************************
jquery that needs cleaned up appears at the end of this file!
Code is online at JSFIDDLE: http://jsfiddle.net/vC88q/1/
*********************************************************-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
</head>

<body>
    <table> 
        <thead> 
            <tr> 
                <th>Color Table</th>
            </tr> 
        </thead> 
        <tbody> 
            <tr> 
                <td data-color="red">red</td>
            </tr> 
            <tr> 
                <td data-color="white">white</td>
            </tr> 
            <tr> 
                <td data-color="blue">blue</td>
            </tr> 
            <tr> 
                <td data-color="bluewhite">bluewhite</td>
            </tr> 
            <tr> 
                <td data-color="red">red</td>
            </tr>
            <tr> 
                <td data-color="red">red</td>
            </tr> 
            <tr> 
                <td data-color="red">red</td>
            </tr> 
            <tr> 
                <td data-color="blue">blue</td>
            </tr> 
            <tr> 
                <td data-color="blue">blue</td>
            </tr> 
        </tbody> 
    </table> 
    <br />
    <br />
    <div class="filtertable">
        <a href="#" data-color="all">All</a>
        <a href="#" data-color="red">Red</a>
        <a href="#" data-color="white">White</a>
        <a href="#" data-color="blue">Blue</a>
        <a href="#" data-color="bluewhite">Blue and White</a>
    </div>

<script type="text/javascript">

/*******************************************************
Here is the jquery I need help with, how can I reduce the code
I've written? Code is online at JSFIDDLE: http://jsfiddle.net/vC88q/1/
*********************************************************/

$(document).ready(function() 
    { 
    /*****************************************************************
    when users clicks the "all" filter, show all the hidden table rows
    *****************************************************************/
    $('.filtertable a[data-color=all]').click(function() {
        $('td[data-color]').parent().show();
    });
    /****************************************************************
    when users clicks the "red" filter, hide all but red table rows
    *****************************************************************/
    $('.filtertable a[data-color=red]').click(function() {
        $('td[data-color=red]').parent().show();
        $('td[data-color=white]').parent().hide();
        $('td[data-color=blue]').parent().hide();
        $('td[data-color=bluewhite]').parent().hide();
    });
    /*****************************************************************
    when users clicks the "white" filter, hide all but white table rows
    ****************************************************************/
    $('.filtertable a[data-color=white]').click(function() {
        $('td[data-color=white]').parent().show();
        $('td[data-color=red]').parent().hide();
        $('td[data-color=blue]').parent().hide();
        $('td[data-color=bluewhite]').parent().hide();
    });
    /****************************************************************
    when users clicks the "blue" filter, hide all but blue table rows
    *****************************************************************/
    $('.filtertable a[data-color=blue]').click(function() {
        $('td[data-color=blue]').parent().show();
        $('td[data-color=white]').parent().hide();
        $('td[data-color=red]').parent().hide();
        $('td[data-color=bluewhite]').parent().hide();
    });
    /*****************************************************************
    when users clicks the "blue and white" filter, hide all but blue+white table rows
    *****************************************************************/
    $('.filtertable a[data-color=bluewhite]').click(function() {
        $('td[data-color=bluewhite]').parent().show();
        $('td[data-color=white]').parent().hide();
        $('td[data-color=blue]').parent().hide();
        $('td[data-color=red]').parent().hide();
    });
} 
); 
</script>
</body>
</html>

【问题讨论】:

    标签: javascript jquery html-table tablefilter


    【解决方案1】:
    $(document).ready(function()
        {
            $('.filtertable a[data-color]').click(function() {
                var color = $(this).data().color;
                if(color == 'all'){
                     $('td[data-color]').parent().show();
                } else {
                    $('td[data-color='+ color +']').parent().show();
                    $('td[data-color!='+color+']').parent().hide();
                }
            });
        }
    ); 
    

    【讨论】:

    • 太棒了。这非常干净,适用于所有配置。感谢大家的快速回复。我希望我能选择几个正确的答案!
    • 嘿,你能帮我把这个变量翻译成英文吗? var 颜色 = $(this).data().color;啊,我刚刚意识到 .data 是一种访问 data-color 属性的方式...谢谢!
    • 想选择几个答案?这就是投票的目的。 ;-)
    【解决方案2】:

    我经常在这类事情上使用 CSS。

    <table id="myTable" class="all">
      <tr class="red"><td>red</td></tr>
      <tr class="yellow"><td>yellow</td></tr>
      <tr class="blue"><td>blue</td></tr>
    <table>
    
    <div class="controls">
      <a href="#" data-color="all">All</a>
      <a href="#" data-color="red">Red</a>
      <a href="#" data-color="white">White</a>
      <a href="#" data-color="blue">Blue</a>
    </div>
    

    然后在 JS 中

    $('.controls a').on('click', function(e){
      e.preventDefault();
      var c= $(this).data('color');
      $('#myTable')[0].className = c;
    });​
    

    CSS 决定什么时候显示:

    tr { display: none; }
    .all tr { display: table-row; }
    .red tr.red { display: table-row; }
    .blue tr.blue { display: table-row; }
    .yellow tr.yellow { display: table-row; }
    

    JSFiddle

    【讨论】:

    • 很棒的答案。我将对此进行更多探索......这是迄今为止完成我想做的最短的JS......
    【解决方案3】:

    Here's a demo

    $(function() {
        var td = $('#colortable td');
        $('.filtertable').on('click', 'a', function() {
            var color = this.getAttribute('data-color');
            if (color === 'all') {
                td.parent().show()
            } else {
                td.filter('[data-color=' + color + ']').parent().show()
                td.filter('[data-color!=' + color + ']').parent().hide()
            }
        });
    });​
    

    【讨论】:

      【解决方案4】:

      http://jsfiddle.net/vC88q/6/

          $('.filtertable a[data-color]').click(function() {
              var color = $(this).data().color;
              $('td[data-color=' + color + ']').parent().show();
              $('td[data-color!=' + color + ']').parent().hide();
          });
      

      【讨论】:

      • 我在尝试此操作时遇到了“全部”按钮不再起作用的问题...我想我现在明白了,我只需要一个 if/else 来处理它,然后我需要专注于颜色。
      【解决方案5】:

      以下内容应替换您的所有代码。

      $(document).ready(function() {
          $('.filtertable a[data-color]').click(function() {
              var $this = $(this), color = $this.data('color');
              if (color !== 'all') {
                  $('td[data-color]').parent().hide();
              }
              $('td[data-color' + (color !== 'all' ? '=' + color : '') + ']').parent().show();
          });
      });​
      

      编辑:更新以修复“全部”链接

      【讨论】:

      • 你很快 :) 这适用于颜色,但现在“全部”选项由于某种原因不起作用......即使我保持“全部”选项的代码不变。
      • @Fred 我修复了它,现在它可以工作了。 :-) 我第一次忽略了“全部”选项。
      猜你喜欢
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2011-05-08
      相关资源
      最近更新 更多