Zebra striping a table with hidden rows using CSS3?

后端 未结 9 2283
滥情空心
滥情空心 2020-12-01 20:49

I\'ve got a table

 
&
相关标签:
9条回答
  • 2020-12-01 21:09

    in jquery ..

    var odd = true; 
    $('table tr:visible').each(function() {   
      $(this).removeClass('odd even').addClass(odd?'odd':'even'); 
      odd=!odd 
    });
    
    0 讨论(0)
  • 2020-12-01 21:10

    I came up with a sort of solution but it's reliant on the fact that the table can only ever have a maximum number of hidden rows and comes with the downside of requiring 2 additional CSS rules for each possible hidden row. The principle is that, after each hidden row, you switch the background-color of the odd and even rows around.

    Here's a quick example with just 3 hidden rows and the necessary CSS for up to 4 of them. You can already see how unwieldy the CSS can become but, still, someone may find some use for it:

    table{
      background:#fff;
      border:1px solid #000;
      border-spacing:1px;
      width:100%;
    }
    td{
      padding:20px;
    }
    tr:nth-child(odd)>td{
      background:#999;
    }
    tr:nth-child(even)>td{
      background:#000;
    }
    tr[data-hidden=true]{
      display:none;
    }
    tr[data-hidden=true]~tr:nth-child(odd)>td{
      background:#000;
    }
    tr[data-hidden=true]~tr:nth-child(even)>td{
      background:#999;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(odd)>td{
      background:#999;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(even)>td{
      background:#000;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(odd)>td{
      background:#000;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(even)>td{
      background:#999;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(odd)>td{
      background:#999;
    }
    tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr[data-hidden=true]~tr:nth-child(even)>td{
      background:#000;
    }
    <table>
      <tbody>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr data-hidden="true"><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr data-hidden="true"><td></td><td></td></tr>
        <tr data-hidden="true"><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
        <tr><td></td><td></td></tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2020-12-01 21:16

    You can easily fake the zebra stripes if you apply a vertically repeating gradient on the parent table, positioned exactly to match the rows' height (the rows would have to be transparent). That way the table won't care if anything's hidden, it will repeat no matter what.

    0 讨论(0)
  • 2020-12-01 21:18

    For a jquery way, you could use this function which iterates through the rows in your table, checking the visbility of the row and (re)setting a class for visible odd rows.

        function updateStriping(jquerySelector) {
            var count = 0;
            $(jquerySelector).each(function (index, row) {
                $(row).removeClass('odd');
                if ($(row).is(":visible")) {
                    if (count % 2 == 1) { //odd row
                        $(row).addClass('odd');
                    }
                    count++;
                }            
            });
        }
    

    Use css to set a background for odd rows.

    #mytable tr.odd { background: rgba(0,0,0,.1); }
    

    Then you can call this zebra-striper whenever by using:

    updateStriping("#mytable tr");
    
    0 讨论(0)
  • 2020-12-01 21:23

    If you are using JQuery to change the visibility of rows you can apply the following function to the table to add an .odd class where appropriate. Call it each time the rows visible is different.

            function updateStriping(jquerySelector){
                $(jquerySelector).each(function(index, row){
                    $(row).removeClass('odd');
                    if (index%2==1){ //odd row
                        $(row).addClass('odd');
                    }
                });
            }
    

    And for the css simply do

    table#tableid tr.visible.odd{
        background-color: #EFF3FE;
    }
    
    0 讨论(0)
  • 2020-12-01 21:32

    I add in css:

    tr[style="display: table-row;"]:nth-child(even) {
          background-color:  #f3f6fa;  
    }
    

    and on create tr add in tag

    style="display: table-row;"
    
    0 讨论(0)
提交回复
热议问题