<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>购物车</title>
<link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css">
<script src="../vendor/angular.js"></script>
<script src="car-controller.js"></script>
</head>
<body>
<div class="table-responsive container" ng-controller="CarCtrl">
<table class="table " ng-show="data.length">
<thead>
<tr><th>产品编号</th><th>产品名字</th><th>购买数量</th><th>产品单价</th><th>产品总价</th><th>操作</th></tr>
</thead>
<tr ng-repeat="value in data">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>
<button class="btn btn-primary" ng-click="reduce(value.id)" >-</button>
<input type="number" name="num" ng-model="value.quantity" id="num">
<button class="btn btn-primary" ng-click="add(value.id)">+</button>
</td>
<td>{{value.price}}</td>
<td>{{value.price*value.quantity}}</td>
<td>
<button class="btn btn-danger" ng-click="removeItem(value.id)">移除</button>
</td>
</tr>
<tfoot>
<tr>
<td></td>
<td>总购买数量 </td>
<td>{{totalQuantity()}}</td>
<td>总购买价</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空购物车</button>
</td>
</tr>
</tfoot>
</table>
<p ng-show="!data.length">您的购物车为空</p>
</div>
</body>
</html>
var app=angular.module("app",[]);
var carController=function($scope){
$scope.data=[
{
id:1,
name:'HuaWei',
quantity:'2',
price:4300
},
{
id:2,
name:'iphone7',
quantity:'3',
price:6300
},
{
id:3,
name:'XiaoMi',
quantity:'3',
price:2800
},
{
id:4,
name:'Oppo',
quantity:'3',
price:2100
},
{
id:5,
name:'Vivo',
quantity:'3',
price:2100
}
]
}
/*
* 计算总价
* @method: angular.forEach()
* @param: 1. $scope.obj:要遍历的scope上的数据
* 2. function(item){} 回调,在函数内部处理遍历的数据
* */
$scope.totalPrice=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity*item.price;
})
return total
}
/*计算总数量*/
$scope.totalQuantity=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity;
})
return total
}
/*移除某项
* @param:传入当前项的id作为参数,以确定移除哪一项
* */
$scope.removeItem=function(id){
var index=findIndex();
$scope.data.splice(index,1);
}
/*查找当前操作的元素的位置*/
function findIndex(id){
var index=-1;
angular.forEach($scope.data,function(value,key,obj){
if(value.id===id){
index=key;
return index;
}
})
return index;
}
<tr>
<td></td>
<td>总购买数量 </td>
<td>{{totalQuantity()}}</td>
<td>总购买价</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空购物车</button>
</td>
</tr>
/*增加商品数量*/
$scope.add=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
item.quantity++;
}
/*减少商品数量
* @description:
* 减少当前项的数量,当仅剩一件时弹出对话框确认是否清空。是则调用removeItem方法清除当前项
* */
$scope.reduce=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
if(item.quantity>1){ //判断当前项是否还能再减
item.quantity--;
}else{
var makesure=confirm('确定要清空当前项吗??');
if(makesure){
$scope.removeItem(id)
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有