<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.min.js"></script>
<script src="js/jquery-1.11.1.js"></script>
<script>
angular.module("rxy", []).controller("week3", function($scope) {
$scope.ss = false;
$scope.hh = false;
$scope.s = false;
$scope.goods = [{
names: "qq",
price: 12.9,
num: 2,
}, {
names: "wx",
price: 23.9,
num: 1,
}, {
names: "wx",
price: 99.9,
num: 1,
}];
$scope.add = function() {
$scope.s = true;
}
$scope.sure = function() {
var flag=/米/g;
var na= $("#name").val().replace(flag,'*');
var n = {
names:na,
price: $("#price").val(),
num: $("#num").val(),
};
$scope.goods.push(n);
}
$scope.suan = function(e, name) {
for(var j = 0; j < $scope.goods.length; j++) {
if($scope.goods[j].names == name) {
if($scope.goods[j].num == 1 && e == -1) {
if(confirm("您是否将该商品移除购物车")) {
$scope.goods.splice(j, 1);
}
} else {
$scope.goods[j].num = $scope.goods[j].num + e;
}
}
}
}
$scope.del = function(i) {
$scope.goods.splice(i, 1);
}
$scope.ckAll = function() {
for(var i = 0; i < $scope.goods.length; i++) {
$scope.goods[i].ck = $scope.flag;
}
}
$scope.delAll = function() {
for(var i = 0; i < $scope.goods.length; i++) {
if($scope.goods[i].ck) {
$scope.goods.splice(i, 1);
i--;
}
}
if($scope.goods.length == 0) {
$scope.hh = true;
$scope.ss = true;
}
}
$scope.sum = function() {
var count = 0;
for(var i = 0; i < $scope.goods.length; i++) {
count = count + $scope.goods[i].price * $scope.goods[i].num;
}
return count;
}
})
</script>
</head>
<body ng-app="rxy" ng-controller="week3">
<h1>我的购物车</h1>
<h3 ng-show="ss">购物车为空请继续购物</h3>
<div ng-hide="hh">
<input placeholder="请输入关键字搜索..." ng-model="nn" />
<select style="margin-left: 500px;" ng-model="pai">
<option value="">按数量排序</option>
<option value="+num">按数量正序排序</option>
<option value="-num">按数量倒序排序</option>
</select>
<button style="background: greenyellow;border: none;" ng-click="add()">入库</button><br /><br />
<button style="margin-left: 750px;background: red;border: none;" ng-click="delAll()">清空购物车</button>
<table border="1" style="width: 800px;border-collapse: collapse;text-align: center;">
<tr>
<th><input type="checkbox" ng-click="ckAll()" ng-model="flag" /></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
<tr ng-repeat="i in goods|filter:{name:nn}|orderBy:pai">
<td><input type="checkbox" ng-model="i.ck" /></td>
<td>{{i.names}}</td>
<td>{{i.price|currency:"¥"}}</td>
<td><button ng-click="suan(-1,i.names)">-</button>{{i.num}}<button ng-click="suan(+1,i.names)">+</button></td>
<td>{{i.price*i.num|currency:"¥"}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td colspan="6">总价为:{{sum()|currency:"¥"}}</td>
</tr>
</table>
<div ng-show="s">
name:<input id="name" /> price:
<input id="price" /> num:
<input id="num" />
<button ng-click="sure()">确定</button>
</div>
</div>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/angular.min.js"></script>
<script src="js/jquery-1.11.1.js"></script>
<script>
angular.module("rxy", []).controller("week3", function($scope) {
$scope.ss = false;
$scope.hh = false;
$scope.s = false;
$scope.goods = [{
names: "qq",
price: 12.9,
num: 2,
}, {
names: "wx",
price: 23.9,
num: 1,
}, {
names: "wx",
price: 99.9,
num: 1,
}];
$scope.add = function() {
$scope.s = true;
}
$scope.sure = function() {
var flag=/米/g;
var na= $("#name").val().replace(flag,'*');
var n = {
names:na,
price: $("#price").val(),
num: $("#num").val(),
};
$scope.goods.push(n);
}
$scope.suan = function(e, name) {
for(var j = 0; j < $scope.goods.length; j++) {
if($scope.goods[j].names == name) {
if($scope.goods[j].num == 1 && e == -1) {
if(confirm("您是否将该商品移除购物车")) {
$scope.goods.splice(j, 1);
}
} else {
$scope.goods[j].num = $scope.goods[j].num + e;
}
}
}
}
$scope.del = function(i) {
$scope.goods.splice(i, 1);
}
$scope.ckAll = function() {
for(var i = 0; i < $scope.goods.length; i++) {
$scope.goods[i].ck = $scope.flag;
}
}
$scope.delAll = function() {
for(var i = 0; i < $scope.goods.length; i++) {
if($scope.goods[i].ck) {
$scope.goods.splice(i, 1);
i--;
}
}
if($scope.goods.length == 0) {
$scope.hh = true;
$scope.ss = true;
}
}
$scope.sum = function() {
var count = 0;
for(var i = 0; i < $scope.goods.length; i++) {
count = count + $scope.goods[i].price * $scope.goods[i].num;
}
return count;
}
})
</script>
</head>
<body ng-app="rxy" ng-controller="week3">
<h1>我的购物车</h1>
<h3 ng-show="ss">购物车为空请继续购物</h3>
<div ng-hide="hh">
<input placeholder="请输入关键字搜索..." ng-model="nn" />
<select style="margin-left: 500px;" ng-model="pai">
<option value="">按数量排序</option>
<option value="+num">按数量正序排序</option>
<option value="-num">按数量倒序排序</option>
</select>
<button style="background: greenyellow;border: none;" ng-click="add()">入库</button><br /><br />
<button style="margin-left: 750px;background: red;border: none;" ng-click="delAll()">清空购物车</button>
<table border="1" style="width: 800px;border-collapse: collapse;text-align: center;">
<tr>
<th><input type="checkbox" ng-click="ckAll()" ng-model="flag" /></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
<tr ng-repeat="i in goods|filter:{name:nn}|orderBy:pai">
<td><input type="checkbox" ng-model="i.ck" /></td>
<td>{{i.names}}</td>
<td>{{i.price|currency:"¥"}}</td>
<td><button ng-click="suan(-1,i.names)">-</button>{{i.num}}<button ng-click="suan(+1,i.names)">+</button></td>
<td>{{i.price*i.num|currency:"¥"}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td colspan="6">总价为:{{sum()|currency:"¥"}}</td>
</tr>
</table>
<div ng-show="s">
name:<input id="name" /> price:
<input id="price" /> num:
<input id="num" />
<button ng-click="sure()">确定</button>
</div>
</div>
</body>
</html>