我的购物车

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/angular.js"></script>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.shop = [{
"id": "1",
"name": "qq",
"price": "12.90",
"number": "2",
"totalprice": "25.80"
}, {
"id": "2",
"name": "wx",
"price": "23.90",
"number": "1",
"totalprice": "23.90"
}, {
"id": "3",
"name": "lm",
"price": "99.90",
"number": "1",
"totalprice": "99.80"
}];
//删除
$scope.del = function($index) {
if($index >= 0) {
if(confirm("确认删除")) {
$scope.shop.splice($index, 1);
}
}
}
//清空购物车
$scope.delAll = function($index) {
if($scope.shop.length >= 0) {
if(confirm("确认全部删除")) {
$scope.shop.splice($index);
}
}
}
var findIndex = function(id) {
var index = -1;
angular.forEach($scope.shop, function(item, key) {
if(item.id == id) {
index = key;
return;
}
});
return index;
};
//判断当属小于1 的时候就弹出删除界面
$scope.remove = function(id) {
var index = findIndex(id);
if(index != -1) {
$scope.shop.splice(index, 1);
}
};


//添加
$scope.addOne = function(id) {
var index = findIndex(id);
if(index != -1) {
$scope.shop[index].number++;
}
};
//减少一个数量
$scope.removeOne = function(id) {
var index = findIndex(id);
if(index != -1) {
var x = $scope.shop[index];
if(x.number > 1) {
x.number--;
} else {
var returnKey = confirm("是否删除");
if(returnKey) {
$scope.remove(x.id);
}
}
}
};
//购买总价:
$scope.totalCost = function() {
var total = 0;
angular.forEach($scope.shop, function(item, key) {
total += item.price * item.number;
});
return total;
};
//点击没一条后的删除按钮可以删除
$scope.dele = function(index) {
$scope.shop.splice(index, 1);
}
                   //点击全选
                   $scope.checkAll=function(){
                    for(var i=0;i<$scope.shop.length;i++){
                    if($scope.chec==true){
                    $scope.shop[i].done=true;
                    }else{
                    $scope.shop[i].done=false;
                    }
                    }
                   
                    }
                  //删除选中的
                   $scope.delall=function () {
                    for(var i=0;i<$scope.shop.length;i++){
                         if($scope.shop[i].done==true){
                            $scope.shop.splice(i,1);
                              i--;
                          }
                        }
 }
    });
</script>
</head>


<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h1>我的购物车</h1>
<table border="1" cellspacing="0" cellpadding="10 ">
<tr>
<td><button ng-click="delall()">批量删除</button></td>
<td><button ng-click="delAll($index)">清空购物车</button></td>
</tr>
<tr align="center">
<th><input type="checkbox" ng-click="checkAll()" ng-model="chec"  /></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalprice</th>
<th>option</th>
</tr>
<tr ng-repeat="x in shop" align="center">
<td><input type="checkbox" ng-model="x.done" /></td>
<td>{{x.name}}</td>
<td>{{x.price|currency:'¥'}}</td>
<td>
<button ng-click="removeOne(x.id)">-</button>
<input type="text" ng-model="x.number" />
<button ng-click="addOne(x.id)">+</button>
</td>
<td>{{x.price * x.number|currency:'¥'}}</td>
<td><button id="remove" ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td>总价为:</td>
<td colspan="5">{{totalCost() | currency:'¥:'}}</td>
</tr>
</table>
</center>
</body>


</html>





<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/angular.js" ></script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shops=[{
"name":"qq",
"price":12.9,
"num":3,
"state":false
},{
"name":"lm",
"price":22.9,
"num":1,
"state":false
},{
"name":"wx",
"price":32.9,
"num":3,
"state":false
},{
"name":"qw",
"price":58.9,
"num":2,
"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.del=function($index){
                if($index>=0){
                $scope.shops.splice($index,1);
                }
                }
                //全部删除
                $scope.delete=function($index){
                if($scope.shops.length>=0){
                if(confirm("确认删除?")){
                $scope.shops.splice($index);
                }
                }
                }
                
//获取总价
$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>
<button ng-click="delete($index)">全部删除</button>
<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
<th>name</th>
<th>price</th>
<th>num</th>
<th>totalprice</th>
<th>option</th>
</tr>
<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="num" ng-model="shop.num" />
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.num*shop.price|currency:'¥'}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td colspan="6">总价:<span ng-bind="allPrice() | currency:'¥'"></span></td>
</tr>
</table>
<span ng-show="getSize()">您的购物车为空,请闲逛<a href="#">购物车</a></span>
</center>
</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值