1

我有一张桌子。我希望用户能够通过他们在给定下拉列表中选择的选项来过滤表格。我有它的工作,但它是混乱和难以添加新行(不能让它在 jsfiddle 中工作,抱歉http://jsfiddle.net/anschwem/Y4cf6/2/)。任何简化的代码将不胜感激。此外,如果可以将此代码限制为仅过滤某个表,那就太好了,这样我就可以拥有许多表和许多下拉菜单。如果这可以在没有行 ID 的情况下完成,那就更好了。谢谢!我的表/html:

<table>
<tr id="catRow">
  <td id="cats">cats</td>
</tr>
<tr id="catRow2">
  <td id="cats">cats</td>
</tr>
<tr id="dogRow">
  <td id="dogs">dogs</td>
</tr>
<tr id="birdRow">
  <td id="birds">birds</td>
</tr>
<tr>
  <td id="dogRow2">dogs</td>
</tr>
</table>

                <select id="selectFilter">
                <option id="sel_All">Select...</option>
                <option id="selCats">Cats</option>
                <option id="selDogs">Dogs</option>
                <option id="selBirds">Birds</option>
                </select>

代码:

   <script type='text/javascript'> 
    $(window).load(function(){
     $('select').change(function() {

  if($('#selectFilter option:selected').attr('id') == "sel_All" || $('#selectFilter option:selected').attr('id') == "sel_All"){$('#catRow').show();$('#catRow2').show();$('#dogRow').show();$('#dogRow2').show();$('#birdRow').show();}

  if($('#selectFilter option:selected').attr('id') == "selCats" || $('#selectFilter option:selected').attr('id') == "selCats"){$('#catRow').show();$('#catRow2').show();$('#dogRow').hide();$('#dogRow2').hide();$('#birdRow').hide();}

  if($('#selectFilter option:selected').attr('id') == "selDogs" || $('#selectFilter option:selected').attr('id') == "selDogs"){$('#catRow').hide();$('#catRow2').hide();$('#dogRow').show();$('#dogRow2').show();$('#birdRow').hide();}

  if($('#selectFilter option:selected').attr('id') == "selBirds" || $('#selectFilter option:selected').attr('id') == "selBirds"){$('#catRow').hide();$('#catRow2').hide();$('#dogRow').hide();$('#dogRow2').hide();$('#birdRow').show();}
    </script>
4

4 回答 4

4

我重构了你的小提琴:http: //jsfiddle.net/Y4cf6/4/

通过利用 CSS 类和“值”等内置属性,我们可以轻松地使这段代码更通用。

对于这个 HTML:

<table id="animals">
    <tr class="cat">
        <td>Cat 1</td>
    </tr>
    <tr class="cat">
        <td>Cat 2</td>
    </tr>
    <tr class="dog">
        <td>Dog 1</td>
    </tr>
    <tr class="cat">
        <td>Cat 3</td>
    </tr>
    <tr class="bird">
        <td>Bird 1</td>
    </tr>
    <tr class="cat">
        <td>Cat 4</td>
    </tr>
    <tr class="dog">
        <td>Dog 2</td>
    </tr>
</table>

<select id="selectFilter">
    <option>Select...</option>
    <option value="cat">Cats</option>
    <option value="dog">Dogs</option>
    <option value="bird">Birds</option>
</select>

Javascript 本质上被简化为单行:

$("#selectFilter").on("change", function() {
    $("#animals").find("tr").hide().filter("." + $(this).val()).show();
});

编辑:这不能处理的一种情况是给你一种再次显示所有行的方法。我将此作为练习留给您,但这里有一个提示:您可以读取 的值$(this).val(),如果没有值,则显示所有行而不是过滤它们。

于 2013-01-25T20:42:17.040 回答
2

您可以像这样更改您的 html 标记

<table id='animal'>
  <tr class="cat">
    <td>cats</td>
  </tr>
  <tr class="cat">
    <td>cats</td>
  </tr>
  <tr class="dog">
    <td>dogs</td>
  </tr>
  <tr class="bird">
    <td>birds</td>
  </tr>
  <tr class='dog'>
    <td>dogs</td>
  </tr>
</table>
<select id="selectFilter">
  <option value=''>Select...</option>
  <option value='cat'>Cats</option>
  <option value='dog'>Dogs</option>
  <option value='bird'>Birds</option>
</select>

然后你的 jQuery

$('#selectFilter').change(function(){
    var trs = $('#animal tr'); 
    if(this.value == ''){  // if first option picked show all
         trs.show(); 
    }else{
       var $el = $('.'+this.value);  // element to show
       trs.not($el).hide(); // hide all but the elements to show
       $el.show();       // show elements
    }
});

小提琴

于 2013-01-25T20:39:44.597 回答
2

请在此处找到重构的代码“ http://jsfiddle.net/5fZv7/3/ ”,代码片段如下..

html代码:

<select id="selectFilter">
       <option id="all">Select...</option>
       <option id="cats">Cats</option>
       <option id="dogs">Dogs</option>
       <option id="birds">Birds</option>
</select>
<table border="1">
  <tr class="all cats">
    <td>cats</td>
 </tr>
 <tr class="all cats">
    <td>cats 2</td>
 </tr>
 <tr class="all dogs">
    <td>dogs</td>
 </tr>
 <tr class="all birds">
    <td>birds</td>
 </tr>

和javascript代码:

 $(document).load(function () {
   $('#selectFilter').change(function () {
     $(".all").hide();
     $("." + $(this).find(":selected").attr("id")).show();
   });
 });

希望这可以帮助您轻松有效地维护代码。

于 2013-01-25T20:59:41.183 回答
1

您应该查看DataTables,它内置了这种过滤功能(API中的 fnFilter )

一开始可能有点学习曲线,但最终会更加灵活。

于 2013-01-25T20:37:42.400 回答