<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
document.addEventListener('plusready', function() {
//console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。"
});
</script>
<script src="angular.min.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<style>
h1 {
color: aliceblue;
background: red;
display: flex;
justify-content: center;
align-items: center;
}
table {
width: 100%;
}
tr:nth-child(odd){
background: yellow;
}tr:nth-child(even){
background: cyan;
}
</style>
<body ng-app="myApp" ng-controller="myCtrl">
<h1>购物车</h1>
<input type="text" placeholder="请输入商品名称" ng-model="mo" />
<select ng-model="px">
<option value="price">价格正序</option>
<option value="-price">价格倒序</option>
<option value="btime">时间正序</option>
<option value="-btime">时间倒序</option>
</select>
<input type="button" value="入库/增加" ng-click="xs()" />
<input type="button" value="批量删除" ng-click="alldel()" />
<div id="div">
名称<input type="text" ng-model="name" /><br /> 价格
<input type="text" ng-model="price" /><br /> 产地
<input type="text" ng-model="product" /><br /> 日期
<input type="date" ng-model="btime" /><br />
<input type="button" value="提交" ng-click="add()" />
</div>
<div id="div1">
名称<input type="text" ng-model="name1" /><br /> 价格
<input type="text" ng-model="price1" /><br /> 产地
<input type="text" ng-model="product1" /><br /> 日期
<input type="date" ng-model="btime1" /><br />
<input type="button" value="提交" ng-click="upd()" />
</div>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td> <input type="checkbox" class="che" ng-click="che()" /> </td>
<td>商品名称</td>
<td>价钱</td>
<td>产地</td>
<td>上市日期</td>
<td>操作</td>
</tr>
<tr ng-repeat="g in ser|filter:mo|orderBy:px">
<td> <input type="checkbox" class="che" ng-model="g.che" value="{{g.name}}" /> </td>
<td>{{g.name}}</td>
<td>{{g.price}}</td>
<td>{{g.product}}</td>
<td>{{g.btime}}</td>
<td>
<input type="button" value="删除" ng-click="del(g.name)" />
<input type="button" value="修改" ng-click="xg(g)" />
</td>
</tr>
</table>
<script type="text/javascript">
var mo = angular.module("myApp", []);
mo.controller("myCtrl", function($scope, $http) {
$http.get("aa.json").then(function(red) {
$scope.ser = red.data;
});
$("#div1").fadeToggle(false);
//修改回显
var j="";
$scope.xg = function(g) {
$("#div1").fadeToggle(true);
var name=g.name;
var price=g.price;
var product=g.product;
var btime=g.btime;
j=g.name;
$scope.name1=name;
$scope.price1=price;
$scope.product1=product;
$scope.btime1=btime;
}
//修改
$scope.upd = function(){
var name=$scope.name1;
var price=$scope.price1;
alert(j)
var product=$scope.product1;
var btime=$scope.btime1;
for(var i=$scope.ser.length-1;i>=0;i--){
if($scope.ser[i].name==j){
$scope.ser[i].name=name;
$scope.ser[i].price=price;
$scope.ser[i].product=product;
$scope.ser[i].btime=btime;
alert("修改成功!");
}
}
}
// //修改
// $scope.xg = function(j) {
//
// for(var i =$scope.ser.length-1;i>=0;i--){
// if(j==$scope.ser[i].name){
// var p=prompt("请输入修改的价格",$scope.ser[i].price)
// if(p!=' '||p!=null){
// $scope.ser[i].price=p;
// }
// }
// }
// }
//批量删除
$scope.alldel = function() {
if(confirm("确定删除?")) {
for(var i = $scope.ser.length - 1; i >= 0; i--) {
var j = $scope.ser[i];
if(j.che) {
$scope.ser.splice(i, 1);
}
}
}
}
//全选、反选
$scope.che = function() {
for(var i = 0; i <= $scope.ser.length; i++) {
if($scope.ser[i].che) {
$scope.ser[i].che = false;
} else {
$scope.ser[i].che = true;
}
}
} //单个删除
$scope.del = function(j) {
if(confirm("确定删除?")) {
for(var i = 0; i <= $scope.ser.length; i++) {
if(j == $scope.ser[i].name) {
$scope.ser.splice(i, 1);
}
}
}
}
//显示增加
$("#div").slideToggle(false);
$scope.xs = function() {
$("#div").slideToggle();
}
//增加
$scope.add = function() {
//获取表单元素
var name = $scope.name;
var price = $scope.price;
var product = $scope.product;
var btime = $scope.btime;
if(confirm("确定增加")) {
if(btime == ' ' || btime == null) {
alert("请选择日期!");
}
if(name == ' ' || name == null) {
alert("请输入姓名!");
}
if(price == 0 || price == null) {
alert("价格不为空不为负!");
}
if(product == ' ' || product == null) {
alert("请输入产地!");
}
var t = {
"name": name,
"price": price,
"product": product,
"btime": btime,
};
$scope.ser.push(t);
$("#div").slideToggle(false);
}
}
});
</script>
</body>
</html>