购物车



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
table tr:nth-child(even){
background-color: #FFE4C4;
}
table tr:nth-child(odd){
background-color: #6495ED;
}
</style>
<script type="text/javascript" src="js/angular.js" ></script>
<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
<script>

var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
//准备初始数据
var date1 = new Date("2017-9-01 10:10:10");
var date2 = new Date("2017-1-02 10:10:10");
var date3 = new Date("2017-1-03 10:10:10");
$scope.goods = [{
name:"云南白药",
num:100,
address:"云南",
price:19.911,
date:date1,
flag:true,
ck:false
},{
name:"999感冒灵",
num:30,
address:"北京",
price:12.511,
date:date2,
flag:true,
ck:false
},{
name:"感康云",
num:20,
address:"河北",
price:16.611,
date:date3,
flag:true,
ck:false
}];

//搜索条件
$scope.search="";
$scope.search1="";
$scope.orderByKey = "";

//入库相关
$scope.ifFlag = false;
//点击 入库 是否显示下方的输入区域
$scope.ifAdd = function(){
$scope.ifFlag = !$scope.ifFlag;

}
//添加商品
$scope.newname="";
$scope.newnum="";
$scope.newaddress="";
$scope.newprice="";

$scope.updateIndex = 0;
$scope.addGood = function(){
if($("#btn1").val() == "添加"){
//创建good对象
var good = {
name:$scope.newname,
num:$scope.newnum,
address:$scope.newaddress,
price:parseFloat($scope.newprice),
date:new Date(),
flag:true
};
if($scope.goods.length == 0){//此次添加,如果table没有出现,先显示
$scope.showTitle = true;
}
$scope.goods.unshift(good);
$scope.ifFlag = false;//添加完后,自动隐藏输入区域
}else{
$scope.goods[$scope.updateIndex ].name = $scope.newname;
$scope.goods[$scope.updateIndex ].num = $scope.newnum;
$scope.goods[$scope.updateIndex ].address = $scope.newaddress;
$scope.goods[$scope.updateIndex ].price = $scope.newprice;

}
}

$scope.showTitle = true;
//删除商品
$scope.deleteGood = function($index){
$scope.goods.splice($index,1);//在goods数组中删除此商品
//从新获得总价
$scope.getAllPrice();//调用计算总价的函数

if($scope.goods.length == 0){
$scope.showTitle = false;
}
}
//修改
$scope.updateGood = function($index){
$scope.updateIndex = $index;
//显示修改区域
$scope.ifFlag = true;
$("#btn1").val("修改");

$scope.newname=$scope.goods[$index].name;
$scope.newnum=$scope.goods[$index].num;
$scope.newaddress=$scope.goods[$index].address;
$scope.newprice=$scope.goods[$index].price;
}

//总计
$scope.allPrice = 0;
$scope.getAllPrice = function(){
var count = 0;
for(index in $scope.goods){//遍历goods,得到索引
count += $scope.goods[index].price*$scope.goods[index].num;
}
$scope.allPrice = count;
}
$scope.getAllPrice();//调用计算总价的函数

//减号
$scope.jian1 = function($index){
$scope.goods[$index].num = $scope.goods[$index].num-1;
$scope.getAllPrice();//调用计算总价的函数
}

$scope.ckchange = function(){
var ss = $scope.goods;
debugger;
}

$scope.deleteMore = function(){
debugger;
for(var i=$scope.goods.length-1;i>=0;i--){
var _ck = $scope.goods[i];
if(_ck.ck){
$scope.goods.splice(i,1);
}
}
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
<h3>1511k商品信息</h3>
<input type="text" ng-model="search" style="border: 2px solid red;" placeholder="请输入搜索关键字"/>
<input type="text" ng-model="search1" style="border: 2px solid red;" placeholder="请输入搜索关键字"/>
<select ng-model="orderByKey">
<option value="">--请选择--</option>
<option value="num">数量升序</option>
<option value="-num">数量降序</option>
<option value="price">价格升序</option>
<option value="-price">价格降序</option>
</select>
<input type="button" value="入库" ng-click="ifAdd();"/><br /><br />
<input type="button" value="批量删除" ng-click="deleteMore();" /><br />
<table ng-show="showTitle" >
<tr  style="background-color: #006400;">
<th width="80"><input type="checkbox"  ng-model="uu"/></th>
<th width="80">序号</th><th width="80">名字</th>
<th width="120" ng-click="orderByKey='num'">数量</th><th width="80">地址</th>
<th width="80" ng-click="orderByKey='price'">价格</th><th >日期</th><th width="80">小计</th><th >操作</th></tr>
<tr align="center" ng-repeat="g in goods | orderBy:orderByKey | filter:{name:search,address:search1}">
<td><input type="checkbox" ng-checked="uu" ng-model="g.ck" name='g.name'/></td>
<td>{{$index}}</td>
<td>{{g.name}}</td>
<td><input type="button"value="-" ng-click="g.num=g.num-1;getAllPrice();"/>&nbsp;&nbsp;
{{g.num}}&nbsp;&nbsp;
<input type="button"value="+" ng-click="g.num=g.num+1;getAllPrice();"/>&nbsp;&nbsp;
</td>
<td>{{g.address}}</td>
<!--
                    价钱列
                    -->
<td ng-dblclick="g.flag=false"><span ng-show="g.flag">{{g.price}}</span>
  <input ng-hide="g.flag" ng-model="g.price" ng-change="getAllPrice();" ng-blur="g.flag=true;" type="text" /></td>

<td>{{g.date | date:"yyyy-MM-dd hh:mm:ss"}}</td>
<td>{{g.price*g.num | number:2}}</td>
<td>
<input type="button"value="删除" ng-click="deleteGood($index);"/>
<input type="button" value="修改" ng-click="updateGood($index);"/>
</td>
</tr>
<tr><td colspan="8">{{goods}}</td></tr>
</table><br />
总价:<span>{{allPrice | number:2}}</span>
<div ng-show="ifFlag">
名字:<input type="text" placeholder="输入名字" ng-model="newname" /><br />
数量:<input type="text" placeholder="输入数量" ng-model="newnum"/><br />
地址:<input type="text" placeholder="输入地址" ng-model="newaddress"/><br />
价格:<input type="text" placeholder="输入价格" ng-model="newprice"/><br /><br />
<input type="button" id="btn1" value="添加" ng-click="addGood();"/>

</div>


</center>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值