htoniv
htoniv

Reputation: 1668

Angular bootstrap ui accordion group "open one at a time" not working

I have been using Angular ui-bootstrap. In here oneAtATime is not working even though the value is set to true. Here is my code.

<div ng-repeat="group in groups track by group.key">
  <uib-accordion close-others="oneAtATime">
    <uib-accordion-group>
      <uib-accordion-heading >
        <div>
          {{ group.title }}
        <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
        </div>
      </uib-accordion-heading>
    </uib-accordion-group>
  </uib-accordion>
  </div>

plnkr link.

Upvotes: 1

Views: 2061

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25817

Your HTML structure is wrong. There should be only single uib-accordion element and multiple uib-accordion-group element. So simply change your code like this:

<uib-accordion close-others="oneAtATime">
    <uib-accordion-group ng-repeat="group in groups track by group.key">
        <uib-accordion-heading>
            <div>
                {{ group.title }}
                <i class="pull-right glyphicon"
                   ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
            </div>
        </uib-accordion-heading>
    </uib-accordion-group>
</uib-accordion>

What I did is simply moved your ng-repeat expression in uib-accordion-group element.

See the working example below:

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function($scope) {
  $scope.oneAtATime = true;

  $scope.groups = [{
    title: 'Dynamic Group Header - 1',
    content: 'Dynamic Group Body - 1',
    key: 1
  }, {
    title: 'Dynamic Group Header - 2',
    content: 'Dynamic Group Body - 2',
    key: 2
  }];

  $scope.items = ['Item 1', 'Item 2', 'Item 3'];

  $scope.addItem = function() {
    var newItemNo = $scope.items.length + 1;
    $scope.items.push('Item ' + newItemNo);
  };

  $scope.status = {
    isCustomHeaderOpen: false,
    isFirstOpen: true,
    open: true,
    isFirstDisabled: false
  };
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.js"></script>
  <script src="script.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

  <div ng-controller="AccordionDemoCtrl">
    <div class="checkbox">
      <label>
        <input type="checkbox" ng-model="oneAtATime">Open only one at a time
      </label>
    </div>
    <uib-accordion close-others="oneAtATime">
      <uib-accordion-group ng-repeat="group in groups track by group.key">
        <uib-accordion-heading>
          <div>
            {{ group.title }}
            <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
          </div>
        </uib-accordion-heading>
      </uib-accordion-group>
    </uib-accordion>
  </div>
</body>

</html>

Upvotes: 2

Related Questions