Accordion: allow automatically open first or selected element
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">
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;
}
};
}
};
})
Works with existing accordion:
<accordion-group ng-init="isOpen = true" is-open="isOpen">
Code given by John is not working for me. All the groups will b eopen.
The below code is working :)
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">
Thanks @CarlosCuevas I had the same question (first panel to be open by default) and the solution suggested by you worked.
thejohnfreeman solution works for my problem!
this finally worked for me : < accordion-group ng-init="status.open = true" is-open="status.open" >
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.
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">
What worked for on the first try is to simply add is-open="$first" on the accordion-group element.
the @thejohnfreeman solution works so long as you properly replace the variables
Thank you
is-open="$first" will do the job
<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