當前位置:首頁 » 網購平台 » h5xie購物車
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

h5xie購物車

發布時間: 2021-02-22 11:47:40

① 長城h5柴油車柴油箱放油螺絲在哪

一般的柴油車 柴油箱的放油螺絲都在油箱最底下啊

② angular js 帶復選框購物車怎麼寫

前段時間研究過這個,並且寫了一個購物車的小例子,今天一個偶然的機會提起,可惜忘了差不多了,糊里糊塗的也沒說清楚。翻出來,提醒下自己,保持一顆學習的心,順便再復習一遍。
先上一個最終的效果圖

構圖比較簡單,主要功能:
1. 點擊購買的時候 進行數量的增加或者條目的增加,同時總價格變化;
2. 進行刪除的時候,刪除當前條目,總價變化;
3. 進行數目增加減少的時候,總價格變化;
好,下面說代碼,抓耳撓腮的想想,有點久遠印象不太深刻了;
關於angular的基本用法,這里就不嘮叨了,網上好多的教程;
首先是商品列表,這里自己隨意列舉了一些

<script>
var items = [{
id : '1',
name : '蜂蜜',
price : 30.00
},{
id : '2',
name : '黃豆醬',
price : 15.8
},
{
id : '3',
name : '護手霜',
price : 15.00
},
{
id : '4',
name : '保溫杯',
price : 29.9
},
{
id : '5',
name : '滑鼠',
price : 39.9
},{
id : '6',
name : '米老頭',
price : 8.8
}];
//購物車中的數據;
var boughtList = {};
</script>

主要的html代碼,重新注釋下也讓自己再熟悉一遍

<div class="wrap" ng-controller="showItem"><!-- ng-controller ng的語法 -->
<h5>商品列表</h5>
<div class="left itembox border" >
<ul>
<li class="left" ng-repeat="value in items" item-id={{value.id}}>
<p>{{value.name}}</p>
<p> {{value.price}}</p>
<p>
<a href="javascript:void(0);" ng-click="buyAction($event);"
name={{value.name}} price={{value.price}} item-id={{value.id}} >購買</a>
<!-- dom 事件時的$event 就相當於普通dom事件中的window.event 對象-->
</p>
</li>
</ul>
</div>

<!-- 購物車中的數據 -->
<div class="boughtlist border">
<ul>
<li ng-repeat="value in boughtList" item-id={{value.id}}>
<span>{{value.name}}</span>
<span>{{value.price}}</span>
<span style="width:100px;" item-id={{value.id}}>
<a href="javascript:void(0);" ng-click="value.num=value.num+1;changeNum($event,value.num);" >+</a>
<input class="border" type="number" min=0 ng-model="value.num" ng-init="value.num=1" ng-change="changeNum(value.id,value.num);"/>
<!-- 這里的ng-change 是值發生變化時觸發的事件,其實這里我原先想處理成 一個自動的mvc過程,無果,只好加事件了-->
<a href="javascript:void(0);" ng-click="value.num=value.num-1;changeNum($event,value.num);">-</a>
</span>
<a href="javascript:void(0);" item-id={{value.id}} ng-click="delItem($event);" >刪除</a>
</li>
</ul>
<p ng-init=0 >總價格:{{ total | number:1}}</p>
<!-- angular的優勢體現,number:1也就是number數據,小數點後一位。-->
</div>

我記得,當時覺得比較麻煩的是 input沒有ng-blur事件;
看下js代碼

var ng = angular;
var myapp = ng.mole('myapp',[]);

var common = {
getTotal : function(total){ //每次重新清零 算出總價,這樣的話為什麼還要傳total參數?當時怎麼想的?
total = 0;
for(var k in boughtList){
if(boughtList[k]){
if(boughtList[k].num <=0){
boughtList[k].num = 0;
}
total += boughtList[k].num*boughtList[k].price;
}
}
return total;
}
}

myapp.controller('showItem',function($scope){
$scope.items = items;
$scope.boughtList = boughtList;
$scope.total = 0;
for(var k in boughtList){
if(boughtList[k]){
$scope.total += boughtList[k].num*boughtList[k].price;
}
}
$scope.buyAction = function($event){
var el = $event.target;
var id = el.getAttribute('item-id');
if(boughtList[id]){
boughtList[id].num += 1;
}else{
var arr = [];
arr.name = el.getAttribute('name');
arr.price = el.getAttribute('price');
arr.num = 1;
arr.id = id;
boughtList[id] = arr;
}
$scope.total = common.getTotal($scope.total);
}

$scope.delItem = function($event){
var li = $event.target.parentNode;
li.parentNode.removeChild(li);
var id = $event.target.getAttribute('item-id');
if(boughtList[id]){
delete boughtList[id];
}
$scope.total = common.getTotal($scope.total);
}
$scope.changeNum = function($event,num){
var id;
if(typeof $event == 'string'){
id = $event;
}else{
id = $event.target.parentNode.getAttribute('item-id');
}

boughtList[id].number = num;
$scope.total = common.getTotal($scope.total);
}
});