今天给大家分享一个购物车的例子,希望对大家有所帮助
来源:互联网 发布:网络高清光端机是硬盘 编辑:程序博客网 时间:2024/05/05 16:44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../angular-1.5.5/angular.js"></script>
<style>
button{
background: red;
}
</style>
<script>
var my=angular.module("my",[]);
my.controller("mys",function ($scope) {
/*随意添加数据*/
$scope.data=[
{name:"iPhone8",count:1,price:"8888"},
{name:"iPhone9",count:1,price:"9888"},
{name:"iPhone2s",count:1,price:"3888"},
{name:"iPhone7p+",count:1,price:"10088"}
]
/*删除一个*/
$scope.del=function (index) {
if(confirm('确定移除此项嘛?')){
$scope.data.splice(index,1);
}
};
//增加
$scope.incr = function(index){
$scope.data[index].count++;
}
//减少
$scope.decr = function(index){
if($scope.data[index].count > 1){
$scope.data[index].count--;
}
}
/*总价格*/
$scope.allSum=function () {
var allPrice=0;
for(var i=0;i<$scope.data.length;i++){
allPrice+=$scope.data[i].price*$scope.data[i].count;
}
return allPrice;
};
/*总个数*/
$scope.allNum=function () {
var allnumber=0;
for(var i=0;i<$scope.data.length;i++){
allnumber+=$scope.data[i].count;
}
return allnumber;
};
/*清空购物车*/
$scope.qingkong=function () {
if($scope.data.length==0){
alert('你的购物车为空');
}else{
$scope.data=[];
}
}
})
</script>
</head>
<body ng-app="my" ng-controller="mys">
<table border="1" width="600">
<tr>
<th>产品编号</th>
<th>产品名称</th>
<th>购买数量</th>
<th>产品单价</th>
<th>产品总价</th>
<th>操作</th>
</tr>
<tr ng-repeat="item in data">
<td>{{$index+1}}</td>
<td>{{item.name}}</td>
<td>
<button ng-click="decr($index)">-</button>
<input type="text" ng-model="item.count" readonly="">
<button ng-click="incr($index)">+</button>
</td>
<td>{{item.price}}</td>
<td>{{item.price*item.count}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td colspan="6">总金额<span ng-bind="allSum()|currency:'RMB¥'"></span></td>
</tr>
<tr>
<td colspan="4">总数量<span ng-bind="allNum()"></span></td>
<td colspan="2"><button ng-click="qingkong()">清空购物车</button></td>
</tr>
</table>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../angular-1.5.5/angular.js"></script>
<style>
button{
background: red;
}
</style>
<script>
var my=angular.module("my",[]);
my.controller("mys",function ($scope) {
/*随意添加数据*/
$scope.data=[
{name:"iPhone8",count:1,price:"8888"},
{name:"iPhone9",count:1,price:"9888"},
{name:"iPhone2s",count:1,price:"3888"},
{name:"iPhone7p+",count:1,price:"10088"}
]
/*删除一个*/
$scope.del=function (index) {
if(confirm('确定移除此项嘛?')){
$scope.data.splice(index,1);
}
};
//增加
$scope.incr = function(index){
$scope.data[index].count++;
}
//减少
$scope.decr = function(index){
if($scope.data[index].count > 1){
$scope.data[index].count--;
}
}
/*总价格*/
$scope.allSum=function () {
var allPrice=0;
for(var i=0;i<$scope.data.length;i++){
allPrice+=$scope.data[i].price*$scope.data[i].count;
}
return allPrice;
};
/*总个数*/
$scope.allNum=function () {
var allnumber=0;
for(var i=0;i<$scope.data.length;i++){
allnumber+=$scope.data[i].count;
}
return allnumber;
};
/*清空购物车*/
$scope.qingkong=function () {
if($scope.data.length==0){
alert('你的购物车为空');
}else{
$scope.data=[];
}
}
})
</script>
</head>
<body ng-app="my" ng-controller="mys">
<table border="1" width="600">
<tr>
<th>产品编号</th>
<th>产品名称</th>
<th>购买数量</th>
<th>产品单价</th>
<th>产品总价</th>
<th>操作</th>
</tr>
<tr ng-repeat="item in data">
<td>{{$index+1}}</td>
<td>{{item.name}}</td>
<td>
<button ng-click="decr($index)">-</button>
<input type="text" ng-model="item.count" readonly="">
<button ng-click="incr($index)">+</button>
</td>
<td>{{item.price}}</td>
<td>{{item.price*item.count}}</td>
<td><button ng-click="del($index)">删除</button></td>
</tr>
<tr>
<td colspan="6">总金额<span ng-bind="allSum()|currency:'RMB¥'"></span></td>
</tr>
<tr>
<td colspan="4">总数量<span ng-bind="allNum()"></span></td>
<td colspan="2"><button ng-click="qingkong()">清空购物车</button></td>
</tr>
</table>
</body>
</html>
阅读全文
1 0
- 今天给大家分享一个购物车的例子,希望对大家有所帮助
- 希望对大家的学习有所帮助
- 本人对数据结构的理解,希望对大家有所帮助
- 分享本人累积多年的java开发视频,希望能够对大家有所帮助!
- 多年积累的一些工具希望对大家有所帮助
- 给大家做了一个关于日程的例子,可以参考一下,对进一步学习有所帮助
- 一个技术总监的个人综合评估,希望对大家能有所帮助(转)
- mysql数据库绿色软件制作过程(跟大家分享一下,希望对新手有所帮助)
- 一个Exchange 2010 的密码不定期弹框的问题处理,希望对大家能够有所帮助。
- 程序员拍婚纱照的细节,分享给大家希望对你有帮助
- 怎么发现DLL后门,希望对大家有所帮助。
- Ivy管理项目,希望对大家有所帮助
- 这是在开发的时候经常会遇到的问题希望对大家有所帮助
- 关于exe形式编程的一点心得,希望对大家有所帮助
- [转贴]关于exe形式编程的一点心得,希望对大家有所帮助
- 准备近期写一些关于RESIP开发有关的VOIP资料,希望对大家有所帮助
- 通过博客惊醒整理的文章,希望对大家有所帮助!
- 买二手本子应注意的几点 希望对大家有所帮助
- 查询天气
- mybatis 一对一与一对多collection和association的使用
- 乐观锁
- okhttp
- 回文数的算法
- 今天给大家分享一个购物车的例子,希望对大家有所帮助
- iOS绘制视图
- spark-调优-数据倾斜
- (一)大学里错过表白,现在你将错过它
- angularjs 可添加+修改+删除
- hadoop 2.x常用端口及查看方法
- BZOJ 2437 兔兔与蛋蛋 (博弈论 二分图匹配)
- bianli chaxun 旧密码新密码
- select select二级联动效果