Twitter bootstrap collapse: change display of toggle button

后端 未结 10 690
有刺的猬
有刺的猬 2020-12-12 23:50

I am using Twitter Bootstrap to create collapsible sections of text. The sections are expanded when a + button is pressed. My html code as follows:

<         


        
相关标签:
10条回答
  • 2020-12-13 00:02

    Some may take issue with changing the Bootstrap js (and perhaps validly so) but here is a two line approach to achieving this.

    In bootstrap.js, look for the Collapse.prototype.show function and modify the this.$trigger call to add the html change as follows:

    this.$trigger
      .removeClass('collapsed')
      .attr('aria-expanded', true)
      .html('Collapse')
    

    Likewise in the Collapse.prototype.hide function change it to

    this.$trigger
      .addClass('collapsed')
      .attr('aria-expanded', false)
      .html('Expand')
    

    This will toggle the text between "Collapse" when everything is expanded and "Expand" when everything is collapsed.

    Two lines. Done.

    EDIT: longterm this won't work. bootstrap.js is part of a Nuget package so I don't think it was propogating my change to the server. As mentioned previously, not best practice anyway to edit bootstrap.js, so I implemented PSL's solution which worked great. Nonetheless, my solution will work locally if you need something quick just to try it out.

    0 讨论(0)
  • 2020-12-13 00:04

    Here's another CSS only solution that works with any HTML layout.

    It works with any element you need to switch. Whatever your toggle layout is you just put it inside a couple of elements with the if-collapsed and if-not-collapsed classes inside the toggle element.

    The only catch is that you have to make sure you put the desired initial state of the toggle. If it's initially closed, then put a collapsed class on the toggle.

    It also requires the :not selector, so it doesn't work on IE8.

    HTML example:

    <a class="btn btn-primary collapsed" data-toggle="collapse" href="#collapseExample">
      <!--You can put any valid html inside these!-->
      <span class="if-collapsed">Open</span>
      <span class="if-not-collapsed">Close</span>
    </a>
    <div class="collapse" id="collapseExample">
      <div class="well">
        ...
      </div>
    </div>
    

    Less version:

    [data-toggle="collapse"] {
        &.collapsed .if-not-collapsed {
             display: none;
        }
        &:not(.collapsed) .if-collapsed {
             display: none;
        }
    }
    

    CSS version:

    [data-toggle="collapse"].collapsed .if-not-collapsed {
      display: none;
    }
    [data-toggle="collapse"]:not(.collapsed) .if-collapsed {
      display: none;
    }
    

    JS Fiddle

    0 讨论(0)
  • 2020-12-13 00:05

    I liked the CSS-only solution from PSL, but in my case I needed to include some HTML in the button, and the content CSS property is showing the raw HTML with tags in this case.

    In case that could help someone else, I've forked his fiddle to cover my use case: http://jsfiddle.net/brunoalla/99j11h40/2/

    HTML:

    <div class="row-fluid summary">
        <div class="span11">
            <h2>MyHeading</h2>  
        </div>
        <div class="span1">
            <button class="btn btn-success collapsed" data-toggle="collapse" data-target="#intro">
                <span class="show-ctrl">
                    <i class="fa fa-chevron-down"></i> Expand
                </span>
                <span class="hide-ctrl">
                    <i class="fa fa-chevron-up"></i> Collapse
                </span>            
            </button>
        </div>
    </div>
    <div class="row-fluid summary">
        <div id="intro" class="collapse"> 
            Here comes the text...
        </div>
    </div>
    

    CSS:

    button.btn .show-ctrl{
        display: none;
    }
    button.btn .hide-ctrl{
        display: block;
    }
    button.btn.collapsed .show-ctrl{
        display: block;
    }
    button.btn.collapsed .hide-ctrl{
        display: none;
    }
    
    0 讨论(0)
  • 2020-12-13 00:09

    Easier with inline coding

    <button type="button" ng-click="showmore = (showmore !=null && showmore) ? false : true;" class="btn float-right" data-toggle="collapse" data-target="#moreoptions">
                <span class="glyphicon" ng-class="showmore ? 'glyphicon-collapse-up': 'glyphicon-collapse-down'"></span>
                {{ showmore !=null && showmore ? "Hide More Options" : "Show More Options" }}
            </button>
    
    
    <div id="moreoptions" class="collapse">Your Panel</div>
    
    0 讨论(0)
  • 2020-12-13 00:13

    My following JS solution is better than the other approaches here because it ensures that it will always say 'open' when the target is closed, and vice versa.

    HTML:

    <a href="#collapseExample" class="btn btn-primary" data-toggle="collapse" data-toggle-secondary="Close">
        Open
    </a>
    <div class="collapse" id="collapseExample">
      <div class="well">
        ...
      </div>
    </div>
    

    JS:

    $('[data-toggle-secondary]').each(function() {
        var $toggle = $(this);
        var originalText = $toggle.text();
        var secondaryText = $toggle.data('toggle-secondary');
        var $target = $($toggle.attr('href'));
    
        $target.on('show.bs.collapse hide.bs.collapse', function() {
            if ($toggle.text() == originalText) {
                $toggle.text(secondaryText);
            } else {
                $toggle.text(originalText);
            }
        });
    });
    

    Examples:

    $('[data-toggle-secondary]').each(function() {
        var $toggle = $(this);
        var originalText = $toggle.text();
        var secondaryText = $toggle.data('toggle-secondary');
        var $target = $($toggle.attr('href'));
    
        $target.on('show.bs.collapse hide.bs.collapse', function() {
            if ($toggle.text() == originalText) {
                $toggle.text(secondaryText);
            } else {
                $toggle.text(originalText);
            }
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"/>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
    
    <a href="#collapseExample" class="btn btn-primary" data-toggle="collapse" data-toggle-secondary="Close">
        Open
    </a>
    <div class="collapse" id="collapseExample">
      <div class="well">
        ...
      </div>
    </div>

    JS Fiddle

    Other benefits of this approach:

    • the code is DRY and reusable
    • each collapse button stays separate
    • you only need to put one change into the HTML: adding the data-toggle-secondary attribute
    0 讨论(0)
  • 2020-12-13 00:18

    I guess you could look inside your downloaded code where exactly there is a + sign (but this might not be very easy).

    What I'd do? I'd find the class/id of the DOM elements that contain the + sign (suppose it's ".collapsible", and with Javascript (actually jQuery):

    <script>
         $(document).ready(function() {
             var content=$(".collapsible").html().replace("+", "-");
             $(".collapsible").html(content));
         });
    </script>
    

    edit Alright... Sorry I haven't looked at the bootstrap code... but I guess it works with something like slideToggle, or slideDown and slideUp... Imagine it's a slideToggle for the elements of class .collapsible, which reveal contents of some .info elements. Then:

             $(".collapsible").click(function() { 
                 var content=$(".collapsible").html();
                 if $(this).next().css("display") === "none") { 
                     $(".collapsible").html(content.replace("+", "-"));
                 }
                 else $(".collapsible").html(content.replace("-", "+"));
             });
    

    This seems like the opposite thing to do, but since the actual animation runs in parallel, you will check css before animation, and that's why you need to check if it's visible (which will mean it will be hidden once the animation is complete) and then set the corresponding + or -.

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