简体   繁体   English

在Bootstrap数据表中使用纯CSS3隐藏表行

[英]Hide table rows using pure CSS3 in Bootstrap datatable

I use Bootstrap with datatables and would like to hide specific table rows using pure CSS, without any JavaScript / jQuery. 我将Bootstrap与datatables结合使用,并希望使用纯CSS隐藏特定的表行, 而无需任何JavaScript / jQuery。

I thought, I can do it like this, but it doesn't work, unfortunately: 我以为可以这样做,但不幸的是,它不起作用:

 input[name=hide_rows] + table tr.hide-this-row { display: table-row; } input[name=hide_rows]:checked + table tr.hide-this-row { display: none; } 
 <div class="form-group"> <label for="hide_rows">Hide rows?</label> <input type="checkbox" id="hide_rows" name="hide_rows" checked> </div> <div class="table-responsive"> <table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'> <thead> <tr> <th>Head #1</th> <th>Head #2</th> <th>Head #3</th> </tr> </thead> <tbody> <tr class="show-this-row"> <td>Row #1</td> <td>Row #1</td> <td>Row #1</td> </tr> <tr class="hide-this-row"> <td>Row #2</td> <td>Row #2</td> <td>Row #2</td> </tr> <tr class="show-this-row"> <td>Row #3</td> <td>Row #3</td> <td>Row #3</td> </tr> @endforeach </tbody> </table> </div> 

Please note, that Bootstrap and the datatable is adding some divs there and at the end, it has some more HTML tags between. 请注意,Bootstrap和数据表在其中添加了一些divs ,最后,它之间还有一些HTML标记。

That's the reason, why I don't would like to use this solution: 这就是原因,为什么我不想使用此解决方案:

 input[name=hide_rows] + div > div > div > table tr.hide-this-row { display: table-row; } input[name=hide_rows]:checked + div > div > div > table tr.hide-this-row { display: none; } 
 <div class="table-responsive"> <label for="hide_rows">Hide rows?</label> <input type="checkbox" id="hide_rows" name="hide_rows" checked> <table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'> <thead> <tr> <th>Head #1</th> <th>Head #2</th> <th>Head #3</th> </tr> </thead> <tbody> <tr class="show-this-row"> <td>Row #1</td> <td>Row #1</td> <td>Row #1</td> </tr> <tr class="hide-this-row"> <td>Row #2</td> <td>Row #2</td> <td>Row #2</td> </tr> <tr class="show-this-row"> <td>Row #3</td> <td>Row #3</td> <td>Row #3</td> </tr> @endforeach </tbody> </table> </div> 

I don't like this solution as you have to count and add that much div s as there are between and I also had to move the checkbox outside the <div class="form-group"> and inside the <div class="table-responsive"> . 我不喜欢这种解决方案,因为您必须计算并添加尽可能多的div ,而且我还必须将复选框移到<div class="form-group"><div class="table-responsive">

It would be much better, if there is an easier solution like the below code for the first shown HTML code: 如果有一个更简单的解决方案,例如下面显示的第一个HTML代码的代码,那就更好了:

input[name=hide_rows] + * table tr.hide-this-row {
        display: table-row;
}

input[name=hide_rows]:checked + * table tr.hide-this-row {
        display: none;
}

The last CSS code is not working, unfortunately. 不幸的是,最后的CSS代码无法正常工作。

You can achieve this with minor change in your html. 您可以通过在html中进行较小的更改来实现此目的。 If you move your table-responsive div inside the form-group below checkbox then you will be able to hide the rows. 如果将table-responsive div移动到复选框下面的form-group内,则可以隐藏行。

Here is a code: 这是一个代码:

<div class="form-group">
    <label for="hide_rows">Hide rows?</label>
    <input type="checkbox" id="hide_rows" name="hide_rows" checked/>

<div class="table-responsive">
    <table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'>
        <thead>
            <tr>
                <th>Head #1</th>
                <th>Head #2</th>
                <th>Head #3</th>
            </tr>
        </thead>
        <tbody>
            <tr class="show-this-row">
                <td>Row #1</td>
                <td>Row #1</td>
                <td>Row #1</td>
            </tr>

            <tr class="hide-this-row">
                <td>Row #2</td>
                <td>Row #2</td>
                <td>Row #2</td>
            </tr>

            <tr class="show-this-row">
                <td>Row #3</td>
                <td>Row #3</td>
                <td>Row #3</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
</div>

CSS: CSS:

input[name=hide_rows] + .table-responsive .hide-this-row {
        display: table-row;
}

input[name=hide_rows]:checked + .table-responsive .hide-this-row  {
        display: none;
}

Snippet: 片段:

 input[name=hide_rows] + .table-responsive .hide-this-row { display: table-row; } input[name=hide_rows]:checked + .table-responsive .hide-this-row { display: none; } 
 <div class="form-group"> <label for="hide_rows">Hide rows?</label> <input type="checkbox" id="hide_rows" name="hide_rows" checked/> <div class="table-responsive"> <table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'> <thead> <tr> <th>Head #1</th> <th>Head #2</th> <th>Head #3</th> </tr> </thead> <tbody> <tr class="show-this-row"> <td>Row #1</td> <td>Row #1</td> <td>Row #1</td> </tr> <tr class="hide-this-row"> <td>Row #2</td> <td>Row #2</td> <td>Row #2</td> </tr> <tr class="show-this-row"> <td>Row #3</td> <td>Row #3</td> <td>Row #3</td> </tr> @endforeach </tbody> </table> </div> </div> 

You can also check it here 您也可以在这里检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM