How do I hide the middle of a table using jQuery?

前端 未结 6 880
时光取名叫无心
时光取名叫无心 2020-12-23 12:28

I have a really long 3 column table. I would like to


    <         
相关标签:
6条回答
  • 2020-12-23 13:07

    The easiest way is to add a <tbody> in to group the rows and toggle that between none and table-row-group (catch exceptions and set it to block for IE). Not sure about making it specific to jquery but that's the "normal" way of doing things.

    0 讨论(0)
  • 2020-12-23 13:09

    Try to use slice() method:

    $("#table tr").slice(1, 4).hide();
    
    0 讨论(0)
  • 2020-12-23 13:16

    Here's a solution which doesn't require any extra markup and it degrades nicely.

    <table id="myTable">
        <tbody>
            <tr><td>Cell</td><td>Cell</td></tr>
            <tr><td>Cell</td><td>Cell</td></tr>
            <tr><td>Cell</td><td>Cell</td></tr>
            <tr><td>Cell</td><td>Cell</td></tr>
            <tr><td>Cell</td><td>Cell</td></tr>
        </tbody>
    </table>
    

    and the jQuery... I've hardcoded in a few things here (like the table identifier, number of rows to show, etc. These could be put into a class attribute on the table if you wanted it to be more reusable. (eg: <table class="hidey_2">)

    var showTopAndBottom = 2,
        minRows = 4,
        $rows = $('#myTable tr').length),
        length = $rows.length
    ;
    if (length > minRows) {
        $rows
            .slice(showTopAndBottom, length - showTopAndBottom)
            .hide()
        ;
        $rows
            .eq(showTopAndBottom - 1)
            .after(
                // this colspan could be worked out by counting the cells in another row
                $("<tr><td colspan=\"2\">Show</td></tr>").click(function() {
                    $(this)
                        .remove()
                        .nextAll()
                        .show()
                    ;
                })
            )
        ;
    }
    
    0 讨论(0)
  • 2020-12-23 13:21

    If you give your middle <tr /> tags the "Table_Middle" class it makes it much easier to do. Then it only takes a few lines of jQuery. One to add the "Show Full Table" row, and another to add a click listener for that row. Make sure to change the colspan attribute's "X" value to the number of columns in your table.

     // jQuery chaining is useful here
     $(".Table_Middle").hide()
                       .eq(0)
                       .before('<tr colspan="X" class="showFull">Show Full Table<tr/>');
    
    $(".showFull").click(function() {
        $(this).toggle();
        $(".Table_Middle").toggle();
    });
    

    This is useful because it degrades nicely and is accessible across lots of browsers/devices. If JavaScript is turned off, or CSS is disabled (or any other scenario that could cause this code to not be supported), there is no "show full table" row.

    0 讨论(0)
  • 2020-12-23 13:31

    Something like this could work:

    <table>
        <tbody>
          <tr><td>Column1</td><td>Column2</td></tr>
          <tr><td>Column1</td><td>Column2</td></tr>
          <tr class="Show_Rows"><td>Start</td><td>Hiding</td></tr>
        </tbody>
        <tbody class="Table_Middle" style="display:none">
          <tr><td>Column1</td><td>Column2</td></tr>
          <tr><td>Column1</td><td>Column2</td></tr>
          <tr><td>Column1</td><td>Column2</td></tr>
        </tbody>
        <tbody>
          <tr class="Show_Rows"><td>End</td><td>Hiding</td></tr>
          <tr><td>Column1</td><td>Column2</td></tr>
          <tr><td>Column1</td><td>Column2</td></tr>
        </tbody>
    </table>
    
    
    $('#something').click( function() {
        $('.Table_Middle').hide();
        $('.Show_Rows').show();
    });
    
    $('.Show_Rows').click( function() { 
        $('.Show_Rows').hide();
        $('.Table_Middle').show();
    });
    
    0 讨论(0)
  • 2020-12-23 13:31

    I'd probably do it like this:

    <table>
        <thead>
            <tr>
                <th>Col1</th>
                <th>Col2</th>
                <th>Col3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>data1</td>
                <td>data1</td>
                <td>data1</td>
            </tr>
            ...
        </tbody>
        <tbody id="hidden-rows">
            <tr>
                <td colspan="3">
                    <a href="#" onclick="$('#hidden-rows').hide();$('#extra-rows').show();">
                        Show hidden rows
                    </a>
                </td>
            </tr>
        </tbody>
        <tbody id="extra-rows" style="display: none;">
            <tr>
                <td>data1</td>
                <td>data1</td>
                <td>data1</td>
            </tr>
            ...
        </tbody>
        <tbody>
            <tr>
                <td>data1</td>
                <td>data1</td>
                <td>data1</td>
            </tr>
            ...
        </tbody>
    </table>
    

    It's not a great method, because it doesn't degrade nicely.

    To get it to degrade nicely, you'd have to have all the rows shown initially, and then hide them with your jQuery document ready function, and also create the row with the link in.

    Also, your method of giving the rows to hide a particular class should also work. The jQuery would look something like this:

    $(document).ready(function() {
        $('tr.Table_Middle').hide();
    });
    

    You'd still need to create the row with the link to unhide them though.

    0 讨论(0)
提交回复
热议问题
Column1Column2