Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

controlling isOpen in angular ui bootstrap

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;
      }
    }

  }
like image 447
jhlosin Avatar asked Dec 09 '15 23:12

jhlosin


1 Answers

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.

like image 162
Rob J Avatar answered Oct 20 '22 18:10

Rob J