<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="angular.js"></script>
<script src="checklist.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-app="app" ng-controller="Ctrl">
<input type="checkbox" ng-checked="user.roles.length == roles.length" ng-click="checkAll()" >全选/反选<br>
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role" > {{role.text}}<br>
</label>
<br>
<br>
<br> user.roles {{ user.roles}}<br>
<br> role.id {{ids}}<br>
<button class="right radius reveal-button" ng-click="del()">删除</button>
</div>
</body>
</html>
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl', function($scope) {
$scope.roles = [
{id: 1, text: 'guest'},
{id: 2, text: 'user'},
{id: 3, text: 'customer'},
{id: 4, text: 'admin'}
];
$scope.ids = [];
//选择的结果集合
$scope.user = {
roles: []
};
$scope.checkAll = function() {
console.log($scope.user.roles.length == $scope.roles.length)
if($scope.user.roles.length == $scope.roles.length){
$scope.user.roles = [];
$scope.ids = [];
}else{
$scope.user.roles = angular.copy($scope.roles);
$scope.ids = [];
}
};
$scope.del =function(){
for(x in $scope.user.roles){
$scope.ids.push($scope.user.roles[x].id);
}
console.log($scope.ids)
};
});
方法如下:
需要用到AngularJS特性:双向数据绑定
注意:没写一行DOM代码!这就是ng的优点,bootstrap.css为了布局,JS代码也只是简单创建ng模块和ng控制器。
效果:
代码如下:
<!DOCTYPE html>
<html lang="en" ng-app="myModule5"><!--3、ng-app="myModule5"启动ng并调用模块-->
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/bootstrap.css">
<title>全选/取消全选</title>
</head>
<body>
<div class="container" ng-controller="myCtrl5"><!--4、ng-controller="myCtrl5"启用控制器-->
<h2>全选和取消全选</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>选择</th>
<th>姓名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>Tom</td>
<td>
<button class="btn btn-danger btn-xs">删除</button>
</td>
</tr>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>Mary</td>
<td>
<button class="btn btn-danger btn-xs">删除</button>
</td>
</tr>
<tr>
<td>
<input ng-checked="selectAll" type="checkbox">
</td>
<td>King</td>
<td>
<button class="btn btn-danger btn-xs">删除</button>
</td>
</tr>
</tbody>
</table>
<input type="checkbox" ng-model="selectAll">
<span ng-hide="selectAll">全选</span>
<span ng-show="selectAll">取消全选</span>
</div>
<script src="js/angular.js"></script><!--1、引入angularJS-->
<script>
//2、创建自定义模块和控制器
angular.module('myModule5', ['ng']).
controller('myCtrl5', function($scope){
});
</script>
</body>
</html>
ps:AngularJs 简单实现全选,多选操作。
js全选和全不选效果?
js全选反选的问题首先选择器写法就不对document.getElementsByName[game],至少这么写其次由于你所有input标签所有的name属性都不一样,不能批量选择,除非像楼上贴代码那哥们一样,把所有的复选框的name属性都改为game也可以。全选取消全选!--引入angularJS--\/\/创建自定义模块和控制器angular.module(my...
js全选和全不选效果?
js全选反选的问题首先选择器写法就不对document.getElementsByName[game],至少这么写其次由于你所有input标签所有的name属性都不一样,不能批量选择,除非像楼上贴代码那哥们一样,把所有的复选框的name属性都改为game也可以。全选取消全选!--引入angularJS--\/\/创建自定义模块和控制器angular.module(my...