bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Accordion: allow automatically open first or selected element

Open rewritten opened this issue 12 years ago • 14 comments

When the accordion is built, one of the groups may be initialized as open. Setting

<accordion-group is-open="true">

doesn't work, because it can't be closed.

Maybe, as a configuration setting (similarly to closeOthers) can help:

  // This is called from the accordion-group directive to add itself to the accordion
  this.addGroup = function(groupScope) {
    var that = this;
    var openFirst;
    if (!this.groups.length) {
      openFirst = angular.isDefined($attrs.openFirst) ? $scope.$eval($attrs.openFirst) : accordionConfig.openFirst;
      if ( openFirst ) {
        groupScope.isOpen = true;
      }
    }
    this.groups.push(groupScope);

    groupScope.$on('$destroy', function (event) {
      that.removeGroup(groupScope);
    });
  };

or an attribute on a specific group, which will set the isOpen value

<accordion-group is-open-at-start="true">

rewritten avatar May 08 '14 20:05 rewritten

For my application, I modified the directive to add a new attribute, "initallyOpen" which is evaluated once.

My html looks something like:

<accordion-group ng-repeat="item in items" initially-open="(item.id == selectedItemId)" is-open="status" >
// The modified accordion directive
.directive('accordionGroup', function() {
  return {
    require:'^accordion',         // We need this directive to be inside an accordion
    restrict:'EA',
    transclude:true,              // It transcludes the contents of the directive into the template
    replace: true,                // The element containing the directive will be replaced with the template
    templateUrl:'template/accordion/accordion-group.html',
    scope: {
      heading: '@',               // Interpolate the heading attribute onto this scope
      isOpen: '=?',
      isDisabled: '=?',
      initiallyOpen: '=?'
    },
    controller: function() {
      this.setHeading = function(element) {
        this.heading = element;
      };
    },
    link: function(scope, element, attrs, accordionCtrl) {
      accordionCtrl.addGroup(scope);

      if(scope.initiallyOpen) {
          scope.isOpen = true;
      }

      scope.$watch('isOpen', function(value) {
        if ( value ) {
          accordionCtrl.closeOthers(scope);
        }
      });

      scope.toggleOpen = function() {
        if ( !scope.isDisabled ) {
          scope.isOpen = !scope.isOpen;
        }
      };
    }
  };
})

netblue avatar May 13 '14 20:05 netblue

Works with existing accordion:

<accordion-group ng-init="isOpen = true" is-open="isOpen">

thejohnfreeman avatar Aug 18 '14 00:08 thejohnfreeman

Code given by John is not working for me. All the groups will b eopen. The below code is working :)

jijogodrej avatar Nov 13 '14 16:11 jijogodrej

I'm not sure if this is what you're looking for but I wanted the first panel to be open by default:

<accordion-group ng-repeat="thing in things" ng-init="status.open = $first" is-open="status.open">

CarlosCuevas avatar Dec 19 '14 18:12 CarlosCuevas

Thanks @CarlosCuevas I had the same question (first panel to be open by default) and the solution suggested by you worked.

ameckoni avatar Mar 28 '15 22:03 ameckoni

thejohnfreeman solution works for my problem!

pjukalkar avatar Apr 17 '15 20:04 pjukalkar

this finally worked for me : < accordion-group ng-init="status.open = true" is-open="status.open" >

aleksandrj1 avatar May 06 '15 21:05 aleksandrj1

Ahh - But the real problem for me was the ng-controller was spec'd on the accordion. It needs to be on a div that contains the accordion directive. Then: <accordion-group is-open="status.isFirstOpen"> works as expected. Otherwise it just stays open if you hard code a true in there.

aleksandrj1 avatar May 06 '15 21:05 aleksandrj1

if your using ng-repeat this works for me <accordion-group ng-init="isOpen = $index == 0 ? true : false" is-open="isOpen" ng-repeat="item in list">

kevcoxe avatar Jul 17 '15 17:07 kevcoxe

What worked for on the first try is to simply add is-open="$first" on the accordion-group element.

OlafDeBacker avatar Nov 24 '15 08:11 OlafDeBacker

the @thejohnfreeman solution works so long as you properly replace the variables

evfreethy avatar Feb 25 '16 23:02 evfreethy

Thank you

velikodimitrov avatar May 31 '16 03:05 velikodimitrov

is-open="$first" will do the job

virendra656 avatar Oct 21 '16 09:10 virendra656

<accordion-group *ngFor="let product of data.products; let i = index;" [isOpened]='i == 0'>

For new one I have work with this. This code work fine for me in new accordion

chetanbane avatar Jan 24 '18 07:01 chetanbane