Why does a table in a collapsible Bootstrap panel change width?
Asked Answered
B

2

11

I have put together a Bootply demonstration here: http://www.bootply.com/Ss2aAnzqlZ.

It's a panel with a table as per http://getbootstrap.com/components/#panels-tables. However, I've also made the panel collapsible. The collapsing bit works OK, but the table itself doesn't retain shape. As you'll see in the Bootply, it doesn't fill the width of the panel when you first load the page. When you click "Improvements" in the panel header to collapse the panel, the table takes up the full panel width during the animation, then disappears. When you click again to show the panel content, the table is the full width until the animation stops, at which point, it shrinks back to what looks like an "auto" width.

Oddly enough, inspecting the table element shows that the table itself is full width, but the thead, tbody and tfoot aren't.

I've sort of tracked it down to the presence of the "collapse" class in the table. If you start the Bootply without the "collapse" class, it's full width until you collapse the panel. When you expand it, it goes back to auto width. I don't know why ... do you?

Here's the snippet, but the collapsing doesn't appear to run here. The Bootply is better.

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div style="margin:15px">
    <div class="panel panel-default">
        <div class="panel-heading">
            <a href="#" data-toggle="collapse" data-target="#improvementsPanel" aria-expanded="true" class="">Improvements</a>
        </div>
        <table id="improvementsPanel" class="table panel-collapse collapse in" aria-expanded="true" style="">
            <thead>
                <tr>
                    <th>Description</th>
                    <th class="text-right">Qty</th>
                    <th>UOM</th>
                    <th class="text-right">Rate</th>
                    <th class="text-right">Value</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Item 1</td>
                    <td class="text-right">133.00</td>
                    <td>m²</td>
                    <td class="text-right">425.00</td>
                    <td class="text-right">56,525</td>
                </tr>
                <tr>
                    <td>Item 2</td>
                    <td class="text-right">85.00</td>
                    <td>m²</td>
                    <td class="text-right">70.00</td>
                    <td class="text-right">5,950</td>
                </tr>
                <tr>
                    <td>Item 3</td>
                    <td class="text-right">25.00</td>
                    <td>m²</td>
                    <td class="text-right">100.00</td>
                    <td class="text-right">2,500</td>
                </tr>
                <tr>
                    <td>Item 4</td>
                    <td class="text-right"></td>
                    <td></td>
                    <td class="text-right"></td>
                    <td class="text-right">1,500</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th class="text-right" colspan="4">Total</th>
                    <th class="text-right">66,475</th>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
Bert answered 14/11, 2014 at 5:31 Comment(0)
B
19

The collapse class toggles the display style on the table between none and block and this appears to be interfering with the standard table CSS.

You can resolve this by putting your table inside a div and setting that div to collapse rather than the table.

New Bootply: http://www.bootply.com/L8h2OdpMuD

HTML:

<div style="margin: 15px">
    <div class="panel panel-default">
        <div class="panel-heading">
            <a href="#" data-toggle="collapse" data-target="#improvementsPanel" aria-expanded="true" class="">Improvements</a>
        </div>
        <div id="improvementsPanel" class="panel-collapse collapse in" aria-expanded="true">
            <table class="table">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th class="text-right">Qty</th>
                        <th>UOM</th>
                        <th class="text-right">Rate</th>
                        <th class="text-right">Value</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Item 1</td>
                        <td class="text-right">133.00</td>
                        <td>m²</td>
                        <td class="text-right">425.00</td>
                        <td class="text-right">56,525</td>
                    </tr>
                    <tr>
                        <td>Item 2</td>
                        <td class="text-right">85.00</td>
                        <td>m²</td>
                        <td class="text-right">70.00</td>
                        <td class="text-right">5,950</td>
                    </tr>
                    <tr>
                        <td>Item 3</td>
                        <td class="text-right">25.00</td>
                        <td>m²</td>
                        <td class="text-right">100.00</td>
                        <td class="text-right">2,500</td>
                    </tr>
                    <tr>
                        <td>Item 4</td>
                        <td class="text-right"></td>
                        <td></td>
                        <td class="text-right"></td>
                        <td class="text-right">1,500</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th class="text-right" colspan="4">Total</th>
                        <th class="text-right">66,475</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
Brigade answered 14/11, 2014 at 8:27 Comment(2)
Gah ... I thought the table had to be a direct child of the panel for it to work correctly. Thanks for that.Bert
Unfortunately, put table in a div will result a little UI layout problem: you will see an unwanted border at the bottom of the panel header and more space at the end of the panel. However, you can set the the collapse target to something inside the table, like thead and tbody, and they will be OK.Neau
S
9

Alternatively, you can add the css:

table.collapse.in {
   display: table;
}

Arguably this could be a patch to component-animations.less.

Stamm answered 19/11, 2014 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.