国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

AngularJS 實現購物車全選反選功能

2024-05-06 15:25:16
字體:
來源:轉載
供稿:網友

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示; 

<!DOCTYPE html><html lang="en" ng-app="testMo"><head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" > <style> .div1{  margin: 20px; } </style></head><body><div ng-controller="testCtrl" class="div1"> <h4>angularJS--購物車實現全選/取消全選</h4> <button type="button" class="btn btn-info" ng-click="addProduct()">添加商品</button> <button type="button" class="btn btn-danger" ng-click="deleteProduct()">刪除商品</button> <br><br> <table class="table table-bordered table-responsive" > <thead> <td>操作</td> <td>check狀態</td> <td>商品名稱</td> <td>單價</td> <td>數量</td> <td>小計</td> </thead> <tr ng-repeat="p in cart" >  <td><input type="checkbox" ng-checked="p.checked" ng-click="echoChange(p.id,p.checked,selectAll)"></td>  <td>{{p.checked}}||{{p.checked}}</td>  <td>{{p.name}}</td>  <td>單價:¥{{p.price}}</td>  <td>數量:<input type="number" ng-model="p.count" min="0" value="p.count"></td>  <td>小計:¥{{p.sum}}</td> </tr> </table> <br> <input type="checkbox" ng-model="selectAll" ng-click="selectAllClick(selectAll)"><span ng-hide="selectAll" >全選</span><span ng-show="selectAll">取消全選</span> <br><br> 已選擇<span>{{jishuqi}}</span>件商品,總金額:<span>¥{{ sumTotal }}</span></div><script src="../js/angular.js"></script><script> angular.module('testMo',['ng']).controller('testCtrl',function($scope){// $scope.p1=new Object();// $scope.p1.price=10;// $scope.p1.count=1; //購物車應該是一個數組 $scope.selectAll=false;//全選默認為false $scope.cart=[{id:0,name:'商品0',price:10,count:5,sum:10,checked:false}]; $scope.addProduct= function (){  var p=new Object();  p.id=$scope.cart.length;  p.name='商品'+ p.id  p.price=Math.floor(Math.random()*100);//對數值向下取整  p.count=1;  p.sum= p.price* p.count;  p.checked=false;  $scope.cart.push({id: p.id,name: p.name,price:p.price,count: p.count,sum: p.sum,checked: p.checked});  console.log($scope.cart); } //刪除商品 $scope.deleteProduct= function (){  $scope.cart.pop();//刪除數組中的最后的一個元素,并且返回這個元素,會改變數組里的元素 } //全選按鈕check的點擊事件 $scope.selectAllClick= function (sa) {  for(var i=0;i<$scope.cart.length;i++){  $scope.cart[i].checked=sa;  } } //單個數據的check事件 $scope.echoChange=function(id,ch,se){  $scope.cart[id].checked=!ch;  //當所有都選中時,全選也要被勾選  var cc=0;//計算當前數組中checked為真的數目  for(var i=0;i<$scope.cart.length;i++){//  if($scope.cart[i].checked==true){//   cc++;//  }  $scope.cart[i].checked?cc++:cc;  }  $scope.selectAll=(cc==$scope.cart.length);//當為真的數目=數組長度時,證明全部勾選//  console.log($scope.selectAll); } //監控數據 $scope.$watch('cart',function(newValue,oldValue,scope){  $scope.sumTotal=0; //總計  $scope.jishuqi=0; //計數器  for(var i in newValue) {  var sumN = newValue[i].count * newValue[i].price; //計算出新的結果  $scope.cart[i].sum = sumN.toFixed(2); //保留兩位小數并且把它賦值給元數據;  if (newValue[i].checked) {   $scope.sumTotal += sumN;   $scope.jishuqi++;//   console.log($scope.sumTotal);//   console.log($scope.jishuqi);  }  } },true); /*$watch簡介:在digest執行時,如果watch觀察的的value與上一次執行時不一樣時,就會被觸發。  AngularJS內部的watch實現了頁面隨model的及時更新。  $watch方法在用的時候主要是手動的監聽一個對象,但對象發生變化時觸發某個事件。  $watch(watchFn,watchAction,deepWatch);  如果不加第三個參數,那么只會監聽cart數組,只有當cart引用改變時才會觸發,因此當需要監聽一些引用對象時需要把第三個參數設置成true。  */ });</script></body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 无极县| 德钦县| 南木林县| 巫山县| 安庆市| 钟山县| 河西区| 郓城县| 昌邑市| 达孜县| 鄂伦春自治旗| 杂多县| 黎平县| 贵港市| 库车县| 巫山县| 嘉荫县| 新昌县| 垦利县| 庆城县| 塔河县| 泸水县| 汶上县| 永寿县| 昭平县| 甘孜县| 绥滨县| 龙岩市| 牡丹江市| 怀远县| 沙坪坝区| 辉县市| 通海县| 乌兰察布市| 霍州市| 泌阳县| 林芝县| 马山县| 沂南县| 信宜市| 万盛区|