angularJS商品添加数量加减

<%@ 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">&times;</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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值