0

I want to loop over an array of menus and submenus(maybe sub of submenus) .

I use metronic template (look the second-last item of this link Metronic Template ("Multi Level Menu")

I have this structure :

$scope.dashitems = [{
    title: 'Company',
    icon: 'icon-layers',
    href: '#/dashboard1',
    isActive: path === '/dashboard1'
}, {
    title: 'Buildings',
    icon: 'icon-layers',
    href: '#/Buildings',
    isActive: path === '/Buildings'
}, {
    title: 'Floors',
    icon: 'icon-layers',
    href: '#/Floors',
    isActive: path === '/Floors'
}, {
    title: 'Spaces',
    icon: 'icon-layers',
    href: 'javascript:;',
    isActive: path === '/Spaces',
    subitems: [{
        title: 'OpenSpaces',
        icon: 'icon-layers',
        href: '#/OpenSpaces',
        isActive: path === '/OpenSpaces',
        subitems: [{
            title: 'OpenSpaces2',
            icon: 'icon-layers',
            href: '#/OpenSpaces2',
            isActive: path === '/OpenSpaces2',
        }]
    }, ]
}, {
    title: 'Meeting',
    icon: 'icon-layers',
    href: '#/meeting',
    isActive: path === '/meeting'
}];

this not work :

function printList(dashitems){
            $scope.menu = '<ul>';
            angular.forEach(dashitems, function(value, key) {
                $scope.menu+="<li>";
                if(value.hasOwnProperty('subitems')){


                  $scope.menu=' <a ng-href="{{ value.href }}" class="nav-link nav-toggle">'+
                              '<i ng-class="value.icon"></i>'+
                              '<span class="title">{{ value.title }}</span>'+
                              '<span class="arrow open"></span>'+
                          '</a>';


                  printList(value.subitems);
                }else{

                   $scope.menu+="<a href='javascript:;' class='nav-link nav-toggle'>"+
                      "<i class='value.icon'></i>"+
                      "<span class='title'>{{value.title}}</span>"+
                  "</a></li>";
                }
            });
            $scope.menu += "<ul>";
            return $scope.menu;
        }

How to loop over this structure and produce the same html of the "Multi Level Menu" ?

EDIT:

angular
  .module('app').directive('menuBar', function() {
    return {
      restrict: 'E',
      controller: ['$scope', '$location', function($scope, $location) {
        //function & dashitems
            $scope.printList($scope.dashitems);
      }]
    }   
});

2 Answers 2

1

You can create a directive that make lists recursively.

<menu ng-model="dashItems"></menu>

The directive should do somethink like:

  1. create a funcion printList(dashItems)
  2. open a ul element
  3. Iterate dashItems, for each item generates a li element
  4. If an item hasOwnProperty('subItem'), you call printList(dashItems.subitem) recursively.
  5. Finally close the ul element and return list.

Now you only have to do: element.append(printList(dashItems))

That's a high level approach but I think it could be usefull.

Edited: I'm going to help you to create the function:

function printList(dashitems){
            $scope.menu = '<ul>';
            angular.forEach(dashitems, function(value, key) {
                $scope.menu+="<li>";
                if(value.hasOwnProperty('subitems')){

                  $scope.menu=' <a ng-href="{{ value.href }}" class="nav-link nav-toggle">'+
                              '<i ng-class="value.icon"></i>'+
                              '<span class="title">{{ value.title }}</span>'+
                              '<span class="arrow open"></span>'+
                          '</a>';


                  printList(value.subitems);
                }else{

                   $scope.menu+="<a href='javascript:;' class='nav-link nav-toggle'>"+
                      "<i class='value.icon'></i>"+
                      "<span class='title'>{{value.title}}</span>"+
                  "</a>";
                }
                $scope.menu+="</li>";
            });
            $scope.menu += "<ul>";
            return $scope.menu;
        }

I think that could work

angular
  .module('app').directive('menuBar', function() {
    return {
      restrict: 'E',
      scope: {
          list: '=dashitems'
      }
      controller: ['$scope', '$location', function($scope, $location) {
        //function & dashitems
            $scope.printList($scope.dashitems);
      }]
    }   
});
Sign up to request clarification or add additional context in comments.

6 Comments

i will create the directive but first i want to display the correct menu with ng-repeat, look the question!
You can't use ng-repeat, it's not recursively
look the question, I tried to create the print function List! Any suggestion?
and how i can associate that function into a directive? thank you
scope.printList = function (dashitems) {... } Noticed that's the directive scope
|
0

Look at the source code.. They use Bootstrap to put together a multi-level menu. You'd probably be best served checking out Bootstrap's page. They use classes to arrange and set up a menu. You can do it through Bootstrap or there is a pure AngularJS Bootstrap option in the AngularUI project.

You're going to want to use a directive to iterate through your collection like ng-repeat or something. You can write a custom directive like the other answer suggests, too. I find it all comes down to a matter of preference. I use ng-repeat and nest repeats together to create multi-level items.

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.