AngularJS 實(shí)現(xiàn)購(gòu)物車全選反選功能
廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示;
<!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--購(gòu)物車實(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;
//購(gòu)物車應(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);//對(duì)數(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è)元素,會(huì)改變數(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ù)組長(zhǎng)度時(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簡(jiǎn)介:在digest執(zhí)行時(shí),如果watch觀察的的value與上一次執(zhí)行時(shí)不一樣時(shí),就會(huì)被觸發(fā)。
AngularJS內(nèi)部的watch實(shí)現(xiàn)了頁(yè)面隨model的及時(shí)更新。
$watch方法在用的時(shí)候主要是手動(dòng)的監(jiān)聽(tīng)一個(gè)對(duì)象,但對(duì)象發(fā)生變化時(shí)觸發(fā)某個(gè)事件。
$watch(watchFn,watchAction,deepWatch);
如果不加第三個(gè)參數(shù),那么只會(huì)監(jiān)聽(tīng)cart數(shù)組,只有當(dāng)cart引用改變時(shí)才會(huì)觸發(fā),因此當(dāng)需要監(jiān)聽(tīng)一些引用對(duì)象時(shí)需要把第三個(gè)參數(shù)設(shè)置成true。
*/
});
</script>
</body>
</html>
PS:下面給大家分享angularjs 購(gòu)物車的代碼,具體代碼如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>購(gòu)物車</title>
<script type="text/javascript" src="js/angular.js"></script>
</head>
<body ng-app="product" ng-controller="productController">
<center>
<h2>商品列表</h2>
<div class="container">
<!--導(dǎo)航欄-->
<nav>
<div >
<div id="bs-example-navbar-collapse-1">
<div>
<input type="text" ng-model="search" placeholder="產(chǎn)品名稱">
產(chǎn)品價(jià)格:
<select>
<option>0-1000</option>
<option>1000-2000</option>
<option>2000-5000</option>
</select>
<input type="button" style="background:#FF0000" value="全部刪除" ng-click="removeAll()">
</div>
</div>
</div>
</nav><br />
<table border="1 solid" cellpadding="10" cellspacing="0">
<thead>
<tr>
<th ng-click="sortProduct('id')">
產(chǎn)品編號(hào)
<span></span>
</th>
<th ng-click="sortProduct('name')">
產(chǎn)品名稱
<span></span>
</th>
<th ng-click="sortProduct( 'price')">
產(chǎn)品價(jià)格
<span></span>
</th>
<th>
操作
<span></span>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in productList | filter:{ 'name':search} | orderBy:(orderSign+orderColumn) ">
<td>
{{item.id}}
</td>
<td>
{{item.name}}
</td>
<td>
{{item.price | currency:'(RMB)'}}
</td>
<td>
<input type="button" style="background:#FF0000" value="刪除" ng-click="delProduct(item.name)">
</td>
</tr>
</tbody>
</table>
</div>
<script>
angular.module('product',[])
.factory('productList',function(){
return [
{ id:910,name:"imac",price:15400 },
{ id:80,name:"iphone",price:5400 },
{ id:29,name:"ipad",price:14200 },
{ id:500,name:"ipad air",price:23400 },
{ id:1200,name:"ipad mini",price:22000},
{ id:100,name:"android",price:9990 }
]
})
.controller('productController',function($scope,productList){
/*$scope.search = "ipad";//定義一個(gè)變量
alert($scope.search);*/
$scope.productList=productList
$scope.orderColumn='name'; //排序字段
$scope.orderSign='-'; //為空時(shí)正序 為負(fù)號(hào)時(shí)倒序
$scope.sortProduct=function(sortColumn){ //點(diǎn)擊列標(biāo)題排序事件
$scope.orderColumn=sortColumn;//覺(jué)得按照那一列進(jìn)行排序
if($scope.orderSign=="-"){
$scope.orderSign="";
}else{
$scope.orderSign='-';
}
};
//刪除產(chǎn)品
$scope.delProduct = function(name){
//alert(name);
if(name!=""){
if(confirm("是否刪除"+name+"商品") ){
var p;
for (index in $scope.productList) {
p = $scope.productList[index];
if(p.name == name){
$scope.productList.splice(index,1);
}
}
}
}
}
//清空購(gòu)物車
$scope.removeAll = function(){
if(confirm("你確定要清空購(gòu)物車所有商品嗎?")){
$scope.productList = [];
}
}
});
</script>
</center>
</body>
</html>
好了,代碼到此結(jié)束。
總結(jié)
以上所述是小編給大家介紹的AngularJS 實(shí)現(xiàn)購(gòu)物車全選反選功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Angularjs中date過(guò)濾器失效的問(wèn)題及解決方法
這篇文章主要介紹了Angularjs中date過(guò)濾器失效的問(wèn)題及解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Angular directive遞歸實(shí)現(xiàn)目錄樹結(jié)構(gòu)代碼實(shí)例
本篇文章主要介紹了Angular directive遞歸實(shí)現(xiàn)目錄樹結(jié)構(gòu)代碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
AngularJS教程之簡(jiǎn)單應(yīng)用程序示例
本文主要介紹AngularJS簡(jiǎn)單應(yīng)用程序,這里提供了詳細(xì)的流程和代碼程序,有需要的小伙伴可以參考下2016-08-08
AngularJS學(xué)習(xí)筆記之表單驗(yàn)證功能實(shí)例詳解
這篇文章主要介紹了AngularJS學(xué)習(xí)筆記之表單驗(yàn)證功能,結(jié)合實(shí)例形式分析了AngularJS針對(duì)表單的校驗(yàn)、監(jiān)控等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
AngularJS實(shí)現(xiàn)的生成隨機(jī)數(shù)與猜數(shù)字大小功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的生成隨機(jī)數(shù)與猜數(shù)字大小功能,結(jié)合完整實(shí)例形式分析了AngularJS隨機(jī)數(shù)生成與數(shù)值判定相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Angularjs渲染的 using 指令的星級(jí)評(píng)分系統(tǒng)示例
本篇文章主要介紹了Angularjs渲染的 using 指令的星級(jí)評(píng)分系統(tǒng)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Angular實(shí)現(xiàn)二級(jí)導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Angular實(shí)現(xiàn)二級(jí)導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
詳解Angularjs 如何自定義Img的ng-load 事件
本篇文章主要介紹了詳解Angularjs 如何自定義Img的ng-load 事件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
angular框架實(shí)現(xiàn)全選與單選chekbox的自定義
這篇文章主要介紹了angular框架實(shí)現(xiàn)全選與單選chekbox的自定義,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

