0

How do I create a multiple-level (embedded) jqueryui accordian using Angular?

The layout should have collable sections with this hierarchy:

  1. Level 1
    1. Lesson 1
    2. Lesson 2
    3. Lesson 3
  2. Level 2
    1. Lesson 1
    2. Lesson 2
    3. Lesson 3

1 Answer 1

1

Here is my solution.

HTML

        <div ng-controller="startLessonController">
        <div id="accordianLevel">
            <div ng-repeat="level in course.levels" >
              <h2> {{level.number}} - {{level.name}}: {{level.title}}</h2>
                <div >
                    {{level.description}}
                    <div id="accordianLesson">
                        <div ng-repeat="lesson in level.lessons" >
                          <h3> {{lesson.number}} - {{lesson.name}}: {{lesson.title}}</h3>
                             <ul>
                                <li ng-repeat="goal in lesson.goals">{{goal}}</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

Here is the Angular js with embedded data for testing (truncated)

var startLessonApp = angular.module('startLessonApp', []);
startLessonApp.controller('startLessonController', ['$scope', function($scope) {
$scope.course = {
"levels": [
 {
  "number": 1,
  "name": "Level One",
  "title": "Conversation Starters",
  "lessons": [
    {
      "number": 1,
      "name": "Lesson One"
    }
  ]
 }
};

Here is the js to initialize the accordian. Note the override to explicitly identify the accordian header elements (h2 for Level, and h3 for Lesson)

(function () {
  "use strict";
  $(document).ready(function () {
    $( "#accordianLevel" ).accordion({
        header: "h2",
        collapsible: true,
        active:false,
        heightStyle:"content"
    });
            $( "#accordianLesson" ).accordion({
        collapsible: true,
        header: "h3",
        active:false,
        heightStyle:"content"
    });
  });
}());

Result on page

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

1 Comment

You missed ]}; for $scope.course-Array. But didn't work for me! Maybe in newer Versions of jquery-ui there doesn't have to be an <div> in Front of the header-tag of the accordion (<h2>) ?!

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.