console
function mapForMenu(source) {
var child = source;
var parent = [];
var temp = [];
var result = [];
for (var i = 0; i < child.length; i++) {
if (child[i].pid === '10') {
parent.push(child[i]);
result.push(child[i]);
child.splice(i, 1);
i--;
}
}
result.sort(function(a, b) {
var aID = parseInt(a.id);
var bID = parseInt(b.id);
if (aID < bID) return - 1;
if (aID > bID) return 1;
return 0;
});
var currentChild = null;
var currentParent = null;
var found = false;
while (child.length) {
found = false;
for (var i = 0; i < child.length; i++) {
currentChild = child[i];
for (var j = 0; j < parent.length; j++) {
currentParent = parent[j];
if (currentChild.pid === currentParent.id) {
found = true;
if (!currentParent.subMenu) currentParent.subMenu = [];
currentParent.subMenu.push(currentChild);
child.splice(i, 1);
i--;
break;
}
}
}
if (!found) break;
temp = [];
for (var i = 0; i < parent.length; i++) {
currentParent = parent[i];
if (currentParent.subMenu) {
currentParent.subMenu.sort(function(a, b) {
var aID = parseInt(a.id);
var bID = parseInt(b.id);
if (aID < bID) return - 1;
if (aID > bID) return 1;
return 0;
});
for (var j = 0; j < currentParent.subMenu.length; j++) {
currentChild = currentParent.subMenu[j];
temp.push(currentChild);
}
}
}
parent = temp;
}
return result;
}
alert('tt');
var app = angular.module('myApp', []);
app.controller('RoleAuthCtrl',
function($scope, $state, $stateParams, $http, $log) {
var self = this;
$scope.ctrl.role = {};
$scope.requestID = $stateParams.ID;
$http.post("http://120.25.80.158:81/js/roleAuth.json").then(function(resp) {
if (resp.data.code === 'CD000001') {
$scope.data = resp.data.body.resultList;
$scope.list = mapForMenu(angular.copy($scope.data));
$log.info($scope.list);
} else {
$log.error(resp);
}
},
function(error) {
$log.error(error);
});
$scope.ctrl.role.menuIdArr = [];
$scope.result = ['profit', 'profit.deal'];
$scope.isSelected = function(menuId) {
return $scope.result.indexOf(menuId) !== -1;
};
$scope.selectAll = function() {
if ($scope.select_all) {
$scope.ctrl.role.menuIdArr = [];
angular.forEach($scope.list,
function(roleOne) {
roleOne.checked = true;
$scope.ctrl.role.menuIdArr.push(roleOne.menuId);
angular.forEach(roleOne.subMenu,
function(roleTwo) {
roleTwo.checked = true;
$scope.ctrl.role.menuIdArr.push(roleTwo.menuId);
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = true;
$scope.ctrl.role.menuIdArr.push(roleThree.menuId);
})
})
})
} else {
angular.forEach($scope.list,
function(roleOne) {
roleOne.checked = false;
angular.forEach(roleOne.subMenu,
function(roleTwo) {
roleTwo.checked = false;
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = false;
})
});
$scope.ctrl.role.menuIdArr = [];
})
}
console.log($scope.ctrl.role.menuIdArr);
};
$scope.selectOne = function($event, $index, menuId) {
angular.forEach($scope.list,
function(roleOne) {
var index = $scope.ctrl.role.menuIdArr.indexOf(roleOne.menuId);
if (roleOne.checked && index === -1) {
$scope.ctrl.role.menuIdArr.push(roleOne.menuId);
angular.forEach(roleOne.subMenu,
function(roleTwo) {
roleTwo.checked = true;
$scope.ctrl.role.menuIdArr.push(roleTwo.menuId);
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = true;
$scope.ctrl.role.menuIdArr.push(roleThree.menuId);
})
})
} else if (!roleOne.checked && index !== -1) {
$scope.ctrl.role.menuIdArr.splice(index, 1);
angular.forEach(roleOne.subMenu,
function(roleTwo) {
roleTwo.checked = false;
$scope.ctrl.role.menuIdArr.splice(roleTwo.menuId, 1);
if (roleTwo.subMenu !== null) {
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = false;
$scope.ctrl.role.menuIdArr.splice(roleThree.menuId, 1);
})
}
});
};
})
if ($scope.data.length === $scope.ctrl.role.menuIdArr.length) {
$scope.select_all = true;
} else {
$scope.select_all = false;
}
console.log($scope.ctrl.role.menuIdArr);
};
$scope.selectTwo = function($event, $index, roleOne, menuId) {
angular.forEach(roleOne.subMenu,
function(roleTwo) {
var index = $scope.ctrl.role.menuIdArr.indexOf(roleTwo.menuId);
if (roleTwo.checked && index === -1) {
$scope.ctrl.role.menuIdArr.push(roleTwo.menuId);
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = true;
$scope.ctrl.role.menuIdArr.push(roleThree.menuId);
})
} else if (!roleTwo.checked && index !== -1) {
$scope.ctrl.role.menuIdArr.splice(index, 1);
angular.forEach(roleTwo.subMenu,
function(roleThree) {
roleThree.checked = false;
$scope.ctrl.role.menuIdArr.splice(roleThree.menuId, 1);
})
}
});
$log.info(roleOne.subMenu.length);
if ($scope.data.length === $scope.ctrl.role.menuIdArr.length) {
$scope.select_all = true;
} else {
$scope.select_all = false;
}
console.log($scope.ctrl.role.menuIdArr);
};
$scope.selectThree = function($event, $index, roleTwo, menuId) {
angular.forEach(roleTwo.subMenu,
function(roleThree) {
var index = $scope.ctrl.role.menuIdArr.indexOf(roleThree.menuId);
if (roleThree.checked && index === -1) {
$scope.ctrl.role.menuIdArr.push(roleThree.menuId);
} else if (!roleThree.checked && index !== -1) {
$scope.ctrl.role.menuIdArr.splice(index, 1);
}
});
$log.info(roleTwo.subMenu.length);
if (roleTwo.subMenu.length === '') {
$scope.select_all = true;
} else {
$scope.select_all = false;
}
if ($scope.data.length === $scope.ctrl.role.menuIdArr.length) {
$scope.select_all = true;
} else {
$scope.select_all = false;
}
console.log($scope.ctrl.role.menuIdArr);
};
self.submit = function() {
$log.log(self.role);
}
});
<div ng-app="myApp">
<div class="row wrapper brole-bottom white-bg page-heading">
<div class="col-lg-10">
<h2>
权限设置
</h2>
<ol class="breadcrumb">
<li>
<a ui-sref="index.main">
首页
</a>
</li>
<li>
<a ui-sref="role.list">
权限管理
</a>
</li>
<li class="active">
<strong>
编辑权限
</strong>
</li>
</ol>
</div>
<div class="col-lg-2">
</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight" ng-controller="RoleAuthCtrl as ctrl">
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>
修改权限
<small>
请选择权限
</small>
</h5>
</div>
<div class="ibox-content">
<form role="form" class="form-horizontal" novalidate name="role_auth_form"
ng-submit="ctrl.submit()" name="roleAuthForm">
<input type="hidden" name="userLevel" value="{{userLevel}}" ng-model="ctrl.role.userLevel"
ng-init="ctrl.role.userLevel=userLevel" />
<input type="hidden" name="userID" value="{{userID}}" ng-model="ctrl.role.userID"
ng-init="ctrl.role.userID=userID" />
<input type="hidden" name="requestID" value="{{requestID}}" ng-model="ctrl.role.requestID"
ng-init="ctrl.role.requestID=requestID" />
<input type="hidden" name="roleID" value="{{roleID}}" ng-model="ctrl.role.roleID"
/>
<div class="row">
<div class="col-lg-6">
<label for="flag">
全选
<input id="flag" type="checkbox" ng-model="select_all" ng-change="selectAll()">
</label>
<ul>
<li ng-repeat="roleOne in list">
<input type=" " id="{{roleOne.menuId}}" value="{{roleOne.menuId}}" ng-model="roleOne.checked"
ng-change="selectOne($event,$index,roleOne.menuId)" ng-checked="isSelected(roleOne.menuId)">
<label for="{{roleOne.menuId}}">
{{roleOne.pageTitle}}
</label>
<ul ng-if="roleOne.subMenu">
<li ng-repeat="roleTwo in roleOne.subMenu">
<input type="checkbox" id="{{roleTwo.menuId}}" value="{{roleTwo.menuId}}"
ng-model="roleTwo.checked" ng-change="selectTwo($event,$index,roleOne,roleTwo.menuId)"
ng-checked="isSelected(roleTwo.menuId)">
<label for="{{roleTwo.menuId}}">
{{roleTwo.pageTitle}}
</label>
<ul ng-if="roleTwo.subMenu">
<li ng-repeat="roleThree in roleTwo.subMenu">
<input type="checkbox" id="{{roleThree.menuId}}" value="{{roleThree.menuId}}"
ng-model="roleThree.checked" ng-change="selectThree($event,$index,roleTwo,roleThree.menuId)"
ng-checked="isSelected(roleThree.menuId)">
<label for="{{roleThree.menuId}}">
{{roleThree.pageTitle}}
</label>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="clearfix visible-xs-block">
</div>
<div class="col-lg-6">
</div>
</div>
<div class="hr-line-dashed">
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-white" type="reset" ui-sref="role.list">
取消
</button>
<button class="btn btn-primary" type="submit" ng-disabled="role_auth_form.$invalid">
新增产品
</button>
</div>
</div>
<pre>
form = {{ctrl.role }}
</pre>
</form>
</div>
</div>
</div>
</div>
</div>
</div>