Bootstrap 3折叠表图标开关

时间:2015-04-14 09:53:41

标签: jquery twitter-bootstrap html-table accordion

我必须使用Bootstrap 3表,其中表行可以点击并且像手风琴一样。

只有一行可以“打开”。 单击折叠的行将:

  • 打开点击折叠的行
  • 如果有任何
  • ,请关闭其他行
  • 将图标从“加”切换为“减号”
  • 如果有任何
  • ,则将其他“减号”图标切换为“加号”

我到目前为止:

HTML:

<div class="container">
<table class="table table-bordered" id="overview-table">

        <div id="body-1">
        <tr data-toggle="collapse" data-target="#text-1" class="accordion-toggle collapsed">
          <td scope="row" class="accordion-label">
             <span class="glyphicon glyphicon-plus-sign"></span>
             <h3>Heading 1</h3>
          </td>
          <td class="checkmark-table"></td>
          <td class="checkmark-table"></td>
          <td class="checkmark-table"></td>
        </tr>
        <tr>
            <td colspan="4" class="hiddenRow">
                <div class="accordian-body collapse clearfix" id="text-1">
                  <div class="collapse-feature-list">
                    <ul>
                      <li>Lorem</li>
                      <li>Lorem</li>
                    </ul>
                  </div>
                  <div class="collapse-link">
                    <a href="#">Link #3</a>
                    <a href="#">Link #2</a>
                    <a href="#">Link #1</a>
                  </div>
                </div>
            </td>
        </tr>
      </div>

            <div id="body-2">
        <tr data-toggle="collapse" data-target="#text-2" class="accordion-toggle collapsed" aria-expanded="false">
          <td scope="row" class="accordion-label">
             <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
             <h3>Heading 1</h3>
          </td>
          <td class="checkmark-table"></td>
          <td class="checkmark-table"></td>
          <td class="checkmark-table"></td>
        </tr>
        <tr>
            <td colspan="4" class="hiddenRow">
                <div class="accordian-body collapse clearfix" id="text-2">
                  <div class="collapse-feature-list">
                    <ul>
                      <li>Lorem</li>
                      <li>Loremr</li>
                    </ul>
                  </div>
                  <div class="collapse-link">
                    <a href="#">Link #3</a>
                    <a href="#">Link #2</a>
                    <a href="#">Link #1</a>
                  </div>
                </div>
            </td>
        </tr>
      </div>

</table>
</div>

JQUERY:

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

        });
        $('.collapse').on('shown.bs.collapse', function() {
          $(".glyphicon").addClass('glyphicon-minus-sign').removeClass('glyphicon-plus-sign');
        });

        $('.collapse').on('hidden.bs.collapse', function() {
            $(".glyphicon").addClass('glyphicon-plus-sign').removeClass('glyphicon-minus-sign');
        });

JSFIDDLE

如何选择特定行以获得上述功能?

2 个答案:

答案 0 :(得分:4)

UPDATE /溶液

我在这里找到了一个仅限CSS的解决方案https://stackoverflow.com/a/18568997/1437245

因为我的.accordion-toggle是<tr>:之前的行为就像另一个表格单元格并打破了布局。所以我不得不使用 .accordion-toggle td:first-child:before.accordion-toggle.collapsed td:first-child:before选择第一个单元格并在其之前放置伪选择器....

使用JSFIDDLE https://jsfiddle.net/DTcHh/6591/

答案 1 :(得分:1)

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

        });
        $('.collapsed').on('click', function() {
            $(".glyphicon").addClass('glyphicon-plus-sign').removeClass('glyphicon-minus-sign');
            if( $(this).find(".glyphicon").hasClass('glyphicon-plus-sign')) {
                $(this).find(".glyphicon").addClass('glyphicon-minus-sign').removeClass('glyphiconplus-sign');

jsfiddle:https://jsfiddle.net/DTcHh/6594/