Creating Accordion Table with Bootstrap

后端 未结 3 1781
长发绾君心
长发绾君心 2020-12-13 02:14

I have a table that\'s populated from a database which has lots of columns (around 30). A solution someone thought of was to create an accordion out the table, where each ro

相关标签:
3条回答
  • 2020-12-13 02:50

    This seems to be already asked before:

    This might help:

    Twitter Bootstrap Use collapse.js on table cells [Almost Done]

    UPDATE:

    Your fiddle wasn't loading jQuery, so anything worked.

    <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/

    I also added colspan='2' to the details row. But it's essentially your fiddle with jQuery loaded (in frameworks in the left column)

    0 讨论(0)
  • 2020-12-13 02:54

    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:

    .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.

    Here is updated fiddle: http://jsfiddle.net/Nb7wy/2372/

    0 讨论(0)
  • 2020-12-13 02:59

    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:

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

    I modified this example to do so here: http://jsfiddle.net/QLfMU/116/

    0 讨论(0)
提交回复
热议问题