Showing partial text in bootstrap collapse accordion
Asked Answered
C

3

13

I want to replace a script I have that allows users to "read more" with the Bootstrap collapse accordion. My problem is that as far as I've seen the accordion is either open or closed.

Does anyone know about an option to show some text in closed mode?

In the script I'm using right now I can make some text visible by changing the height of the text area, but in Bootstrap this option doesn't work. When I change the height of .in or .out (the css that controls the text area height), it simply opens and closes until that height. Has anybody found a workaround for this?

My HTML:

<div class="accordion" id="accordion2"> 
  <div class="accordion-group"> 
    <div class="accordion-heading"> 
      <a class="accordion-toggle"  href="#collapseOne"
         data-toggle="collapse" data-parent="#accordion2">
        TITLE OF THE COLLAPSE 
    </a> 
    </div> 
    <div id="collapseOne" class="accordion-body collapse in"> 
      <div class="accordion-inner"> 
        DATA IN THE COLLAPSE 
      </div> 
    </div>
  </div>
</div>
Covert answered 2/9, 2013 at 5:15 Comment(3)
please show some code details pr demo so that we could help you in brief...Nepheline
Sorry for previous postings @Nepheline but every time I click enter SO posts message. This is the bootstrap code <div class="accordion" id="accordion2"> <div class="accordion-group"> <div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne"> TITLE OF THE COLLAPSE </a> </div> <div id="collapseOne" class="accordion-body collapse in"> <div class="accordion-inner"> DATA IN THE COLLAPSE </div> </div> </div>Covert
can you make a fiddle(jsfiddle.net) for this and describe...Nepheline
M
5

Sure you can! Accordions are just collapse controls that have some automatic wiring provided by bootstrap.

The only thing the collapse does is toggle the visibility of specified section plus provide some animations. If you'd like something else to be visible, even when the toggled section is hidden, just put it outside the toggled section so it's always there. Collapsing won't change that.

Here's an example with a teaser panel that's always visible before the main panel. You can style it however you want.

Example

Screenshot example of Accordion with preview text

Demo in Stack Snippets

<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion"
           href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Panel - Header - 1
        </a>
      </h4>
    </div>
    <div class="panel-teaser panel-body" >
      Panel - Teaser - 1
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" 
         role="tabpanel" aria-labelledby="headingOne">

      <div class="panel-body">
        Panel - Body - 1
      </div>
    </div>
  </div>
  
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" 
           href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Panel - Header - 2
        </a>
      </h4>
    </div>
    <div class="panel-teaser panel-body">
      Panel - Teaser - 2
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" 
         role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Panel - Body - 2
      </div>
    </div>
  </div>
  
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingThree">
      <h4 class="panel-title">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion"
           href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
          Panel - Header - 3
        </a>
      </h4>
    </div>
    <div class="panel-teaser panel-body">
      Panel - Teaser - 3
    </div>
    <div id="collapseThree" class="panel-collapse collapse" 
         role="tabpanel" aria-labelledby="headingThree">
      <div class="panel-body">
        Panel - Body - 3
      </div>
    </div>
  </div>
  
</div>
Maciemaciel answered 23/5, 2015 at 22:51 Comment(5)
This works, thanks for the reply. The problem is that I'm getting an album review from mysql through php and it is just a long block of text. In your code's case, how do you put just the first 2 paragraphs under header 1 and the hide the rest of the code under header two? I should add... I'm running this website under Joomla, which Bootstrap version is still 2.3.2.Covert
@kyleMit any update on above comment?Leibniz
@Awais, Instead of re-doing this approach, aded another answer below that I believe has what you're looking for - Probably LizardKG as wellMaciemaciel
@Maciemaciel thanks for the quick response, Can this code will be used for truncate text on 100 char limit instead of that white gradient overlay? like in close collapse it shows only 100 char with ... and on toggle it show whole content.Leibniz
@Awais, If you're going to stop at exactly 100 characters, you'll have to use JS at some point to bin the text into the first 100 chars and the remainder - CSS can't give you an exact character count. So you could probably restructure your DOM to match this example, although at this point, running out of space on the original question to handle more use cases. You can try asking a new question with what you're looking for and what you've tried so far.Maciemaciel
M
2

Here's another approach if you can't breakup your DOM into different pieces for the preview + expanded text, but have one long block of content.

Bootstrap uses the selector .collapse:not(.show) to hide the accordion-body. We can override those styles to be visible, but only up to a certain height. Then, to indicate there's additional text, we can borrow some styling from How to apply a CSS gradient over a text, from a transparent to an opaque colour.

Add the accordion-preview class to the top level component and add the following styles:

.accordion-preview .collapse:not(.show) {
    display: block;
}
.accordion-preview .collapse:not(.show) .accordion-body {
    max-height: 90px;
    position: relative;
    overflow: hidden;
}

.accordion-preview .collapse:not(.show) .accordion-body:after {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 100%;
    width: 100%;
    content: "";
    background: linear-gradient(to top,
       rgba(255,255,255, 1) 5%, 
       rgba(255,255,255, 0) 60%
    );
    pointer-events: none; /* so the text is still selectable */
}

Example

Example screenshot of Accordion with Preview

Demo in Stack Snippets

.accordion-preview .collapse:not(.show) {
    display: block;
}
.accordion-preview .collapse:not(.show) .accordion-body {
    max-height: 90px;
    position: relative;
    overflow: hidden;
}

.accordion-preview .collapse:not(.show) .accordion-body:after {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 100%;
    width: 100%;
    content: "";
    background: linear-gradient(to top,
       rgba(255,255,255, 1) 5%, 
       rgba(255,255,255, 0) 60%
    );
    pointer-events: none; /* so the text is still selectable */
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" ></script>

<div class="accordion accordion-preview" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        Accordion Item #1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>
Maciemaciel answered 24/5, 2022 at 13:56 Comment(0)
P
1

I don't know if i've understood you right, but do you want to show a teaser text under the title? Why not just add the text to the panel-titel with jquery:

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
        1 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.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        2 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. 
      </div>
    </div>
  </div>
</div>

$(function(){
    $("#accordion .panel").each(function(){
        var thispanel = $(this);
        var titletext = thispanel.find(".panel-body").text().substring(1, 100) + " ... click to read more";
        thispanel.find("h4.panel-title a").html(titletext);
    });
});
Pentahedron answered 27/10, 2015 at 17:5 Comment(1)
Thank you very much. This works too :D But as I noted before, what I'd need to make it work the way I want to is to be able to show just the first 2 paragraphs of a review and keep the read hidden under the accordion.Covert

© 2022 - 2024 — McMap. All rights reserved.