购物车2

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="js/angular.js"></script>
		<style>
			button {
				background-color: #337AB7;
			}
			
			input {
				width: 20px;
				height: 15px;
			}
		</style>
		<script type="text/javascript">
			var app = angular.module("myApp", []);
			app.controller("myCtrl", function($scope) {
				$scope.user = [{
					name: "QQ",
					price: "12.9",
					num: 1,
					check: false
				}, {
					name: "WX",
					price: "23.9",
					num: 1,
					check: false
				}, {
					name: "momo",
					price: "99.9",
					num: 1,
					check: false
				}];

				//全选
				$scope.selectAll = false;
				$scope.checkAll = function() {
					if($scope.selectAll == true) {
						for(var i = 0; i < $scope.user.length; i++) {
							$scope.user[i].check = true;
						}
					} else {
						for(var i = 0; i < $scope.user.length; i++) {
							$scope.user[i].check = false;
						}
					}
				}

				//批量删除
				$scope.delAll = function() {
					for(var i = 0; i < $scope.user.length; i++) {
						if($scope.user[i].check == true) {
							$scope.user.splice(i, 1);
							i--;
						} else {
							alert("请选择删除的商品");
						}

						if($scope.user.length == 0){
							$scope.isShow = false;
						}
					}

				}

				$scope.isShow = true;

				//删除
				$scope.delete = function(index) {
					$scope.user.splice(index, 1);
					
					if($scope.user.length == 0){
							$scope.isShow = false;
						}
				}

				//加
				$scope.add = function(index) {
					$scope.user[index].num++;
				}
				//减
				$scope.minus = function(index) {
					if($scope.user[index].num > 1) {
						$scope.user[index].num--;
					} else if($scope.user[index].num == 1) {
						var s = confirm("是否移除改商品");
						if(s) {
							$scope.user.splice(index, 1);
						} else {
							alert("不了");
						}

					}

				}

				//总价
				$scope.priceall = function() {
					var priceAll = 0;
					for(var i = 0; i < $scope.user.length; i++) {
						priceAll += $scope.user[i].price * $scope.user[i].num;
					}
					return priceAll;
				}

			});
		</script>
	</head>

	<body ng-app="myApp" ng-controller="myCtrl">
		<center>
			<h3>我的购物车</h3>
			<div ng-show="isShow">
				<button style="background: #D9534F;" ng-click="delAll()">清空购物车</button>
				<table border="1" cellspacing="0" cellpadding="10">
					<tr>
						<th><input type="checkbox" ng-model="selectAll" ng-click="checkAll()" /></th>
						<th>name</th>
						<th>price</th>
						<th>number</th>
						<th>totalPrice</th>
						<th>option</th>
					</tr>
					<tr ng-repeat="u in user">
						<td><input type="checkbox" ng-model="u.check" /></td>
						<td>{{u.name}}</td>
						<td>{{u.price | currency:"¥"}}</td>
						<td>
							<button ng-click="add($index)">+</button><input type="text" ng-model="u.num" />

							<button ng-click="minus($index)">-</button>
						</td>
						<td>{{(u.price)*(u.num)| currency:"¥"}}</td>
						<td><button ng-click="delete($index)">删除</button></td>
					</tr>
					<tr>
						<td colspan="6">总价为:¥<span ng-bind="priceall()">
						
					</span></td>
					</tr>
				</table>
		</center>
		</div>
	</body>

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值