1
votes

Hi I'm trying to merge an example of an Accordion/Collapse as in this case:

Bootstrap 4 change caret on clicking dropdown (the accepted answer)

with a Bootstrap table where a row is clickable to collapse/expand the rest.

[data-toggle="collapse"]:after {
display: inline-block;
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  content: "\f054";
  transform: rotate(90deg) ;
  transition: all linear 0.25s;
  }
[data-toggle="collapse"].collapsed:after {
  transform: rotate(0deg) ;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>

<div id="accordion" role="tablist">
  <table class="table">
    <tbody>
      <a data-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">

    <tr role="tab" id="headingOne">

        <td>Mark</td>
        <td>Otto</td>
        <td>@mdo</td>


    </tr>
  </a>
  <div id="collapseOne" class="collapse show" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
    <div class="card-body">
      Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
    </div>
  </div>
</tbody>
</table>




  <div class="card">
    <div class="card-header" role="tab" id="headingTwo">
      <h5 class="mb-0">
        <a class="collapsed" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </a>
      </h5>
    </div>
    <div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo" data-parent="#accordion">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="card">
    <div class="card-header" role="tab" id="headingThree">
      <h5 class="mb-0">
        <a class="collapsed" data-toggle="collapse" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Collapsible Group Item #3
        </a>
      </h5>
    </div>
    <div id="collapseThree" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-parent="#accordion">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
</div>

As you can see it's not working, the arrow up is clickable but it's above a row and the row is not clickable at all. Please advise.

EDIT:

I've taken your code and modified it to be a table only solution (no card). The problem I'm having is that the way I've done it the collapsed div appears below the table for all headers (not immediately below the corresponding header). How would I rearrange the below so the #collapseOne appears straight after the first row of the table, not below the table.

<div id="accordion">
   <table class="table b-dark table-sm p-2 mb-0">
     <tbody>
       <tr id="headingOne" data-toggle="collapse" data-target="#collapseOne">
         <td class="border-bottom b-green text-center text-white">First</td>
         <td class="text-center border-right border-bottom text-white">Description</td>
         <td class="border-bottom text-center text-white">Time</td>
         <td class="border-bottom text-white text-right">
           <i class="fa"></i>
         </td>
       </tr>
       <tr id="headingTwo" data-toggle="collapse" data-target="#collapseTwo">
         <td class="border-bottom b-green text-center text-white">Second</td>
         <td class="text-center border-right border-bottom text-white">Desc</td>
         <td class="border-bottom text-center text-white">Time</td>
         <td class="border-bottom text-white text-right">
           <i class="fa"></i>
         </td>
       </tr>
     </tbody>
   </table>
   <div id="collapseOne" class="collapse show b-dark p-3 mt-0" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
     <div class="text-white">
       Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
     </div>
  </div>
 <div id="collapseTwo" class="collapse show b-dark p-3 mt-0" role="tabpanel" aria-labelledby="headingTwo" data-parent="#accordion">
   <div class="text-white">
     Another anotherprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
   </div>
 </div>

1

1 Answers

3
votes

Come on! You can't have elements like <a></a> other than <tr> right after <tbody>. Hence your <a></a> is rendered outside of the table!

And since your <a></a> doesn't have any content, it's rendered kind of as hidden!

You don't have to use <a><a> as the collapse trigger. You can use any HTML element. You just need to initialize it with data-toggle="collapse". And since it's not an anchor tag, it doesn't have href. Don't worry. You can use data-target="xx" instead.

HTML

<div id="accordion">
    <div class="card">
        <div class="card-header">
            <table>
                <tbody>
                    <tr id="headingOne" data-toggle="collapse" data-target="#collapseOne">
                        ...
                    </tr>
                </tbody>
            </table>
        </div>
        <div id="collapseOne" class="collapse show" data-parent="#accordion">
            <div class="card-body"> ... </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header">...</div>
        <div id="collapseTwo" class="collapse" data-parent="#accordion">
            <div class="card-body"> ... </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header">...</div>
        <div id="collapseThree" class="collapse" data-parent="#accordion">
            <div class="card-body"> ... </div>
        </div>
    </div>
</div>

CSS (SASS)

You can apply a little bit of CSS to make the row look like a link / clickable.

#headingOne {
    cursor: pointer;
    color: var(--primary);
}

Result

enter image description here

demo: https://jsfiddle.net/davidliang2008/1t5gaf49/


Change Caret

I see you also want to change carets after clicking each collapsable. This is easy to accomplish with just CSS because when the collapsable collapses, bootstrap adds .collapsed class there. You can change the font-awesome icon's content to either display left arrow or down arrow based on that.

HTML

<a class="collapsed" data-toggle="collapse">
    Collapsible Group Item
    <span class="float-right">
        <i class="fa"></i>
    </span>
</a>

CSS (SASS)

[data-toggle="collapse"] {
  .fa:after {
    content: '\f078';  /* chevron-down */
  }
  
  &.collapsed {
    .fa:after {
      content: '\f054';  /* chevron-right */
    }
  }
}

Result

enter image description here

demo: https://jsfiddle.net/davidliang2008/e75h3asb/