angular实现商品加减

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        table{
            border-collapse: collapse;
        }
        th,td{
            padding: 10px;
            border: 1px solid #000;
        }
    </style>
    <script src="angular-1.5.5/angular.min.js"></script>
    <script>
        var myapp=angular.module("myapp",[]);
        myapp.controller("myCtrl",function ($scope) {
            $scope.data=[{
                    name:"qq",
                    price:"12.90",
                    numbar:"2",
                    totalPrice:"25.80",
                },{
                    name:"wx",
                    price:"23.90",
                    numbar:"1",
                    totalPrice:"23.90",
                },{
                    name:"wx",
                    price:"12.9",
                    numbar:"1",
                    totalPrice:"99.90",
                }
                ];
            $scope.delAll=function () {
                $scope.data.length=0;
            };
            $scope.del=function(index){
                console.log(index);
                if(confirm("是否删除"))
                {
                    $scope.data.splice(index,1);
                }
            };
            $scope.checkAll=false;
            $scope.check2=function(){
                if($scope.checkAll==true){
                    for(var i=0;i<$scope.data.length;i++){
                        $scope.data[i].check=true;
                    }
                }else{
                    for(var i=0;i<$scope.data.length;i++){
                        $scope.data[i].check=false;
                    }
                }
            };
            //反着全选
            var n=0;
            $scope.count=function(index){
                //console.log(index);
                if($scope.data[index].check==true){
                    n++;
                }else{
                    n--
                }
                console.log(n);
                if(n==$scope.data.length){
                    $scope.checkAll=true;
                }else{
                    $scope.checkAll=false;
                }
            };
            $scope.min=function(index){
                $scope.data[index].numbar--;
                if($scope.data[index].numbar<0){
                    $scope.data[index].numbar=0;
                }
            };
            $scope.add=function(index){
                $scope.data[index].numbar++;
            };
            $scope.count=function(){
                var n=0;
                console.log(0);
                for(var i=0;i<$scope.data.length;i++){
                    n+=$scope.data[i].price*$scope.data[i].numbar;
                }
                return n;
            };
        });
    </script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<h1>我的购物车</h1>
<button ng-click="delAll()">删除全部</button>
<table>
    <thead>
    <tr>
        <th><input type="checkbox" ng-model="checkAll" ng-click="check2()"></th>
        <th>name</th>
        <th>price</th>
        <th>numbar</th>
        <th>totalPrice</th>
        <th>option</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="item in data">
        <td><input type="checkbox" ng-model="item.check" ng-click="count($index)"></td>
        <td>{{item.name}}</td>
        <td>{{item.price}}</td>
        <td><span ng-click="min($index)">-</span><input type="numbar" ng-model="item.numbar"><span ng-click="add($index)">+</span></td>
        <td>{{item.price*item.numbar}}</td>
        <td><button ng-click="del($index)">删除</button></td>
    </tr>
    <tr>
        <td colspan="6"><p>总价:<a>{{count()}}</a></p></td>
    </tr>
    </tbody>
</table>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值