AngularJS 購物車全選/取消全選功能的實現(xiàn)方法
剛學(xué)習(xí)angularJS,于是練習(xí)寫了一個類似于購物車的全選/取消全選的功能,主要實現(xiàn)的功能有:
1、勾選全選checkbox,列表數(shù)據(jù)全部被勾選,取消同理,用ng-model實現(xiàn)雙向綁定;
2、選中列表中的所有checkbox,全選也會被勾選;(這里我想到的方法是給每一個對象增加checked字段,然后勾選觸發(fā)echoChange()函數(shù),用了一個cc變量計算當(dāng)前checked為true的個數(shù),然后再判斷被勾選個數(shù)與數(shù)組長度是否相等,相等則證明全部被勾選,于是全選按鈕也賦值為true;不知道還有沒有更簡單的方式?有請留言告訴我,謝謝!)
3、全部勾選后,只要取消一個全選的check狀態(tài)就為false;
4、實現(xiàn)購物車的小計和總金額計算,僅計算被勾選的商品;
存在待完善的問題:
1、數(shù)量我用了type="number",設(shè)置了min=10,但手動輸入的值沒有做限制,所以如果手動輸入會有非法值;
2、刪除商品功能我只是簡單的用了pop()方法,移除最后一個數(shù)組元素,實際應(yīng)該對每一個商品對象實現(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--購物車實現(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>單價</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;
//購物車應(yīng)該是一個數(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ù)組中的最后的一個元素,并且返回這個元素,會改變數(shù)組里的元素
}
//全選按鈕check的點(diǎn)擊事件
$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;
//當(dāng)所有都選中時,全選也要被勾選
var cc=0;//計算當(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ù)組長度時,證明全部勾選
// 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; //計算出新的結(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í)行時,如果watch觀察的的value與上一次執(zhí)行時不一樣時,就會被觸發(fā)。
AngularJS內(nèi)部的watch實現(xiàn)了頁面隨model的及時更新。
$watch方法在用的時候主要是手動的監(jiān)聽一個對象,但對象發(fā)生變化時觸發(fā)某個事件。
$watch(watchFn,watchAction,deepWatch);
如果不加第三個參數(shù),那么只會監(jiān)聽cart數(shù)組,只有當(dāng)cart引用改變時才會觸發(fā),因此當(dāng)需要監(jiān)聽一些引用對象時需要把第三個參數(shù)設(shè)置成true。
*/
});
</script>
</body>
</html>
如果以上代碼有問題或者您有更好的建議,歡迎您聯(lián)系我,謝謝。
以上這篇AngularJS 購物車全選/取消全選功能的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS實現(xiàn)用戶登錄狀態(tài)判斷的方法(Model添加攔截過濾器,路由增加限制)
AngularJS基于ui-route實現(xiàn)深層路由的方法【路由嵌套】

