【问题标题】:Sort a table by drop down values (simplify my code)按下拉值对表格进行排序(简化我的代码)
【发布时间】:2013-01-09 21:27:06
【问题描述】:

我有一张桌子。我希望用户能够通过他们在给定下拉列表中选择的选项来过滤表格。我让它工作了,但是添加新行很麻烦而且很难(不能让它在 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>

【问题讨论】:

  • 等等..你想排序还是过滤?我可能误解了
  • 你可能想要链接到一个工作的 jsfiddle。

标签: javascript jquery select drop-down-menu filter


【解决方案1】:

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

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();
   });
 });

希望这有助于您轻松高效地维护代码。

【讨论】:

  • 工作!漂亮,我要求的一切。谢谢。
【解决方案2】:

我已经重构了你的小提琴: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() 的值,如果没有值,则显示所有行而不是过滤它们。

【讨论】:

  • 请记住,ID 在整个文档中应该是唯一的,而类非常适合在具有相同类型或“类”内容的任何地方重用!
  • 嗯,试图在页面上实现它,它清除了我的整个表。 :/ 不是这种情况的解决方案。
  • 页面上的标记可能有问题 - 请检查小提琴以获取工作示例。
  • 这很好..唯一奇怪的是如果没有选择过滤器..为什么所有的tr都被隐藏了?
【解决方案3】:

您可以像这样更改您的 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
    }
});

FIDDLE

【讨论】:

  • 它没有做任何改变。不知道该告诉你什么。
  • @triplethreat77 您是否将 tr id's 更改为 classes?确保您的选项也具有正确的值。并始终将这些绑定代码包装在 document.ready 函数中
  • 另一个过滤案例,如果你有兴趣:stackoverflow.com/questions/14530741/…
【解决方案4】:

你应该看看DataTables,它内置了这种过滤器(API 中的 fnFilter)

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

【讨论】:

  • 我不能使用数据表。很神奇,就是太重了。我将不得不围绕它重建整个站点。相信我,我试过了。
  • 太糟糕了。你的意思是太重了,因为它使用起来很复杂吗?您能否详细说明您正在构建的内容?
  • 我得到了下面的解决方案。我正在修复一个非常损坏的网站,这是一种修补工作。任何新脚本都会把所有事情都搞砸,所以我需要简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2017-05-02
  • 1970-01-01
相关资源
最近更新 更多