AngularJS表格数据全选反选批量删除,删除的数据添加到数组里
2017-10-24 16:10
591 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../AngularJS/angular.js" ></script>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shops = [{
name:"qq1",
price:12.9,
num:3,
state:false
},{
name:"wx1",
price:22,
num:5,
state:false
},{
name:"qq2",
price:33,
num:6,
state:false
},{
name:"wx2",
price:44,
num:4,
state:false
}];
//增加
$scope.add = function(index){
$scope.shops[index].num += 1;
}
//减少
$scope.reduce = function(index){
if($scope.shops[index].num>1){
$scope.shops[index].num -= 1;
}else{
if(window.confirm("要删除"+$scope.shops[index].name+"吗?")){
$scope.shops.splice(index,1);
}else{
}
}
}
//获取总价
$scope.allPrice = function(){
var all = 0;
for(index in $scope.shops){
all += $scope.shops[index].price * $scope.shops[index].num;
}
return all;
}
//全选
$scope.selectAll = false;
$scope.selectAllFun = function(){
if($scope.selectAll){
for(index in $scope.shops){
$scope.shops[index].state = true;
}
}else{
for(index in $scope.shops){
$scope.shops[index].state = false;
}
}
}
//反选
$scope.checkSelectAll = function(){
var flag = false;
for(index in $scope.shops){
if($scope.shops[index].state){
}else{
flag = true;
}
}
//至少有一个没有被选中
if(flag){
$scope.selectAll = false;
}else{
$scope.selectAll = true;
}
}
//批量删除
$scope.deleteSelected = function(){
var selectedShop = [];
for(index in $scope.shops){
if($scope.shops[index].state){
selectedShop.push($scope.shops[index].name);
}
}
if(selectedShop.length>0){
for(i1 in selectedShop){
for(i2 in $scope.shops){
if(selectedShop[i1] == $scope.shops[i2].name){
$scope.shops.splice(i2,1);
}
}
}
}else{
alert("请先选择")
}
}
//判断数据源的长度
$scope.getSize = function(){
if($scope.shops.length > 0 ){
return false;
}else{
return true;
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>我的购物车</h3>
<button ng-click="deleteSelected()">批量删除</button><br /><br />
<table ng-hide="getSize()" border="1 solid blue" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="shop in shops">
<td><input type="checkbox" ng-model="shop.state" ng-click="checkSelectAll()"/></td>
<td>{{shop.name}}</td>
<td>{{shop.price | currency:"¥"}}</td>
<td>
<button ng-click="reduce($index)">-</button>
<input type="number" ng-model="shop.num" style="width: 30px;"/>
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.price * shop.num | currency:"¥"}}</td>
<td><button>删除</button> </td>
</tr>
<tr>
<td colspan="6">总价为:<span ng-bind="allPrice() | currency:'¥'"></span></td>
</tr>
</tbody>
</table>
<span ng-show="getSize()">您的购物车为空,请先逛<a href="#">购物车</a></span>
</center>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../AngularJS/angular.js" ></script>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shops = [{
name:"qq1",
price:12.9,
num:3,
state:false
},{
name:"wx1",
price:22,
num:5,
state:false
},{
name:"qq2",
price:33,
num:6,
state:false
},{
name:"wx2",
price:44,
num:4,
state:false
}];
//增加
$scope.add = function(index){
$scope.shops[index].num += 1;
}
//减少
$scope.reduce = function(index){
if($scope.shops[index].num>1){
$scope.shops[index].num -= 1;
}else{
if(window.confirm("要删除"+$scope.shops[index].name+"吗?")){
$scope.shops.splice(index,1);
}else{
}
}
}
//获取总价
$scope.allPrice = function(){
var all = 0;
for(index in $scope.shops){
all += $scope.shops[index].price * $scope.shops[index].num;
}
return all;
}
//全选
$scope.selectAll = false;
$scope.selectAllFun = function(){
if($scope.selectAll){
for(index in $scope.shops){
$scope.shops[index].state = true;
}
}else{
for(index in $scope.shops){
$scope.shops[index].state = false;
}
}
}
//反选
$scope.checkSelectAll = function(){
var flag = false;
for(index in $scope.shops){
if($scope.shops[index].state){
}else{
flag = true;
}
}
//至少有一个没有被选中
if(flag){
$scope.selectAll = false;
}else{
$scope.selectAll = true;
}
}
//批量删除
$scope.deleteSelected = function(){
var selectedShop = [];
for(index in $scope.shops){
if($scope.shops[index].state){
selectedShop.push($scope.shops[index].name);
}
}
if(selectedShop.length>0){
for(i1 in selectedShop){
for(i2 in $scope.shops){
if(selectedShop[i1] == $scope.shops[i2].name){
$scope.shops.splice(i2,1);
}
}
}
}else{
alert("请先选择")
}
}
//判断数据源的长度
$scope.getSize = function(){
if($scope.shops.length > 0 ){
return false;
}else{
return true;
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>我的购物车</h3>
<button ng-click="deleteSelected()">批量删除</button><br /><br />
<table ng-hide="getSize()" border="1 solid blue" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="shop in shops">
<td><input type="checkbox" ng-model="shop.state" ng-click="checkSelectAll()"/></td>
<td>{{shop.name}}</td>
<td>{{shop.price | currency:"¥"}}</td>
<td>
<button ng-click="reduce($index)">-</button>
<input type="number" ng-model="shop.num" style="width: 30px;"/>
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.price * shop.num | currency:"¥"}}</td>
<td><button>删除</button> </td>
</tr>
<tr>
<td colspan="6">总价为:<span ng-bind="allPrice() | currency:'¥'"></span></td>
</tr>
</tbody>
</table>
<span ng-show="getSize()">您的购物车为空,请先逛<a href="#">购物车</a></span>
</center>
</body>
</html>
相关文章推荐
- jquery table表格添加删除数据(含批量删除,全选全不选和简单的表单验证)
- AngularJs实现添加,批量删除,全选,反选等功能
- AngularJs实现添加,批量删除,全选,反选等功能
- AngularJS表格数据全选反选,批量删除,借助计数器删除
- angular 输入添加,全选,批量删除表格数据
- angularjs表格批量删除和全选反选
- (angularjs) 数据的增删改查(添加用户,查询年龄,批量删除,全部删除,修改密码) 类似题
- AngularJs多选框表格全选反选批量删除
- 动态创建表格;验证数据,全选和反选,批量删除
- RecyclerView单选框删除条目,全选删除,Recyclerview 实现多选,单选,全选,反选,批量删除的步骤 1.在Recyclerview布局中添加上底部的全选和反选按钮,删除按钮,和计算
- AngularJS商品新增订单批量发货删除排序全选反选日期过滤
- 添加,删除,全选,批量,的数据
- 添加全选反选批量删除
- Silverlight删除数据表格中的数据,表格头部定义全选按钮,批量删除
- AngularJs(全选.全不选.批量删除.添加)
- AngularJS,实现输入框失焦添加内容到最前,当数据重复删除原来并显示在数组最前
- android模拟数据实现全选反选批量删除
- Angularjs 增删改查全选反选批量删除总价总数
- 初学Html5+CSS之表格添加用户+删除用户+修改数据+删除全部+批量删除+查询数据+内容判断
- AngularJS综合用户信息, 表单验证,过滤查询,全选反选,批量删除,修改,添加