欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS 購物車全選/取消全選功能的實(shí)現(xiàn)方法

 更新時(shí)間:2017年08月14日 07:49:59   投稿:jingxian  
下面小編就為大家?guī)硪黄狝ngularJS 購物車全選/取消全選功能的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

剛學(xué)習(xí)angularJS,于是練習(xí)寫了一個(gè)類似于購物車的全選/取消全選的功能,主要實(shí)現(xiàn)的功能有:

1、勾選全選checkbox,列表數(shù)據(jù)全部被勾選,取消同理,用ng-model實(shí)現(xiàn)雙向綁定;

2、選中列表中的所有checkbox,全選也會被勾選;(這里我想到的方法是給每一個(gè)對象增加checked字段,然后勾選觸發(fā)echoChange()函數(shù),用了一個(gè)cc變量計(jì)算當(dāng)前checked為true的個(gè)數(shù),然后再判斷被勾選個(gè)數(shù)與數(shù)組長度是否相等,相等則證明全部被勾選,于是全選按鈕也賦值為true;不知道還有沒有更簡單的方式?有請留言告訴我,謝謝?。?/p>

3、全部勾選后,只要取消一個(gè)全選的check狀態(tài)就為false;

4、實(shí)現(xiàn)購物車的小計(jì)和總金額計(jì)算,僅計(jì)算被勾選的商品;

存在待完善的問題:

1、數(shù)量我用了type="number",設(shè)置了min=10,但手動輸入的值沒有做限制,所以如果手動輸入會有非法值;

2、刪除商品功能我只是簡單的用了pop()方法,移除最后一個(gè)數(shù)組元素,實(shí)際應(yīng)該對每一個(gè)商品對象實(shí)現(xiàn)刪除;

3、全選/取消全選應(yīng)該還有更嚴(yán)謹(jǐn)?shù)姆椒?,待完善?/p>

附上效果圖:

附上代碼:

<!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--購物車實(shí)現(xiàn)全選/取消全選</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狀態(tài)</td>
  <td>商品名稱</td>
  <td>單價(jià)</td>
  <td>數(shù)量</td>
  <td>小計(jì)</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>單價(jià):¥{{p.price}}</td>
   <td>數(shù)量:<input type="number" ng-model="p.count" min="0" value="p.count"></td>
   <td>小計(jì):¥{{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;
  //購物車應(yīng)該是一個(gè)數(shù)組
  $scope.selectAll=false;//全選默認(rèn)為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);//對數(shù)值向下取整
   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();//刪除數(shù)組中的最后的一個(gè)元素,并且返回這個(gè)元素,會改變數(shù)組里的元素
  }
 
  //全選按鈕check的點(diǎn)擊事件
  $scope.selectAllClick= function (sa) {
    for(var i=0;i<$scope.cart.length;i++){
    $scope.cart[i].checked=sa;
   }
  }
  //單個(gè)數(shù)據(jù)的check事件
  $scope.echoChange=function(id,ch,se){
   $scope.cart[id].checked=!ch;
   //當(dāng)所有都選中時(shí),全選也要被勾選
   var cc=0;//計(jì)算當(dāng)前數(shù)組中checked為真的數(shù)目
   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);//當(dāng)為真的數(shù)目=數(shù)組長度時(shí),證明全部勾選
//   console.log($scope.selectAll);
  }
  //監(jiān)控?cái)?shù)據(jù)
   $scope.$watch('cart',function(newValue,oldValue,scope){
    $scope.sumTotal=0; //總計(jì)
    $scope.jishuqi=0; //計(jì)數(shù)器
    for(var i in newValue) {
     var sumN = newValue[i].count * newValue[i].price; //計(jì)算出新的結(jié)果
     $scope.cart[i].sum = sumN.toFixed(2); //保留兩位小數(shù)并且把它賦值給元數(shù)據(jù);
     if (newValue[i].checked) {
      $scope.sumTotal += sumN;
      $scope.jishuqi++;
//      console.log($scope.sumTotal);
//      console.log($scope.jishuqi);
     }
    }
   },true);
  /*$watch簡介:在digest執(zhí)行時(shí),如果watch觀察的的value與上一次執(zhí)行時(shí)不一樣時(shí),就會被觸發(fā)。
      AngularJS內(nèi)部的watch實(shí)現(xiàn)了頁面隨model的及時(shí)更新。
      $watch方法在用的時(shí)候主要是手動的監(jiān)聽一個(gè)對象,但對象發(fā)生變化時(shí)觸發(fā)某個(gè)事件。
   $watch(watchFn,watchAction,deepWatch);
   如果不加第三個(gè)參數(shù),那么只會監(jiān)聽cart數(shù)組,只有當(dāng)cart引用改變時(shí)才會觸發(fā),因此當(dāng)需要監(jiān)聽一些引用對象時(shí)需要把第三個(gè)參數(shù)設(shè)置成true。
      */
 });
</script>
</body>
</html>

如果以上代碼有問題或者您有更好的建議,歡迎您聯(lián)系我,謝謝。

以上這篇AngularJS 購物車全選/取消全選功能的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論