网络或获取数据删除小计总计清空

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script src="../js/angular.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
angular.module("myapp", []).controller("myctrl", function($scope, $http) {
$scope.flag = true;
//1.获取数据
$http.get("http://result.eolinker.com/rR1VBtT56a6bb220c10b3d44b65b4787a8aec03c4ec32ce?uri=ThirdTest").then(function(cheng) {
$scope.goods = cheng.data;
//alert($scope.goods.length);
})
//2.总价
$scope.getTotal = function() {
var t = 0;
for(var i = 0; i < $scope.goods.length; i++) {
t = t + $scope.goods[i].number * $scope.goods[i].price;
}
return t;
}
//3.删除---单行
$scope.del = function(i) {
$scope.goods.splice(i, 1);
}
//4.清空购物车
$scope.clear = function() {
$scope.goods = [];
$scope.flag = false;
}
//5.减少数量
$scope.jian = function(index) {
var num = $scope.goods[index].number;
if(num > 1) {
$scope.goods[index].number--;
} else {
var f = confirm("是否删除此商品?");
if(f) {
$scope.goods.splice(index, 1);
}
}
}


})
</script>
</head>


<body ng-app="myapp" ng-controller="myctrl" style="width: 600px;">
<h2>我的购物车</h2>
<span ng-show="!flag">你的购物车已清空<a href="#">去商城逛逛</a></span>
<button ng-click="clear()" style="float: right;">清空购物车</button>
<table border="1px" cellspacing="0" cellpadding="0" width="600px" ng-show="flag">
<tr>
<td><input type="checkbox" ng-model="ischeck" /></td>
<td>商品名称</td>
<td ng-click="px='-price'">商品价格</td>
<td>商品数量</td>
<td ng-click="px='number*price'">小计</td>
<td>操作</td>
</tr>
<tr ng-repeat="s in goods|orderBy:px">
<td><input type="checkbox"  ng-model="ischeck" /></td>
<td>{{s.name}}</td>
<td>{{s.price|currency:"¥"}}</td>
<td><button ng-click="s.number=s.number+1">+</button><input type="text" value="{{s.number}}" /><button ng-click="jian($index)">-</button></td>
<td>{{s.number*s.price|currency:"¥"}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
</table>
<span ng-show="flag">总计:{{getTotal()|currency:"¥"}}</span>
</body>


</html>

猜你喜欢

转载自blog.csdn.net/h_builder/article/details/80074869