<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'shopping.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
<script type="text/javascript">
var shopTable=angular.module("app",[]);
shopTable.controller("shopController",["$scope",function($scope){
//创建商品数组
$scope.shops=[];
//打开模态框
$scope.add=function(){
//清空模态框
$scope.name = "";
$scope.price = "";
//打开
angular.element("#myModal").modal('show');
};
//添加商品
$scope.insert=function(){
//添加
var shop={name:$scope.name,price:$scope.price,count:1};
$scope.shops.push(shop);
$scope.zongji();
//隐藏模态框
angular.element("#myModal").modal("hide");
};
$scope.incr=function(index,num){
num++;
$scope.shops[index].count=num;
$scope.zongji();
};
$scope.decr=function(index,num){
//只有大于一的时候才减
if(num>1){
num --;
$scope.shops[index].count=num;
$scope.zongji();
}
};
$scope.gai=function(index,num){
alert(num);
$scope.shops[index].count=num;
$scope.zongji();
};
$scope.zongji=function(){
$scope.zongshu=0;
$scope.heji=0;
for(var i=0;i<$scope.shops.length;i++){
$scope.zongshu= $scope.shops[i].count+$scope.zongshu;
$scope.heji+=$scope.shops[i].count*$scope.shops[i].price;
}
};
}]);
</script>
</head>
<body ng-app="app" ng-controller="shopController">
<button type="button" class="btn btn-success" ng-click="add()">添加</button>
<table class="table table-striped">
<tr>
<td>商品名称</td><td>商品价格</td><td>数量</td><td>总价</td>
</tr>
<tr ng-repeat="shop in shops">
<td>{{shop.name}}</td><td>{{shop.price|currency}}</td>
<td><button ng-click="decr($index,shop.count)">-</button><input type="number" size="1" ng-model="shop.count" ng-blur="gai($index,shop.count)"><button ng-click="incr($index,shop.count)">+</button></td>
<td>{{shop.price*shop.count|currency}}</td>
</tr>
<tr><td colspan="2">合计</td><td>{{zongshu}}</td><td>{{heji|currency}}</td></tr>
</table>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">添加商品</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firstname" placeholder="请输入名称" ng-model="name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">价格</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastname" placeholder="请输入价格" ng-model="price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" ng-click="insert()">提交</button>
</form>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
</body>
</html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'shopping.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script>
<script type="text/javascript">
var shopTable=angular.module("app",[]);
shopTable.controller("shopController",["$scope",function($scope){
//创建商品数组
$scope.shops=[];
//打开模态框
$scope.add=function(){
//清空模态框
$scope.name = "";
$scope.price = "";
//打开
angular.element("#myModal").modal('show');
};
//添加商品
$scope.insert=function(){
//添加
var shop={name:$scope.name,price:$scope.price,count:1};
$scope.shops.push(shop);
$scope.zongji();
//隐藏模态框
angular.element("#myModal").modal("hide");
};
$scope.incr=function(index,num){
num++;
$scope.shops[index].count=num;
$scope.zongji();
};
$scope.decr=function(index,num){
//只有大于一的时候才减
if(num>1){
num --;
$scope.shops[index].count=num;
$scope.zongji();
}
};
$scope.gai=function(index,num){
alert(num);
$scope.shops[index].count=num;
$scope.zongji();
};
$scope.zongji=function(){
$scope.zongshu=0;
$scope.heji=0;
for(var i=0;i<$scope.shops.length;i++){
$scope.zongshu= $scope.shops[i].count+$scope.zongshu;
$scope.heji+=$scope.shops[i].count*$scope.shops[i].price;
}
};
}]);
</script>
</head>
<body ng-app="app" ng-controller="shopController">
<button type="button" class="btn btn-success" ng-click="add()">添加</button>
<table class="table table-striped">
<tr>
<td>商品名称</td><td>商品价格</td><td>数量</td><td>总价</td>
</tr>
<tr ng-repeat="shop in shops">
<td>{{shop.name}}</td><td>{{shop.price|currency}}</td>
<td><button ng-click="decr($index,shop.count)">-</button><input type="number" size="1" ng-model="shop.count" ng-blur="gai($index,shop.count)"><button ng-click="incr($index,shop.count)">+</button></td>
<td>{{shop.price*shop.count|currency}}</td>
</tr>
<tr><td colspan="2">合计</td><td>{{zongshu}}</td><td>{{heji|currency}}</td></tr>
</table>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">添加商品</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firstname" placeholder="请输入名称" ng-model="name">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">价格</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastname" placeholder="请输入价格" ng-model="price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button type="button" class="btn btn-primary" ng-click="insert()">提交</button>
</form>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
</body>
</html>