【问题标题】:Bootstrap Collapse interfering with responsive table layoutBootstrap折叠与响应表布局发生干扰
【发布时间】:2014-04-06 07:46:32
【问题描述】:

我早先提出了一个与此相关的问题,但我意识到我在叫错树,所以我要重新开始。我不知道为什么昨天这成了一个问题,但我已经能够在 jsFiddle - http://jsfiddle.net/dspitzle/22Sr5/4/ 上重现它。我有两个不同的表格实例(代码包含在下面),唯一的区别是我分配了class='requests_targets collapse in' 的 thead 和 tbody 标签以启用折叠功能(使用标题中的 V 形按钮)。具有折叠类的类全部被压扁,因此显然响应式表结构正在被覆盖。

在 Fiddle 上,您会注意到当您折叠部分时(在它们消失之前),表格布局会自行纠正。这到底是怎么回事?您可以看到我包含的唯一 CSS 和 Javascript 是 jquery 和 bootstrap 文件,以及一个用于切换按钮上 V 形的功能。

<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <div class="table-responsive">
                <table class="table table-striped table-condensed" id='request_header'>
                    <caption class="h2">Outstanding Requests [1]
                        <button class="btn section-toggle btn-default btn-sm" data-toggle="collapse" data-target=".requests_targets"><span class="icon_toggle glyphicon glyphicon-chevron-up"></span>
                        </button>
                    </caption>
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Date</th>
                            <th>Service</th>
                            <th>Status</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>LastName, FirstName</td>
                            <td>2/28/2014</td>
                            <td>Books/Toys</td>
                            <td>In Progress</td>
                            <td><a href="#" class="btn btn-default btn-sm" \=""><span class="glyphicon glyphicon-pencil"></span></a>
                            </td>
                            <td><a href="#" class="btn btn-default btn-sm" \=""><span class="glyphicon glyphicon-pencil"></span> Child</a>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="col-sm-12">
            <div class="table-responsive">
                <table class="table table-striped table-condensed" id='request_header2'>
                    <caption class="h2">Outstanding Requests [1]
                        <button class="btn section-toggle btn-default btn-sm" data-toggle="collapse" data-target=".requests_targets"><span class="icon_toggle glyphicon glyphicon-chevron-up"></span>
                        </button>
                    </caption>
                    <thead class="requests_targets collapse in">
                        <tr>
                            <th>Name</th>
                            <th>Date</th>
                            <th>Service</th>
                            <th>Status</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody class="requests_targets collapse in">
                        <tr>
                            <td>LastName, FirstName</td>
                            <td>2/28/2014</td>
                            <td>Books/Toys</td>
                            <td>In Progress</td>
                            <td><a href="#" class="btn btn-default btn-sm" \=""><span class="glyphicon glyphicon-pencil"></span></a>
                            </td>
                            <td><a href="#" class="btn btn-default btn-sm" \=""><span class="glyphicon glyphicon-pencil"></span> Child</a>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

编辑:很奇怪,我一定是在发布之前不小心擦掉了代码

【问题讨论】:

    标签: twitter-bootstrap


    【解决方案1】:

    在您的样式表中使用以下内容。 (调用 bootstrap.css 后)

    #request_header .collapse.in,
    #request_header2 .collapse.in {
        display: table-row-group;
    }
    

    【讨论】:

    • 这比我的解决方案更简单,似乎也能做到。好的。 jsfiddle.net/isherwood/22Sr5/19
    • 太棒了!非常感谢。我仍然不明白为什么昨天突然开始失败,也不明白为什么其他人没有遇到这个,但至少我有一个解决方案!
    【解决方案2】:

    Bootstrap 正在将您的表格元素设置为 display: block(而不是 display: table 等)。您可能希望使用回调来扭转这种情况。

    $('#myCollapsible').on('shown.bs.collapse', function () {
        $('#request_header2 tr').css('display', 'table-row');
        $('#request_header2 th, #request_header2 td').css('display', 'table-cell');
    })
    

    http://getbootstrap.com/javascript/#collapse

    【讨论】:

    • 我不知道那会是什么样子,特别是回调会采用什么形式。
    • 我只是尝试将 thead.in, tbody.in {display: table !important;} 添加到我的 CSS 文件(和小提琴)中,但它本身并没有这样做。
    • 好的,我已经清除了我的 CSS 尝试,并在 Fiddle 上尝试了您的代码(以原始形式并用 .requests_targets 替换 #myCollapsible),但它似乎没有任何区别。根据我对您所做的事情的理解,它确实看起来应该可以工作。
    • 我还尝试扩充您的代码以覆盖整个受影响的结构集: $('.requests_targets').on('shown.bs.collapse', function () { $(' #request_header2 thead').css('display', 'table-header-group'); $('#request_header2 tbody').css('display', 'table-row-group'); $('#request_header2 tr').css('display', 'table-row'); $('#request_header2 th, #request_header2 td').css('display', 'table-cell'); })
    【解决方案3】:

    我认为如果您去掉可折叠表格中的&lt;caption /&gt; 元素并在表格上方写上您的&lt;h2 /&gt; 标题(如this),效果会更好。

    编辑:修复断开的链接。

    EDIT2:通过使表格响应来修复示例。

    【讨论】:

    • 嗯。语义上我的更合适,但你的有工作的好处。我要去看看 isherwood 是否可以帮助我解决他的问题,但如果归根结底,我可能会求助于它。我主要想知道a)为什么这只是昨天才成为问题,b)为什么库的两个组件的这种相对直接的交互会产生这种冲突。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 2016-05-28
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2014-12-09
    • 2019-03-09
    相关资源
    最近更新 更多