Sort a table by drop down values (simplify my code

2019-03-01 05:50发布

I have a table. I want the user to be able to be able to filter the table by the option they pick in a given drop down. I have it working, but it's messy and hard to add new rows with (can't get it working in jsfiddle, sorry http://jsfiddle.net/anschwem/Y4cf6/2/). Any simplified code would be greatly appreciated. Also, it would be nice if this code could be restricted to only filtering a certain table, so I can have many tables and many drop downs. If this could be done without row ids, even better. Thanks! My table/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>

Code:

   <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条回答
成全新的幸福
2楼-- · 2019-03-01 06:33

Please find the refactored code "http://jsfiddle.net/5fZv7/3/" here and the code snippet is as below..

html code:

<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>

and the javascript code:

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

Hopes this helps you to maintain the code easily and efficiently.

查看更多
一夜七次
3楼-- · 2019-03-01 06:41

I've refactored your fiddle: http://jsfiddle.net/Y4cf6/4/

By taking advantage of CSS classes and built-in attributes like "value", we can easily make this code more generic.

For this 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>

The Javascript is reduced to essentially a one-liner:

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

Edit: the one case this doesn't handle is giving you a way to show all the rows again. I'll leave this as an exercise for you, but here's a tip: You could read the value of $(this).val(), and if there isn't a value, then show all the rows instead of filtering them.

查看更多
等我变得足够好
4楼-- · 2019-03-01 06:43

You can change your html markup like this

<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>

Then your 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

查看更多
倾城 Initia
5楼-- · 2019-03-01 06:53

You should check out DataTables, it has this kind of filtering built in (fnFilter in the API)

It might be a bit of a learning curve at first, but will be much more flexible in the end.

查看更多
登录 后发表回答