angular购物车demo

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src="angular.js"></script>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <script type="text/javascript" src="2.js"></script>

</head>
<body ng-app="MyCar" ng-controller="aa">
    <input type="text" class="span3" placeholder="Type something…" ng-model="search" ng-show="data.length">

    <div class="container" >
        <table class="table table-striped"  ng-show="data.length">
            <caption style="text-align: center"><h3>购物车列表</h3></caption>
            <thead>
                <tr>
                    <th ng-click=change('id') ng-class="{dropup : order==='-'}">产品编号<span class="caret"></span></th>
                    <th ng-click=change('name') ng-class="{dropup : order==='-'}">产品名字<span class="caret"></span></th>
                    <th ng-click=change('quantity') ng-class="{dropup : order==='-'}">购买数量<span class="caret"></span></th>
                    <th ng-click=change('price') ng-class="{dropup : order==='-'}">产品单价<span class="caret"></span></th>
                    <th ng-click=change('price') ng-class="{dropup : order==='-'}">产品总价<span class="caret"></span></th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody >
                <tr ng-repeat="item in data | filter : {id:search} | orderBy:order + orderType">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>
                        <button type="button" class="btn btn-info active" ng-click="remove(item.id)">-</button>
                        <input type="text" value="{{item.quantity}}" ng-model="item.quantity" />
                        <button type="button" class="btn btn-info active" ng-click="add(item.id)">+</button>
                    </td>
                    <td>{{item.price | currency:'¥' }}</td>
                    <td>{{item.price*item.quantity | currency:'¥'}}</td>
                    <td>
                        <button type="button" class="btn btn-danger active" ng-click="pop(item.id)">移除</button>
                    </td>
                </tr>
                <tr>
                    <td>总价:</td>
                    <td ng-bind="totalprice()" ></td>
                    <td>购买总数:</td>
                    <td>{{totalnum()}}</td>
                    <td colspan="2"><button ng-click=popAll() type="button" class="btn btn-danger active">全部移除</button></td>

                </tr>
            </tbody>
        </table>
        <p ng-show="! data.length">购物车为空</p>
    </div>
</body>
</html>

js

var myInjectorModule = angular.module("MyCar",[]);
myInjectorModule.controller("aa",["$scope",function($scope){
    $scope.data=[{
        id:100,
        name:'iphone5',
        quantity:24,
        price:4000
    },{
        id:200,
        name:'htc',
        quantity:33,
        price:3500
    },{
        id:300,
        name:'xiaomi',
        quantity:22,
        price:2456
    },{
        id:400,
        name:'sony',
        quantity:64,
        price:4560
    },]

    /**
     * [pp description]购买总价钱
     * @return {[type]} [description]
     */
    $scope.totalprice = function(){
        var total=0;
        angular.forEach($scope.data,function(item){
            total+=item.quantity*item.price;
        })
        return total;
    }
    // $scope.totalprice =pp();

    /**
     * [pp description]购买总数
     * @return {[type]} [description]
     */
    $scope.totalnum = function(){
        var total=0;
        angular.forEach($scope.data,function(item){
            total+=item.quantity;
        })
        return total;
    }
    // $scope.totalnum =ppp();

    /**
     * 删除
     * @param  {[type]} id [description]
     * @return {[type]}    [description]
     */
    $scope.pop=function(id){
        var index=findIndex(id);
        if(index !== -1){
            $scope.data.splice(index,1)
        }
    }
    /**
     * 删除全部
     * @return {[type]} [description]
     */
    $scope.popAll=function(){

        $scope.data=[]
    }

    /**
     * 减少一个数量
     * @param  {[type]} id [description]
     * @return {[type]}    [description]
     */
    $scope.remove=function(id){
        var index=findIndex(id);
        if(index!==-1){
            if($scope.data[index].quantity>1){
                --$scope.data[index].quantity
            }else{
                var flag=confirm("你确定要删除这个商品么?")
                if(flag){
                    $scope.pop(id)
                }
            }

        }

    }
    /**
     * 添加一个数量
     * @param  {[type]} id [description]
     * @return {[type]}    [description]
     */
    $scope.add=function(id){
        var index=findIndex(id);
        if(index!==-1){
            ++$scope.data[index].quantity
        }
    }

    /**
     * 查找对应id对象所在的坐标
     * @param  {[type]} id [description]
     * @return {[type]}    [description]
     */
    var findIndex=function(id){
        var index=-1;
        angular.forEach($scope.data,function(item,key){
            if(item.id==id){
                index=key;
                return index;
            }
        })
        return index;
    }
    /**
     * 监视data数据的变化,脏检查
     * @param  {[type]} newdValue                                                                                  [description]
     * @param  {[type]} oldValue){      angular.forEach(newdValue,function(item,key){           if(item.quantity<1){                var flag          [description]
     * @param  {[type]} true                                                                                       [description]
     * @return {[type]}                                                                                            [description]
     */
    $scope.$watch("data",function(newdValue,oldValue){
        angular.forEach(newdValue,function(item,key){
            if(item.quantity<1){
                var flag=confirm("你确定要删除这个商品么?")
                if(flag){
                    $scope.pop(item.id)
                }else{
                    alert(1)
                    item.quantity=oldValue[key].quantity
                }
            }
        })
    },true)

    /**
     * 过滤器
     * @type {String}
     */
    $scope.order="-";
    $scope.orderType="id";
    $scope.change=function(type){
        $scope.orderType=type;
        if($scope.order=='-'){
            $scope.order="";
        }else{
            $scope.order="-"
        }
    }
}
])

效果图:
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发疯的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值