溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-09-03 18:31:19 來源:腳本之家 閱讀:146 作者:jingxian 欄目:web開發(fā)

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

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

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

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

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

存在待完善的問題:

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

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

3、全選/取消全選應該還有更嚴謹?shù)姆椒?,待完善?/p>

附上效果圖:

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

附上代碼:

<!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">
 <h5>angularJS--購物車實現(xiàn)全選/取消全選</h5>
 <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>單價</td>
  <td>數(shù)量</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>數(shù)量:<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;
  //購物車應該是一個數(shù)組
  $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);//對數(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ù)組中的最后的一個元素,并且返回這個元素,會改變數(shù)組里的元素
  }
 
  //全選按鈕check的點擊事件
  $scope.selectAllClick= function (sa) {
    for(var i=0;i<$scope.cart.length;i++){
    $scope.cart[i].checked=sa;
   }
  }
  //單個數(shù)據(jù)的check事件
  $scope.echoChange=function(id,ch,se){
   $scope.cart[id].checked=!ch;
   //當所有都選中時,全選也要被勾選
   var cc=0;//計算當前數(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);//當為真的數(shù)目=數(shù)組長度時,證明全部勾選
//   console.log($scope.selectAll);
  }
  //監(jiān)控數(shù)據(jù)
   $scope.$watch('cart',function(newValue,oldValue,scope){
    $scope.sumTotal=0; //總計
    $scope.jishuqi=0; //計數(shù)器
    for(var i in newValue) {
     var sumN = newValue[i].count * newValue[i].price; //計算出新的結果
     $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í)行時,如果watch觀察的的value與上一次執(zhí)行時不一樣時,就會被觸發(fā)。
      AngularJS內部的watch實現(xiàn)了頁面隨model的及時更新。
      $watch方法在用的時候主要是手動的監(jiān)聽一個對象,但對象發(fā)生變化時觸發(fā)某個事件。
   $watch(watchFn,watchAction,deepWatch);
   如果不加第三個參數(shù),那么只會監(jiān)聽cart數(shù)組,只有當cart引用改變時才會觸發(fā),因此當需要監(jiān)聽一些引用對象時需要把第三個參數(shù)設置成true。
      */
 });
</script>
</body>
</html>

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

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI