html购物车angular表个操作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>购物车操作</title>
<style>
table tr:nth-child(even){ background-color: lightgray;}
tr button{ width: 30px;}
</style>
<script src="js/jquery-1.11.1.js"></script>
<script src="js/angular.min.js"></script>
<script>
var sapp=angular.module("myapp",[]);
sapp.controller("con",function($scope){
$scope.stus=[];
$scope.fen1="price";
$scope.fen2=true;
$scope.add=function(){
if ($scope.uname==undefined||$scope.uname==""|| $scope.uname.length<=2 || $scope.uname.length>20) {
alert("商品名称输入有误!");
}else if($scope.typee==undefined || $scope.typee.length<=2 || $scope.typee.length>20 || $scope.typee==""){
alert("商品类型输入有误!");
}else if($scope.price<0 || $scope.price==undefined){
alert("商品价格输入有误!");
}else{
if($scope.count==null){
$scope.count=1;
}
var good={name:$scope.uname,type:$scope.typee,price:$scope.price,count:$scope.count};
$scope.stus.push(good);
}
}
$scope.del=function(e){
// 判断是否删除
if (confirm("确认删除吗?")) {
$scope.stus.splice(e,1);
}
}
$scope.sum=function(){
var zo1=0;
for (var i in $scope.stus) {
zo1=zo1+$scope.stus[i].price*$scope.stus[i].count;
}
return zo1;
}
$scope.num=function(cc,i){
if($scope.stus[i].count>=1){
$scope.stus[i].count=$scope.stus[i].count+cc;
}else{
if(confirm("是否要删除该商品?")){
$scope.stus.splice(i,1);
}
}
}
})
</script>
</head>
<body ng-app="myapp" ng-controller="con">
商品名称:<input ng-model="uname"/>
类型:<input ng-model="typee"/>
价格:<input type="number" ng-model="price"/>
数量:<input type="number" ng-model="count"/>     <button ng-click="add()">添加</button>

<table width="900" border="1" cellspacing="0">
<tr style="background-color: lightslategray;">
<th>商品序号</th>
<th>商品名称</th>
<th ng-click="fen1='price';fen2=!fen2">商品价格</th>
<th ng-click="fen1='count';fen2=!fen2">商品数量</th>
<th>商品类型</th>
<th>小计</th>
<th>操作</th>
</tr>
<tr ng-repeat="a in stus|orderBy:fen1:fen2">
<td>{{$index}}</td>
<td>{{a.name}}</td>
<td>{{a.price}}</td>
<td><button ng-click="num(-1,$index)">-</button><input style="width: 30px;" ng-model="a.count" /><button ng-click="num(1,$index)">+</button></td>
<td>{{a.type}}</td>
<td>{{a.price*a.count}}</td>
<td><button ng-click="del($index)" style="width: 50px;">删除</button></td>
</tr>
</table>
<p style="margin-left: 600px;">总计:{{sum()}}</p>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值