Jquery show/hide table rows

前端 未结 3 1147
盖世英雄少女心
盖世英雄少女心 2020-12-05 10:48

I want to be able to show/hide the rows in a table using jquery. Ideally I want to have buttons above the table to sort the table with.

i.e [Show rows with id:black]

相关标签:
3条回答
  • 2020-12-05 10:56

    Change your black and white IDs to classes instead (duplicate IDs are invalid), add 2 buttons with the proper IDs, and do this:

    var rows = $('table.someclass tr');
    
    $('#showBlackButton').click(function() {
        var black = rows.filter('.black').show();
        rows.not( black ).hide();
    });
    
    $('#showWhiteButton').click(function() {
        var white = rows.filter('.white').show();
        rows.not( white ).hide();
    });
    
    $('#showAll').click(function() {
        rows.show();
    });
    

    <button id="showBlackButton">show black</button>
    <button id="showWhiteButton">show white</button>
    <button id="showAll">show all</button>
    
    <table class="someclass" border="0" cellpadding="0" cellspacing="0" summary="bla bla bla">
        <caption>bla bla bla</caption>
        <thead>
              <tr class="black">
                ...
              </tr>
        </thead>
        <tbody>
            <tr class="white">
                ...
            </tr>
            <tr class="black">
               ...
            </tr>
        </tbody>
    </table>
    

    It uses the filter()[docs] method to filter the rows with the black or white class (depending on the button).

    Then it uses the not()[docs] method to do the opposite filter, excluding the black or white rows that were previously found.


    EDIT: You could also pass a selector to .not() instead of the previously found set. It may perform better that way:

    rows.not( `.black` ).hide();
    
    // ...
    
    rows.not( `.white` ).hide();
    

    ...or better yet, just keep a cached set of both right from the start:

    var rows = $('table.someclass tr');
    var black = rows.filter('.black');
    var white = rows.filter('.white');
    
    $('#showBlackButton').click(function() {
        black.show();
        white.hide();
    });
    
    $('#showWhiteButton').click(function() {
        white.show();
        black.hide();
    });
    
    0 讨论(0)
  • 2020-12-05 11:12

    The filter function wasn't working for me at all; maybe the more recent version of jquery doesn't perform as the version used in above code. Regardless; I used:

        var black = $('.black');
        var white = $('.white');
    

    The selector will find every element classed under black or white. Button functions stay as stated above:

        $('#showBlackButton').click(function() {
               black.show();
               white.hide();
        });
    
        $('#showWhiteButton').click(function() {
               white.show();
               black.hide();
        });
    
    0 讨论(0)
  • 2020-12-05 11:17

    http://sandbox.phpcode.eu/g/corrected-b5fe953c76d4b82f7e63f1cef1bc506e.php

    <span id="black_only">Show only black</span><br>
    <span id="white_only">Show only white</span><br>
    <span id="all">Show all of them</span>
    <style>
    .black{background-color:black;}
    #white{background-color:white;}
    </style>
    <table class="someclass" border="0" cellpadding="0" cellspacing="0" summary="bla bla bla">
    <caption>bla bla bla</caption>
    <thead>
      <tr class="black">
        <th>Header Text</th>
        <th>Header Text</th>
        <th>Header Text</th>
        <th>Header Text</th>
        <th>Header Text</th>
        <th>Header Text</th>
      </tr>
    </thead>
    <tbody>
      <tr id="white">
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
    </tr>
      <tr class="black" style="background-color:black;">
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
        <td>Some Text</td>
    </tr>
    </tbody>
    <script>
    $(function(){
       $("#black_only").click(function(){
        $("#white").hide();
        $(".black").show();
    
       });
       $("#white_only").click(function(){
        $(".black").hide();
        $("#white").show();
    
       });
       $("#all").click(function(){
        $("#white").show();
        $(".black").show();
    
       });
    
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题