1

I'm trying to have a simple accordion with two buttons at the top: one button that opens every panel and one that collapses them.

I'm trying to use jQuery's prop method to alter the is-open property on each uib-accordion element, but nothing happens when I click on either button.

<div ng-controller="AccordionController">
    <p>
        <button type="button" class="btn btn-default btn-sm" ng-click="openAll()">Open All</button>
        <button type="button" class="btn btn-default btn-sm" ng-click="collapseAll()">Collapse All</button>
    </p>
    <uib-accordion close-others="false">
        <uib-accordion-group class="accordionGroup" ng-repeat="xmlResult in xmlResults" is-open="false">
            <uib-accordion-heading>
                //heading...
            </uib-accordion-heading>
            //content...
        </uib-accordion-group>
    </uib-accordion>
</div>

Here is my controller

angular.module("ResultsTreeViewer").controller("AccordionController", function ($scope) {

    $scope.openAll = function () {
        $(".accordionGroup").prop("is-open", true);
    };
    $scope.collapseAll = function () {
        $(".accordionGroup").prop("is-open", false);
    }
});

I'd love an Angular only solution but that isn't necessary. I just want it to work.

4
  • is-open is an attribute and not a property. Commented Mar 10, 2016 at 18:23
  • @Stryner Using attr doesn't work either. What do you suggest I use? Commented Mar 10, 2016 at 18:30
  • When inspecting the element, it works for me. Commented Mar 10, 2016 at 18:34
  • @Stryner I see that it updated the is-open attribute, but it's not opening or closing any of the accordions. I suppose the answer to that is beyond the scope of this question. Commented Mar 10, 2016 at 19:11

1 Answer 1

1

I'll say that don't use jQuery with Angular, inside controller.

Rather have isOpen variable inside each accordion record and then make it true & false by calling single function actionAccordion function(Make sense to have it in angular way).

Markup

<p>
    <button type="button" class="btn btn-default btn-sm" ng-click="actionAccordion(true)">Open All</button>
    <button type="button" class="btn btn-default btn-sm" ng-click="actionAccordion(false)">Collapse All</button>
</p>
<uib-accordion close-others="false">
    <uib-accordion-group class="accordionGroup" ng-repeat="xmlResult in xmlResults" 
      is-open="xmlResult.isOpen">
        <uib-accordion-heading>
            //heading...
        </uib-accordion-heading>
        //content...
    </uib-accordion-group>
</uib-accordion>

Code

$scope.actionAccordion = function (flag) {
    angular.forEach($scope.xmlResults, function(xmlResult){
        xmlResults.isOpen = flag;
    })
};
Sign up to request clarification or add additional context in comments.

2 Comments

This is as correct as an answer can get for my question because the is-open property can only accept an angular object, not a string literal.
@LCIII yupe.. internally accordion directive has isOpen variable with it... it toggle whenever he wanted to show & hide(this toggling process can happen from both side, because accordion binded with isOpen: '=')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.