简体   繁体   English

使用 Bootstrap 创建手风琴表

[英]Creating Accordion Table with Bootstrap

I have a table that's populated from a database which has lots of columns (around 30).我有一个从数据库填充的表,该数据库有很多列(大约 30 列)。 A solution someone thought of was to create an accordion out the table, where each row is clickable and will accordion downwards with the rest of the columns worth of information.有人想到的一个解决方案是在表格外创建一个手风琴,其中每一行都是可点击的,并将与其余列的信息一起向下折叠。 I am having trouble getting Bootstrap to do this properly for me.我无法让 Bootstrap 为我正确执行此操作。

<table class="table table-hover">
    <thead>
        <th></th><th></th><th></th>
    </thead>

    <tbody>
        <tr data-toggle="collapse" data-target="#accordion" class="clickable">
            <td>Some Stuff</td>
            <td>Some more stuff</td>
            <td>And some more</td>
        </tr>
        <tr>
            <td>
                <div id="accordion" class="collapse">Hidden by default</div>
            </td>
        </tr>
    </tbody>
</table>

As you can see from the jsfiddle , this functionality is not working.jsfiddle可以看出,此功能不起作用。 Im not really sure what's wrong, and Bootstrap's docs don't go into much detail on collapsing.我不太确定出了什么问题,Bootstrap 的文档没有详细介绍折叠。

Any assistance would be greatly appreciated, thanks!任何帮助将不胜感激,谢谢!

This seems to be already asked before:这似乎之前已经问过:

This might help:这可能有帮助:

Twitter Bootstrap Use collapse.js on table cells [Almost Done] Twitter Bootstrap 在表格单元格上使用 collapse.js [几乎完成]

UPDATE:更新:

Your fiddle wasn't loading jQuery, so anything worked.你的小提琴没有加载 jQuery,所以一切正常。

<table class="table table-hover">
<thead>
  <tr>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>

<tbody>
    <tr data-toggle="collapse" data-target="#accordion" class="clickable">
        <td>Some Stuff</td>
        <td>Some more stuff</td>
        <td>And some more</td>
    </tr>
    <tr>
        <td colspan="3">
            <div id="accordion" class="collapse">Hidden by default</div>
        </td>
    </tr>
</tbody>
</table>

Try this one: http://jsfiddle.net/Nb7wy/2/试试这个: http : //jsfiddle.net/Nb7wy/2/

I also added colspan='2' to the details row.我还将colspan='2'添加到详细信息行。 But it's essentially your fiddle with jQuery loaded (in frameworks in the left column)但它本质上是你的 jQuery 加载(在左栏中的框架中)

For anyone who came here looking for how to get the true accordion effect and only allow one row to be expanded at a time, you can add an event handler for show.bs.collapse like so:对于来这里寻找如何获得真正的手风琴效果并且一次只允许展开一行的任何人,您可以为 show.bs.collapse 添加一个事件处理程序,如下所示:

$('.collapse').on('show.bs.collapse', function () {
    $('.collapse.in').collapse('hide');
});

I modified this example to do so here: http://jsfiddle.net/QLfMU/116/我修改了这个例子在这里这样做: http : //jsfiddle.net/QLfMU/116/

In the accepted answer you get annoying spacing between the visible rows when the expandable row is hidden.在接受的答案中,当隐藏可扩展行时,可见行之间的间距会很烦人。 You can get rid of that by adding this to css:您可以通过将其添加到 css 来摆脱它:

.collapse-row.collapsed + tr {
     display: none;
}

'+' is adjacent sibling selector , so if you want your expandable row to be the next row, this selects the next tr following tr named collapse-row. '+' 是相邻的同级选择器,因此如果您希望可扩展行成为下一行,这将选择名为 collapse-row 的 tr 之后的下一个 tr。

Here is updated fiddle: http://jsfiddle.net/Nb7wy/2372/这是更新的小提琴: http : //jsfiddle.net/Nb7wy/2372/

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

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