AngularJs简单购物车的实现
来源:互联网 发布:大数据教师培训方案 编辑:程序博客网 时间:2024/06/09 19:30
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shops = [{
name:"qq1",
price:12.9,
num:3,
state:false
},{
name:"wx1",
price:22,
num:5,
state:false
},{
name:"qq2",
price:33,
num:6,
state:false
},{
name:"wx2",
price:44,
num:4,
state:false
}];
//增加
$scope.add = function(index){
$scope.shops[index].num += 1;
}
//减少
$scope.reduce = function(index){
if($scope.shops[index].num>1){
$scope.shops[index].num -= 1;
}else{
if(window.confirm("要删除"+$scope.shops[index].name+"吗?")){
$scope.shops.splice(index,1);
}else{
}
}
}
//获取总价
$scope.allPrice = function(){
var all = 0;
for(index in $scope.shops){
all += $scope.shops[index].price * $scope.shops[index].num;
}
return all;
}
//全选
$scope.selectAll = false;
$scope.selectAllFun = function(){
if($scope.selectAll){
for(index in $scope.shops){
$scope.shops[index].state = true;
}
}else{
for(index in $scope.shops){
$scope.shops[index].state = false;
}
}
}
//反选
$scope.checkSelectAll = function(){
var flag = false;
for(index in $scope.shops){
if($scope.shops[index].state){
}else{
flag = true;
}
}
//至少有一个没有被选中
if(flag){
$scope.selectAll = false;
}else{
$scope.selectAll = true;
}
}
//批量删除
$scope.deleteSelected = function(){
var selectedShop = [];
for(index in $scope.shops){
if($scope.shops[index].state){
selectedShop.push($scope.shops[index].name);
}
}
if(selectedShop.length>0){
for(i1 in selectedShop){
for(i2 in $scope.shops){
if(selectedShop[i1] == $scope.shops[i2].name){
$scope.shops.splice(i2,1);
}
}
}
}else{
alert("请先选择")
}
}
//删除单条数据
$scope.deleItem = function(index){
$scope.shops.splice(index,1);
}
//判断数据源的长度
$scope.getSize = function(){
if($scope.shops.length > 0 ){
return false;
}else{
return true;
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>我的购物车</h3>
<button ng-click="deleteSelected()">批量删除</button><br /><br />
<table ng-hide="getSize()" border="1 solid blue" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="shop in shops">
<td><input type="checkbox" ng-model="shop.state" ng-click="checkSelectAll()"/></td>
<td>{{shop.name}}</td>
<td>{{shop.price | currency:"¥"}}</td>
<td>
<button ng-click="reduce($index)">-</button>
<input type="number" ng-model="shop.num" style="width: 30px;"/>
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.price * shop.num | currency:"¥"}}</td>
<td><button ng-click="deleItem(index)">删除</button> </td>
</tr>
<tr>
<td colspan="6">总价为:<span ng-bind="allPrice() | currency:'¥'"></span></td>
</tr>
</tbody>
</table>
<span ng-show="getSize()">您的购物车为空,请先逛<a href="#">购物车</a></span>
</center>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shops = [{
name:"qq1",
price:12.9,
num:3,
state:false
},{
name:"wx1",
price:22,
num:5,
state:false
},{
name:"qq2",
price:33,
num:6,
state:false
},{
name:"wx2",
price:44,
num:4,
state:false
}];
//增加
$scope.add = function(index){
$scope.shops[index].num += 1;
}
//减少
$scope.reduce = function(index){
if($scope.shops[index].num>1){
$scope.shops[index].num -= 1;
}else{
if(window.confirm("要删除"+$scope.shops[index].name+"吗?")){
$scope.shops.splice(index,1);
}else{
}
}
}
//获取总价
$scope.allPrice = function(){
var all = 0;
for(index in $scope.shops){
all += $scope.shops[index].price * $scope.shops[index].num;
}
return all;
}
//全选
$scope.selectAll = false;
$scope.selectAllFun = function(){
if($scope.selectAll){
for(index in $scope.shops){
$scope.shops[index].state = true;
}
}else{
for(index in $scope.shops){
$scope.shops[index].state = false;
}
}
}
//反选
$scope.checkSelectAll = function(){
var flag = false;
for(index in $scope.shops){
if($scope.shops[index].state){
}else{
flag = true;
}
}
//至少有一个没有被选中
if(flag){
$scope.selectAll = false;
}else{
$scope.selectAll = true;
}
}
//批量删除
$scope.deleteSelected = function(){
var selectedShop = [];
for(index in $scope.shops){
if($scope.shops[index].state){
selectedShop.push($scope.shops[index].name);
}
}
if(selectedShop.length>0){
for(i1 in selectedShop){
for(i2 in $scope.shops){
if(selectedShop[i1] == $scope.shops[i2].name){
$scope.shops.splice(i2,1);
}
}
}
}else{
alert("请先选择")
}
}
//删除单条数据
$scope.deleItem = function(index){
$scope.shops.splice(index,1);
}
//判断数据源的长度
$scope.getSize = function(){
if($scope.shops.length > 0 ){
return false;
}else{
return true;
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>我的购物车</h3>
<button ng-click="deleteSelected()">批量删除</button><br /><br />
<table ng-hide="getSize()" border="1 solid blue" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectAll" ng-click="selectAllFun()"/></th>
<th>name</th>
<th>price</th>
<th>number</th>
<th>totalPrice</th>
<th>option</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="shop in shops">
<td><input type="checkbox" ng-model="shop.state" ng-click="checkSelectAll()"/></td>
<td>{{shop.name}}</td>
<td>{{shop.price | currency:"¥"}}</td>
<td>
<button ng-click="reduce($index)">-</button>
<input type="number" ng-model="shop.num" style="width: 30px;"/>
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.price * shop.num | currency:"¥"}}</td>
<td><button ng-click="deleItem(index)">删除</button> </td>
</tr>
<tr>
<td colspan="6">总价为:<span ng-bind="allPrice() | currency:'¥'"></span></td>
</tr>
</tbody>
</table>
<span ng-show="getSize()">您的购物车为空,请先逛<a href="#">购物车</a></span>
</center>
</body>
</html>
阅读全文
0 0
- AngularJs实现简单的购物车
- Angularjs实现简单的购物车
- AngularJs简单购物车的实现
- AngularJS 实现简单购物车
- 简单实现angularJs购物车的增删改的样式
- AngularJs简单购物车的是实现2
- angularjs简单实现购物车需求
- angularjs简单购物车
- AngularJs购物车实现
- angularjs实现购物车
- AngularJs实现购物车
- angularjs实现的购物车功能
- angularjs实现购物车的一些功能
- angularjs简单购物车源码
- angularJS实现购物车功能
- angularJS实现购物车订单
- AngularJS实现购物车(一)
- AngularJS实现购物车(二)
- mongodb 安装与配置
- 第1132期AI100_机器学习日报(2017-10-24)
- ImageLOader
- Angular球员页面---一个简单的Demo
- 多条目展示
- AngularJs简单购物车的实现
- 智能小车42:openwrt的系统配置与gpio操作
- 【12345】输出54321
- 金字塔原理——表达的逻辑(笔记)
- JZOJ5424. 【NOIP2017提高A组集训10.25】凤凰院凶真
- 文章标题
- JZOJ 5409 【NOIP2017提高A组集训10.21】Fantasy
- 【Lucas定理+高维前缀和】2017.10.26杂题[虚妄之诺]题解
- 一个考验c语言和数据结构功底的小项目