creating nested list in the ionic for mobile app

11,807

Solution 1

I don't know who made this code, but it is really great solving you problem.

angular.module('ionicApp', ['ionic'])

.controller('MyCtrl', function($scope) {
  $scope.groups = [];
  for (var i=0; i<10; i++) {
    $scope.groups[i] = {
      name: i,
      items: [],
      show: false
    };
    for (var j=0; j<3; j++) {
      $scope.groups[i].items.push(i + '-' + j);
    }
  }
  
  /*
   * if given group is the selected group, deselect it
   * else, select the given group
   */
  $scope.toggleGroup = function(group) {
    group.show = !group.show;
  };
  $scope.isGroupShown = function(group) {
    return group.show;
  };
  
});
.list .item.item-accordion {
  line-height: 38px;
  padding-top: 0;
  padding-bottom: 0;
  transition: 0.09s all linear;
}
.list .item.item-accordion.ng-hide {
  line-height: 0px;
}
.list .item.item-accordion.ng-hide-add,
.list .item.item-accordion.ng-hide-remove {
  display: block !important;
}
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Ionic collapsible list</title>
   
    <link href="//code.ionicframework.com/1.0.0-beta.12/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/1.0.0-beta.12/js/ionic.bundle.js"></script>
    </script>
  </head>

  <body ng-controller="MyCtrl">
    
    <ion-header-bar class="bar-positive">
      <h1 class="title">Ionic collapsible list</h1>
    </ion-header-bar>

    <ion-content>

      <ion-list>
        <div ng-repeat="group in groups">
          <ion-item class="item-stable"
                    ng-click="toggleGroup(group)"
                    ng-class="{active: isGroupShown(group)}">
              <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
            &nbsp;
            Group {{group.name}}
          </ion-item>
          <ion-item class="item-accordion"
                    ng-repeat="item in group.items"
                    ng-show="isGroupShown(group)">
            {{item}}
          </ion-item>
        </div>
      </ion-list>

    </ion-content>
      
  </body>
</html>

Solution 2

Not sure if it is the recommended way but this worked for me

        <ion-list>
            <ion-item nav-clear menu-close ng-click="toggleSubmenu()">Menu 1</ion-item>
            <ion-item nav-clear menu-close ng-show="submenu">
                <ion-list>
                    <ion-item nav-clear menu-close>Submenu 1</ion-item>
                    <ion-item nav-clear menu-close>Submenu 2</ion-item>
                    <ion-item nav-clear menu-close>Submenu 3</ion-item>
                </ion-list>
            </ion-item>
            <ion-item nav-clear menu-close>Menu 2</ion-item>
            <ion-item nav-clear menu-close>Menu 3</ion-item>
            <ion-item nav-clear menu-close>Menu 4</ion-item>
        </ion-list>

Solution 3

Hope this will be working

HTML Code

<ion-item  ng-click="showSubMenu()">
    Show Submenu            
    <div ng-if="isShowSubMenu">
        <ion-item menu-close  href="#/app/one">
            Submenu Menu One
        </ion-item>
        <ion-item menu-close href="#/app/two">
            Submenu Menu Two
        </ion-item>
        <ion-item menu-close href="#/app/three">
            Submenu Menu Three
        </ion-item>
    </div>
</ion-item>

Write down below function in controller in Controller.Js file

 $scope.showSubMenu = function(){
      $scope.isShowSubMenu=!$scope.isShowSubMenu;
  };

Write down below code in app.js

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    //// ===========  New Start
    .state('app.one', {
        url: '/one',
        views: {
            'menuContent': {
                templateUrl: 'templates/1Menu.html',
                controller: 'FirstCtrl'
            }
        }
    })
    .state('app.two', {
        url: '/two',
        views: {
            'menuContent': {
                templateUrl: 'templates/2Menu.html',
                controller: 'SecondCtrl'
            }
        }
    })

    .state('app.three', {
        url: '/three',
        views: {
            'menuContent': {
                templateUrl: 'templates/3Menu.html',
                controller: 'ThirdCtrl'
            }
        }
    })
});

Create controllers in controller.js file

.controller('FirstCtrl', function($scope){
    $scope.pageLable='One';
})

.controller('SecondCtrl', function($scope){
    $scope.pageLable='Two';
})

.controller('ThirdCtrl', function($scope){
    $scope.pageLable='Three';
}) 

HTML Page 1Menu.html

<ion-view view-title="One">
  <ion-content>
    <h1>{{pageLable}}</h1>
  </ion-content>
</ion-view>

Solution 4

You can use the ionic-starter-tabs which illustrates the navigation you are describing. This is a running example from the same source: http://plnkr.co/edit/qYMCrt?p=preview

This behavior is achieved by ui-router, already included with Ionic:

 .state('tab.friend-detail', {
  url: '/friend/:friendId',
  views: {
    'tab-friends': {
      templateUrl: 'friend-detail.html',
      controller: 'FriendDetailCtrl'
    }
  }
})
Share:
11,807
Vipul
Author by

Vipul

Updated on June 26, 2022

Comments

  • Vipul
    Vipul almost 2 years

    I am developing the mobile app. I want to create a nested list. Can anyone help me out. For e.g. On my first page list is: Food, Beverages. Clicking on food I want to display L1,L2,L3 etc.

    How can I achieve that?