angular js 购物车练习题

<style>
        /*隔行变色*/
            table tr:nth-child(2n){
                background: #2FAE2E;
            }
            table tr:nth-child(3n){
                background: red;
            }
        </style>
        <script src="js/angular.min.js"></script>
        <script>
            var app=angular.module("yong",[]);
            app.controller("jiaong",function($scope,$http){
                $scope.stus=[{ gid: 001, gname: '手机', gnum: 3, gprice: 1000, gcount: 3000 },
                        { gid: 002, gname: '电脑', gnum: 3, gprice: 2000, gcount: 6000 },
                        { gid: 003, gname: '电视', gnum: 6, gprice: 500, gcount: 3000 }]
                
                //根据商品名称查询商品
                $scope.sel=function(){
                    //如果输入框没有内容,提示
                    var gname=$scope.cxsname;
                    if(gname==""||gname==undefined){
                        alert("查询内容不能为空");
                    }else{
                        $scope.btnsname=gname;
                    }
                }
                //删除
                $scope.dele=function(i){
                    $scope.stus.splice(i,1)    
                }
                //总金额
                    $scope.gum=function(){
                        count = 0;
                        for(var i in $scope.stus){
                            count=count+$scope.stus[i].gnum*$scope.stus[i].gprice;
                        }
                        return count;
                }
                //清空购物车
                $scope.clear=function(){
                if(window.confirm("确定要清空购物车吗?")){
                $scope.stus=[];
            }
         }
                //总数量
                    $scope.gnum=function(){
                        count = 0;
                        for(var i in $scope.stus){
                            count=count+$scope.stus[i].gnum;
                        }
                        return count;
                }
                //点击减号时候,找当当前商品---判断数量1---如果为1直接从购物车列表中删除
                $scope.jian=function(name){
                    for(var i=0;i<$scope.stus.length;i++){  
                        if($scope.stus[i].gname==name&&$scope.stus[i].gnum==0){
                            var f= confirm("是否把商品从购物成中移除?");
                            if(f){
                                $scope.stus.splice(i,1);
                            }
                        }else if($scope.stus[i].gname==name){
                            $scope.stus[i].gnum--;
                        }
                    }
                }
            })
            
        </script>
    </head>
    <body ng-app="yong" ng-controller="jiaong" style="margin: 0 auto; width: 800px;">
        <h1 align="center">我的购物车详情</h1>
        
        <input type="text" ng-model="mc" placeholder="根据商品名称查询"/>
        
        <table border="1px" width="800px">
            <tr align="center">
                <td ng-click="px='+gid'">商品编号</td>
                <td>商品名称</td>
                <td>商品数量</td>
                <td>商品单价</td>
                <td ng-click="px='-gcount'">价格小计</td>
                <td>操作</td>
            </tr>
            <tr ng-repeat="s in stus|filter:{gname:mc}|orderBy:px">
                <td>{{s.gid}}</td>
                <td>{{s.gname}}</td>
                <td>
                    <input type="number" ng-model="s.gnum" /><button ng-click="jian(s.gname)">-</button> </td>
                </td>
                <td>{{s.gprice}}</td>
                <td>{{s.gnum*s.gcount}}</td>
                <td><button ng-click="dele($index)">移除</button></td>
                
            </tr>
        </table>
        <button>商品数量:{{gnum()}}</button>
        <button>商品总价:{{gum()}}</button>
        <button ng-click="clear()">清空购物车</button>
    </body>

效果图:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值