angular和BootStrap3實(shí)現(xiàn)購物車功能
一、頁面搭建
1、html結(jié)構(gòu)
采用BootStrap3來快速創(chuàng)建一個購物車表單樣式。
主要功能:
A:列表數(shù)據(jù)所示
B:增加刪除端口
C:清空購物車
D:商品數(shù)量的增減
E:動態(tài)計算商品價格以及總價
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>購物車</title>
<link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css">
<script src="../vendor/angular.js"></script>
<script src="car-controller.js"></script>
</head>
<body>
<div class="table-responsive container" ng-controller="CarCtrl">
<table class="table " ng-show="data.length">
<thead>
<tr><th>產(chǎn)品編號</th><th>產(chǎn)品名字</th><th>購買數(shù)量</th><th>產(chǎn)品單價</th><th>產(chǎn)品總價</th><th>操作</th></tr>
</thead>
<tr ng-repeat="value in data">
<td>{{value.id}}</td>
<td>{{value.name}}</td>
<td>
<button class="btn btn-primary" ng-click="reduce(value.id)" >-</button>
<input type="number" name="num" ng-model="value.quantity" id="num">
<button class="btn btn-primary" ng-click="add(value.id)">+</button>
</td>
<td>{{value.price}}</td>
<td>{{value.price*value.quantity}}</td>
<td>
<button class="btn btn-danger" ng-click="removeItem(value.id)">移除</button>
</td>
</tr>
<tfoot>
<tr>
<td></td>
<td>總購買數(shù)量 </td>
<td>{{totalQuantity()}}</td>
<td>總購買價</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空購物車</button>
</td>
</tr>
</tfoot>
</table>
<p ng-show="!data.length">您的購物車為空</p>
</div>
</body>
</html>
2、js邏輯部分
1 注冊應(yīng)用app
2 定義controller以即數(shù)據(jù)
3 在html中綁定應(yīng)用以及controller,實(shí)現(xiàn)數(shù)據(jù)渲染
var app=angular.module("app",[]);
var carController=function($scope){
$scope.data=[
{
id:1,
name:'HuaWei',
quantity:'2',
price:4300
},
{
id:2,
name:'iphone7',
quantity:'3',
price:6300
},
{
id:3,
name:'XiaoMi',
quantity:'3',
price:2800
},
{
id:4,
name:'Oppo',
quantity:'3',
price:2100
},
{
id:5,
name:'Vivo',
quantity:'3',
price:2100
}
]
}
注意:
1、在html中通過ng-app注冊應(yīng)用,ng-controller來綁定控制器作用域,ng-repeat遍歷商品數(shù)據(jù)data
2、在js中,angular提供了angular.forEach(obj,fn(item){})方法來遍歷angular的數(shù)據(jù),并可以在fn中對數(shù)據(jù)做進(jìn)一步處理。此處計算總價便通過此方法
二、業(yè)務(wù)邏輯
1、總價計算操作
/*
* 計算總價
* @method: angular.forEach()
* @param: 1. $scope.obj:要遍歷的scope上的數(shù)據(jù)
* 2. function(item){} 回調(diào),在函數(shù)內(nèi)部處理遍歷的數(shù)據(jù)
* */
$scope.totalPrice=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity*item.price;
})
return total
}
/*計算總數(shù)量*/
$scope.totalQuantity=function(){
var total=0;
angular.forEach($scope.data,function(item){
total+=item.quantity;
})
return total
}
說明:
1)、使用angular提供的forEach方法遍歷當(dāng)前數(shù)據(jù),從而計算出所有數(shù)據(jù)的總價以及總數(shù)量
2、刪除條目
/*移除某項(xiàng)
* @param:傳入當(dāng)前項(xiàng)的id作為參數(shù),以確定移除哪一項(xiàng)
* */
$scope.removeItem=function(id){
var index=findIndex();
$scope.data.splice(index,1);
}
說明:
1)、為代碼利用,所以抽取出來findIndex()方法用于確定當(dāng)前操作的項(xiàng),代碼如下:
/*查找當(dāng)前操作的元素的位置*/
function findIndex(id){
var index=-1;
angular.forEach($scope.data,function(value,key,obj){
if(value.id===id){
index=key;
return index;
}
})
return index;
}
3.清空操作
在頁面中,直接利用表達(dá)式,但data=null完成數(shù)據(jù)的清空操作
<tr>
<td></td>
<td>總購買數(shù)量 </td>
<td>{{totalQuantity()}}</td>
<td>總購買價</td>
<td>{{totalPrice()}}</td>
<td>
<button class="btn btn-danger" ng-click="data=null">清空購物車</button>
</td>
</tr>
4. 增加和刪除商品數(shù)量
/*增加商品數(shù)量*/
$scope.add=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
item.quantity++;
}
/*減少商品數(shù)量
* @description:
* 減少當(dāng)前項(xiàng)的數(shù)量,當(dāng)僅剩一件時彈出對話框確認(rèn)是否清空。是則調(diào)用removeItem方法清除當(dāng)前項(xiàng)
* */
$scope.reduce=function(id){
var index=findIndex($scope,id),
item=$scope.data[index];
if(item.quantity>1){ //判斷當(dāng)前項(xiàng)是否還能再減
item.quantity--;
}else{
var makesure=confirm('確定要清空當(dāng)前項(xiàng)嗎??');
if(makesure){
$scope.removeItem(id)
}
}
}
總結(jié):
此demo主要利用了angular的數(shù)據(jù)雙向綁定特性,從而大大的簡化了操作。
關(guān)鍵點(diǎn):
1)、angular.forEach(obj,fn(value,key,obj)) 迭代器
2)、ng-click指令綁定點(diǎn)擊事件。使用ng-click會自動觸發(fā)angular的臟檢查機(jī)制從而實(shí)時的更新視圖
3)、ng-repeat指令遍歷數(shù)據(jù),渲染頁面
4)、ng-show指令:通過其值來判斷是否顯示(原理是給元素加nghide類名)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Angularjs和bootstrap實(shí)現(xiàn)購物車功能
- 使用Angular.js實(shí)現(xiàn)簡單的購物車功能
- Angularjs 制作購物車功能實(shí)例代碼
- Angular實(shí)現(xiàn)購物車計算示例代碼
- AngularJS 購物車全選/取消全選功能的實(shí)現(xiàn)方法
- angular.js實(shí)現(xiàn)購物車功能
- angularjs實(shí)現(xiàn)簡單的購物車功能
- AngularJS 實(shí)現(xiàn)購物車全選反選功能
- AngularJs 終極購物車(實(shí)例講解)
- Angular動畫實(shí)現(xiàn)的2種方式以及添加購物車動畫實(shí)例代碼
相關(guān)文章
angular.JS實(shí)現(xiàn)網(wǎng)頁禁用調(diào)試、復(fù)制和剪切
這篇文章主要給大家介紹了angular.JS實(shí)現(xiàn)網(wǎng)頁禁用調(diào)試、復(fù)制和剪切的相關(guān)資料,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解 (上)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。2017-07-07
Angular5整合富文本編輯器TinyMCE的方法(漢化+上傳)
TinyMCE是一個輕量級的富文本編輯器,相對于CK編輯器更加精簡,但必須滿足絕大部分場景的需要。這篇文章主要介紹了Angular5整合富文本編輯器TinyMCE(漢化+上傳)的相關(guān)知識,需要的朋友可以參考下2020-05-05
基于AngularJs + Bootstrap + AngularStrap相結(jié)合實(shí)現(xiàn)省市區(qū)聯(lián)動代碼
這篇文章主要給大家介紹基于AngularJs + Bootstrap + AngularStrap相結(jié)合實(shí)現(xiàn)省市區(qū)聯(lián)動的實(shí)例代碼,非常不錯具有參考借鑒價值,感興趣的朋友一起看看吧2016-05-05
Angular實(shí)現(xiàn)點(diǎn)擊按鈕后在上方顯示輸入內(nèi)容的方法
這篇文章主要介紹了Angular實(shí)現(xiàn)點(diǎn)擊按鈕后在上方顯示輸入內(nèi)容的方法,涉及AngularJS事件響應(yīng)及頁面元素屬性動態(tài)設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

