5

I would like use isOpen attributes in angualr ui bootstrap accordion directive so it would open the first element of the first ng-repeat in the accordion. I have tried few things with no luck. can anyone advise on this?

    //html
          <div ng-show="accordionCtrl.isNotString(value);" class="accordionContainer" ng-repeat="(key, value) in accordionCtrl.lessons">
      <div class="accordionStepBox">
        <h4 class="accordionStepTitle">Step {{$index+1}}: {{value.title}}</h4>
        <span>Summary: {{value.summary}}</span>
      </div>
      <div class="accordionCoursesBox">
        <div class="accordionCoursesText">Courses</div>

        <!-- accordion for suffles -->
        <uib-accordion close-others="accordionCtrl.oneAtATime">
          <!-- only give accordion to object vals -->
          <div class="accordion" ng-show="accordionCtrl.isNotString(value);" ng-repeat="(key, value) in value">
            <!-- <uib-accordion-group heading="{{value.title}}" is-open="accordionCtrl.firstIndex($index)"> -->
            <uib-accordion-group heading="{{value.title}}">
              <div ng-repeat="(key, value) in value">
                <div ng-show="accordionCtrl.isNotString(value);" class="accordionSuffleBox">
                  {{$index+1}}. {{value.title}} 
                </div>
              </div>
              <br/>
              <button ui-sref="lesson" class="btn btn-default accordionbutton">Start</button>
            </uib-accordion-group>
          </div>
        </uib-accordion>
      </div>
    </div>

    //controller
    angular
    .module('neuralquestApp')
    .controller('AccordionCtrl', AccordionCtrl);


  function AccordionCtrl(FirebaseUrl, $firebaseObject, $firebaseArray) {
    var accordionCtrl = this;
    var getLessons = getLessons;

    accordionCtrl.oneAtATime = true;

    accordionCtrl.init = init;
    accordionCtrl.init();
    accordionCtrl.isNotString = isNotString;
    accordionCtrl.firstIndex = firstIndex;

    /*======================================
    =            IMPLEMENTATION            =
    ======================================*/

    function init() {
      getLessons();
    }

    function firstIndex(index) {
      if(index === 0){
        return true;
      } else {
        return false;
      }
    }

    function getLessons() {
      var ref = new Firebase(FirebaseUrl);
      accordionCtrl.lessons = $firebaseObject(ref.child('NeuralNetwork'));
    }

    function isNotString(val) {
      // console.log('val', val);
      if(typeof val === "string"){
        console.log('is string', val);
        return false;
      } else {
        return true;
      }
    }

  }
4
  • Your HTML doesn't look valid. Specifically, you're missing a closing tag for the first <div> with the ng-repeat. Also, make sure you write self-closing tags like this: <br/>. Commented Dec 9, 2015 at 23:50
  • oh. code has been updated. Commented Dec 10, 2015 at 0:03
  • Still doesn't look right. The <uib-accordion-group> with the is-open attribute is commented out. Also, it's very bad practice to re-use the same variable names for different things (key, value), and names should also be descriptive. You'll easily get confused about which key or value is being used. Commented Dec 10, 2015 at 0:09
  • Thanks for the input. I was playing with is-open attritbutes but did not work well. so I have multiple uib-accordion-group. I agree on not using the same key,value and I am gonna change it. Commented Dec 10, 2015 at 0:19

1 Answer 1

5

The is-open attribute is setup for 2 way binding with the controller, so you could do something like so:

<div ng-controller="AccordionDemoCtrl">
  <uib-accordion>
    <div ng-repeat="group in groups">
      <uib-accordion-group heading="{{group.title}}" is-open="openIndex[$index]">
        {{group.content}}
      </uib-accordion-group>
    </div>
  </uib-accordion>
</div>

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

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

Example plunk. Also, the close-others attribute default value is true, so you don't need to explicitly set that to true.

Sign up to request clarification or add additional context in comments.

Comments

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.