购物车angular

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="angular-1.3.0.js"></script>
    <style>
        .s{
            text-decoration: none;
        }
        table{
            border-collapse: collapse;
        }
    </style>
    <script type="text/javascript">
        var app=angular.module("m",[]);
        app.controller("myc",function($scope){
            $scope.div1=false;
            $scope.div2=true;
            $scope.data=[
                {
                    name:"qq",
                    price:"12.50",
                    number:1
                },
                {
                    name:"ww",
                    price:"23.50",
                    number:3
                },
                {
                    name:"we",
                    price:"99.90",
                    number:2
                },
                {
                    name:"vc",
                    price:"99.90",
                    number:2
                }
            ]
            //全选
            $scope.che=false;
            $scope.cheall= function () {
                if($scope.che==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 a=0;
            $scope.chepl=function(index){
                if($scope.data[index].check==true){
                    a++;
                }else{
                    a--;
                }
                if(a==$scope.data.length){
                    $scope.che=true;
                }else{
                    $scope.che=false;
                }
            }
            $scope.del= function (index) {
                $scope.data.splice(index,1);
                if($scope.data.length==0){
                   $scope.div1=true;
                   $scope.div2=false;
                }
            }
            //清空购物车
            $scope.qk= function () {
                $scope.data=[];
                $scope.che=false;
                $scope.div1=true;
                $scope.div2=false;
            }
            //加号按钮的点击事件
            $scope.jia= function (index) {
                $scope.data[index].number++;
            }
            //减号按钮的点击事件
            $scope.jian= function (index) {
                if($scope.data[index].number==1){
                    //如果已经数量为1 ,不能再减去,,询问是否删除
                    var queren = confirm("是否删除该商品?");
                    if(queren){
                        //当用户点击确认时删除该条商品信息
                        $scope.data.splice(index,1);
                        if($scope.data.length==0){
                            //如果数组没数据了,,就隐藏div
                            $scope.div2=false;
                            $scope.div1=true;
                        }
                    }
                }else{
                    $scope.data[index].number--;
                }
            }
            //计算总价的方法
            $scope.zongjia= function () {
                var zj=0;//默认总价是0
                for(var u in $scope.data){
                    zj += $scope.data[u].price * $scope.data[u].number;
                }
                return zj;
            }
            $scope.zongliang= function () {
                var zongliang=0;//默认总价是0
                for(var i in $scope.data){
                    zongliang += $scope.data[i].number;
                }
                return zongliang;
            }
        });
    </script>
</head>
<body ng-app="m" ng-controller="myc">
    <div ng-show="div1">您的购物车为空,<a class="s" href="#">去逛商场</a></div>
    <div ng-show="div2">
        <table border="1" align="center" width="600px">
            <tr >
                <td colspan="6">
                    <button style="float: right;border-collapse: collapse;background-color: #880000;color: #f5f5f5" ng-click="qk()">清空购物车</button>
                </td>
            </tr>
            <tr align="center">
                <td><input type="checkbox" ng-model="che" ng-click="cheall()"></td>
                <td>name</td>
                <td>price</td>
                <td>number</td>
                <td>totalprice</td>
                <td>option</td>
            </tr>
            <tr ng-repeat="item in data" align="center">
                <td><input type="checkbox" ng-model="item.check" ng-click="chepl($index)"></td>
                <td>{{item.name}}</td>
                <td>{{item.price | currency:""}}</td>
                <td>
                    <button ng-click="jian($index)" style="background-color: #007aff;border: 0">-</button>
                    <input type="number" ng-model="item.number">
                    <button ng-click="jia($index)" style="background-color: #007aff;border: 0">+</button>
                </td>
                <td>{{item.price*item.number | currency:""}}</td>
                <td>
                    <button style="border:0 ; background-color: #007aff" ng-click="del($index)">删除</button>
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    总价为:{{zongjia() | currency:""}}<span style="margin-left: 150px">总数量为:{{zongliang()}}</span>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值