Expand many accordion groups at once

岁酱吖の 提交于 2019-12-25 11:54:57

问题


I have the following accordion (using angular-ui-bootstrap) inside a paginated loop of elements:

<div data-ng-repeat="m in results">
    <div class="stuff_in_the_middle">
        <accordion id="accordion_{{$index+((currentPage-1)*20)+1}}"  close-others="false">
            <accordion-group>
                [...stuff...]
            </accordion-group>
            <accordion-group>
                [...stuff...]
            </accordion-group>
            <accordion-group>
                [...stuff...]
            </accordion-group>
        </accordion>
        <span id="toggle_{{$index+((currentPage-1)*20)+1}}" onClick="openAllGroups">Toggle groups</span>
    </div>
</div>

I would like to know how to expand all accordion-group elements at once with one click of the Toggle link. Is it possible?


回答1:


You could create your own collapseall directive on the accordion-groups. In this directive you can set the isOpen scope variable (created by angular-ui) to the value from your parent controller and your toggle all button.

EDIT: working demo here (http://plnkr.co/edit/JOOZek2QBSmxIj2pXCkK?p=preview)

js

.controller('MyCtrl', ['$scope', function($scope) {
    $scope.opened = false;
}])

.directive('collapseall', [function() {
    return {
      restrict: 'A',
      scope: {
        collapseall: '='
      },
      link: function(scope, elem, attrs) {
        scope.$watch('collapseall', function(newval, oldval) {
          scope.isOpen = newval;
        })
      }
    }
  }
])

html

<div>
    <accordion close-others="false">
        <accordion-group heading="Item 001" collapseall="opened"> 
        </accordion-group>
        <accordion-group heading="Item 002" collapseall="opened">
        </accordion-group>
        <accordion-group heading="Item 003" collapseall="opened">
        </accordion-group>
    </accordion>
    <button class="btn" ng-click="opened=!opened">Toggle groups</button>
</div>


来源:https://stackoverflow.com/questions/19237993/expand-many-accordion-groups-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!