angular 购物车(+,-),小计,总价




<!DOCTYPE html>
<html>

 <head>
  <meta charset="UTF-8">
  <title></title>
  <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/jquery-1.11.1.js" type="text/javascript" charset="utf-8"></script>

  <script>
   var app = angular.module("myapp", []);
   app.controller("my", function($scope) {
    //模拟数据
    $scope.user = [{
     mingzi: "qq",
     jiage: 10,
     shuliang: 1

    }, {
     mingzi: "wx",
     jiage: 20,
     shuliang: 1

    }, {
     mingzi: "wx",
     jiage: 10,
     shuliang: 1

    }];
    
    
    //清空购物车
    $scope.delall = function() {
     $scope.user = [];
     //全部删除,把表格也清除
     $(function(){
       $("table").hide();
      alert(" 购物车为空,去逛商场;");
     })
    }
    
    
    //删除
    $scope.del = function(index) {
     if(confirm("确定要删除吗?")) {
      $scope.user.splice(index, 1);
     }
    }
    
    
    //+,-号,添加或减少
    //shuliang:是在+,-定义的名字
    $scope.shuliang = function(index, unm) {
     $scope.user[index].shuliang = $scope.user[index].shuliang + unm;
     if($scope.user[index].shuliang <= 0) {
      alert("是否删除该商品");
      $scope.user.splice(index, 1);
     }
    }
    
    
    //计算总价
    $scope.zonghe = function() {
     var sum = 0;
     for(var i = 0; i < $scope.user.length; i++) {
      sum += $scope.user[i].jiage * $scope.user[i].shuliang;
     }
     return sum;
    }
   });
  </script>
 </head>

 <body ng-app="myapp" ng-controller="my">
  <h1>我的购物车</h1>
  <button ng-click="delall()" style="margin-left: 400px;">清空购物车</button>
  <table border="1">
   <tr>
    <td>
     <input type="checkbox" ng-model="quanxuan" />
    </td>
    <td>name</td>
    <td>price</td>
    <td>number</td>
    <td>totaPrice</td>
    <td>option</td>
   </tr>


   <tr ng-repeat="i in user">
    <td>
     <input type="checkbox" ng-checked="quanxuan" ng-model="i.shan" />
    </td>
    <td>{{i.mingzi}}</td>
    <td>{{i.jiage | currency:"¥"}}</td>
    <!-- +  - 号-->
    <td>
     <button ng-click="shuliang($index,+1)">+</button>
     <input ng-model="i.shuliang" />
     <button ng-click="shuliang($index,-1)">-</button>
    </td>
    <!--计算小计-->
    <td>{{i.jiage * i.shuliang}}</td>
    <td>
     <button ng-click="del($indel)">删除</button>
    </td>
    
    

    <tr>
     <td colspan="2">总价:</td>
     <td colspan="4">{{zonghe() | currency:"¥"}}</td>
    </tr>
   </tr>
  </table>
 </body>

</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值