简体   繁体   中英

AngularJS and jquery to collapse / expand multiple bootstrap panels

I have multiple panels that can be opened and closed (expanded and collapsed), when you click on the panel header. I also have a button that can open and close all panels. It kind of works, but not 100%.

If you click on the header of one of the three panels, then the button to open and close all panels. Then two of the panels will open, and one will close. I would like it to close all panels or open all panels.

Also, sometimes the panel close, but the content of the panel is still visible.

Also, if you click on the header of the panel twice, opening and closing the panel, then the button to open and close all panels won't affect it anymore... Lots of bugs..

Made a fiddle to illustrate.

https://jsfiddle.net/fiddlejan/qz30pvjk/

HTML:

<body ng-app="app">
  <div ng-controller="controller as c" style="padding:10px;">

    <button class="btn" ng-click="add()" style="margin-bottom:5px;">
      add panel
    </button>
    <button class="btn" ng-click="collapseAllPanels()" style="margin-bottom:5px;">
      expand / collapse all panels
    </button>

    <!-- childeren -->
    <div class="panel-group">
      <div class="panel panel-info fadein fadeout " ng-repeat="p in panels">
        <div class="panel-heading" data-toggle="collapse" data-target="#test_{{p}}" style="cursor:pointer">
          <h4 class="panel-title">
                 open / close - {{p}}
                    <span class="glyphicon glyphicon-remove topright" ng-click="close(p, $event)"></span>
                </h4>
        </div>

        <div id="test_{{p}}" class="panel-collapse collapse node-panel" aria-expanded="false">
          <div class="panel-body">
            hello {{p}}
          </div>
        </div>
      </div>
    </div>
  </div>

</body>

The Jquery, triggered by AngularJS

  $scope.collapseAllPanels = function() {

    console.log("collapse / expand all panels");

    $('.node-panel').slideToggle("medium", function() {
      console.log("slide animation complete");
    });

  }

Try this

<div class="panel-group">
    <div class="panel panel-info" ng-repeat="p in panels">
        <div class="panel-heading" style="cursor:pointer" ng-click="openSinglePanel(this)">
            <h4 class="panel-title">
                open / close - {{p}}
                <span class="glyphicon glyphicon-remove topright" ng-click="close(p, $event)"></span>
            </h4>
        </div>

        <div id="test_{{p}}" class="panel-collapse collapse node-panel" aria-expanded="false">
            <div class="panel-body">
                hello {{p}}
            </div>
        </div>
    </div>
</div>

$scope.openSinglePanel = function (panel) {
    $(panel).slideToggle("medium", function() {

    });
}

$scope.collapseAllPanels = function() {
    $.each($('.node-panel'), function (key, panel){
        $(panel).slideToggle("medium", function() {
            //console.log("slide animation complete");
        });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM