简化后的主要代码:
$scope.groups = []; for (var i = 0; i < 2; i++) { $scope.groups[i] = { name: i, items: [] }; for (var j = 0; j < 3; j++) { var item = { name: i + '-' + j, items: [] }; for (var k = 0; k < 3; k++) { item.items.push({ name: 'kkk' + k }); } $scope.groups[i].items.push(item); } } $scope.toggleGroup = function (group) { if ($scope.isGroupShown(group)) { $scope.shownGroup = null; $scope.shown2 = null; } else { $scope.shownGroup = group; } }; $scope.isGroupShown = function (group) { return $scope.shownGroup === group; }; $scope.isGroupShown2 = function (group) { return $scope.shown2 === group; }; $scope.max = function (group) { if ($scope.isGroupShown2(group)) { $scope.shown2 = null; } else { $scope.shown2 = group; } };
<ion-list> <div ng-repeat="group in groups"> <ion-item class="item-stable item-icon-right" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}"> <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i> Group {{group.name}} </ion-item> <div ng-repeat="item in group.items"> <ion-item class="item-accordion item-icon-right" ng-if="isGroupShown(group)" ng-click="max(item)" style="padding-left: 50px;"> <i class="icon" ng-class="isGroupShown2(item) ? 'ion-minus' : 'ion-plus'"></i> {{item.name}} </ion-item> <ion-item class="item-accordion" ng-if="isGroupShown2(item)" style="padding-left: 80px;"> <div ng-repeat="subitem in item.items"> {{subitem.name}} </div> </ion-item> </div> </div> </ion-list>
本文转自:http://codepen.io/morsiki/pen/KdMRgN?editors=101
<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 Accordion</title> <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet"> <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script> </head> <body ng-controller="MyCtrl"> <ion-header-bar class="bar-positive"> <h1 class="title">Accordion 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> Group {{group.name}} </ion-item> <div ng-repeat="item in group.items"> <ion-item class="item-accordion" ng-if="isGroupShown(group)" ng-click="max(item)" style="padding-left: 50px;"> <i class="icon" ng-class="isGroupShown2(item) ? 'ion-minus' : 'ion-plus'"></i> {{item.subId}} </ion-item> <ion-item class="item-accordion" ng-if="isGroupShown2(item)" style="padding-left: 80px;"> Example1<br> Example2 </ion-item> </div> </div> </ion-list> </ion-content> </body> </html>
angular.module('ionicApp', ['ionic']) .controller('MyCtrl', function($scope) { $scope.groups = []; for (var i=0; i<10; i++) { $scope.groups[i] = { name: i, items: [] }; 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) { if ($scope.isGroupShown(group)) { $scope.shownGroup = null; } else { $scope.shownGroup = group; } }; $scope.isGroupShown = function(group) { return $scope.shownGroup === group; }; });
body { cursor: url('http://ionicframework.com/img/finger.png'), auto; } /* * http://docs.angularjs.org/api/ng/directive/ngShow#usage_animations */ .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; }