哈嘍,大家好,快半個月沒寫了,現在提筆都有點生硬了,一直沒更新的原因,一個是代碼君也要上班,加上最近工作比較忙,還有就是寫文章一直未被認可,所以沒什么動力再創作了,那時真的堅持不下去,打算放棄了,感謝讀者曹明,一個韓國的研究生讀者,支持我,并給我鼓勵,期待我更新下一篇,我非常感動,瞬間戰斗力恢復,其實你們簡單的點贊,評論,都是給我最大的支持,好了,煽情完畢,該講今天的重點了,購物車,購物車的界面實現到不是很難,難點是處理里面的邏輯,無論是小程序,還是APP,購物車的邏輯都是最難的,下面開始教大家如何實現購物車了,先上效果圖
購物車實現
cart.wxml
<import src="/template/quantity/index.wxml" /><scroll-view class="scroll" scroll-y="true"> <view class="separate"></view> <view wx:for="{{carts}}"> <view class="cart_container"> <image class="item-select" bindtap="switchSelect" data-index="{{index}}" data-id="{{index}}" src="{{item.isSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" /> <image class="item-image" src="{{item.pic}}"></image> <view class="column"> <text class="title">{{item.name}}</text> <view class="row"> <text class="sku-price">¥</text> <text class="sku-price">{{item.price}}</text> <view class="sku"> <template is="quantity" data="{{ ...item.count, componentId: index }}" /> </view> </view> </view> </view> <view class="separate"></view> </view></scroll-view><view class="bottom_total"> <view class="bottom_line"></view> <view class="row"> <image class="item-allselect" bindtap="allSelect" src="{{isAllSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" /> <text class="small_text">全選</text> <text>合計:¥ </text> <text class="price">{{totalMoney}}</text> <button class="button-red" bindtap="toBuy" formType="submit">去結算</button> </view></view>
布局不是很復雜,一個循環列表,循環出購物車商品,外加一個結算的底部控件,還需要提醒的是,循環列表外面要加一層scroll-view,這樣當數據很多是時候,可以滾動,不熟悉scroll-view的,請自行翻看前面幾篇文章,里面有講解
cat.wxss
/* pages/cart/cart.wxss */.cart_container { display: flex; flex-direction: row;}.scroll { margin-bottom: 120rpx;}.column { display: flex; flex-direction: column;}.row { display: flex; flex-direction: row; align-items: center;}.sku { margin-top: 60rpx; margin-left: 100rpx;}.sku-price { color: red; position: relative; margin-top: 70rpx;}.price { color: red; position: relative;}.title { font-size: 38rpx; margin-top: 40rpx;}.small_text { font-size: 28rpx; margin-right: 40rpx; margin-left: 10rpx;}.item-select { width: 40rpx; height: 40rpx; margin-top: 90rpx; margin-left: 20rpx;}.item-allselect { width: 40rpx; height: 40rpx; margin-left: 20rpx;}.item-image { width: 180rpx; height: 180rpx; margin: 20rpx;}.bottom_line { width: 100%; height: 2rpx; background: lightgray;}.bottom_total { position: fixed; display: flex; flex-direction: column; bottom: 0; width: 100%; height: 120rpx; line-height: 120rpx; background: white;}.button-red { background-color: #f44336; /* 紅色 */}button { position: fixed; right: 0; color: white; text-align: center; display: inline-block; font-size: 30rpx; border-radius: 0rpx; width: 30%; height: 120rpx; line-height: 120rpx;}
wxss樣式沒什么可說的,了解其屬性,調用class就好,重點說一下cart.js,全篇的邏輯都在這里面
cart.js
// pages/cart/cart.jsvar Temp = require('../../template/contract.js');Page(Object.assign({}, Temp.Quantity, { data: { isAllSelect:false, totalMoney:0, // 商品詳情介紹 carts: [ { pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148058328876.jpg", name:"日本資生堂洗顏", price:200, isSelect:false, // 數據設定 count: { quantity: 2, min: 1, max: 20 }, }, { pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058301941.jpg', name: "倩碧煥妍活力精華露", price: 340, isSelect: false, // 數據設定 count: { quantity: 1, min: 1, max: 20 }, }, { pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg', name: "特效潤膚露", price: 390, isSelect: false, // 數據設定 count: { quantity: 3, min: 1, max: 20 }, }, { pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058228431.jpg', name: "倩碧水嫩保濕精華面霜", price: 490, isSelect: false, // 數據設定 count: { quantity: 1, min: 1, max: 20 }, }, { pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg', name: "蘭蔻清瑩柔膚爽膚水", price: 289, isSelect: false, // 數據設定 count: { quantity: 10, min: 1, max: 20 }, }, { pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148057921620_middle.jpg", name: "LANCOME蘭蔻小黑瓶精華", price: 230, isSelect: false, // 數據設定 count: { quantity: 1, min: 1, max: 20 }, }, ], }, //勾選事件處理函數 switchSelect: function (e) { // 獲取item項的id,和數組的下標值 var Allprice = 0,i=0; let id = e.target.dataset.id, index = parseInt(e.target.dataset.index); this.data.carts[index].isSelect = !this.data.carts[index].isSelect; //價錢統計 if (this.data.carts[index].isSelect) { this.data.totalMoney = this.data.totalMoney + this.data.carts[index].price; } else { this.data.totalMoney = this.data.totalMoney - this.data.carts[index].price; } //是否全選判斷 for (i = 0; i < this.data.carts.length; i++) { Allprice = Allprice + this.data.carts[i].price; } if (Allprice == this.data.totalMoney) { this.data.isAllSelect=true; } else { this.data.isAllSelect = false; } this.setData({ carts: this.data.carts, totalMoney: this.data.totalMoney, isAllSelect: this.data.isAllSelect, }) }, //全選 allSelect: function (e) { //處理全選邏輯 let i = 0; if (!this.data.isAllSelect) { for (i = 0; i < this.data.carts.length; i++) { this.data.carts[i].isSelect = true; this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price; } } else { for (i = 0; i < this.data.carts.length; i++) { this.data.carts[i].isSelect = false; } this.data.totalMoney=0; } this.setData({ carts: this.data.carts, isAllSelect: !this.data.isAllSelect, totalMoney: this.data.totalMoney, }) }, // 去結算 toBuy() { wx.showToast({ title: '去結算', icon: 'success', duration: 3000 }); this.setData({ showDialog: !this.data.showDialog }); }, //數量變化處理 handleQuantityChange(e) { var componentId = e.componentId; var quantity = e.quantity; this.data.carts[componentId].count.quantity = quantity; this.setData({ carts: this.data.carts, }); }}));
介紹一下用到的參數
switchSelect 勾選按鈕需要做的邏輯處理
allSelect 全選按鈕的邏輯處理
微信小程序數據處理
一、修改數據方式
data:{ name:'我是初始化的name'}
1、this.data.name
this.data.name='我是代碼君data'
2、this.setData
this.setData({ name:'我是代碼君setData' })
這兩種方式都可以改變數據,this.setData的好處是可以有刷新的效果,即實時更新數據
二、修改對象數組
data:{person:{ name:'代碼君', city:'廈門'}}
修改全部對象
this.setData({ person:{ name:'新代碼君', city:'湖南' } })
修改部分數據
this.setData({ 'person.name': '代碼君只修改名字' })//多個數組用這個this.setData({ 'person[0].name': '代碼君只修改名字' })
三、添加刪除數據
1、添加數據concat
//假設這一段是我們要新增的數組var newarray = [{ name:'增加的數據--'+new Date().getTime() ,}];//向前--用newarray與this.data.list合拼this.data.list = newarray.concat(this.data.list);//向后--用this.data.list與newarray合拼this.data.list = this.data.list.concat(newarray);
2、刪除數據splice()刪除數據,然后返回被刪除的數據
//刪除 remove:function (e){ var dataset = e.target.dataset; var Index = dataset.index; //通過index識別要刪除第幾條數據,第二個數據為要刪除的項目數量,通常為1 this.data.list.splice(Index,1); //渲染數據 this.setData({ list:this.data.list }); }
3、清空數據
//清空 clear:function (){ //其實就是讓數組變成一個空數組即可 this.setData({ list:{} }); }
總結
今天主要講解js是如何處理數據邏輯的,也講解了數據的增刪改查,這是必備的知識項,回去要多多練習。好了今天就講到這,祝大家周末愉快~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答