小程序?qū)崿F(xiàn)購(gòu)物車(chē)完整版
小程序?qū)崿F(xiàn)完整購(gòu)物車(chē)[全選/反選計(jì)算金額/加減計(jì)算數(shù)量跟金額],供大家參考,具體內(nèi)容如下
一、wxml頁(yè)面代碼模塊
<view wx:if="{{hasList}}">
<view class="order_list">
<view class="order" wx:for="{{list}}" wx:key="{{index}}">
<view class="xuanze" wx:if="{{item.selected}}" catchtap="selectList" data-index="{{index}}">
<image src="/images/serch/xuanze.png" />
</view>
<view class="xuanze" catchtap="selectList" data-index="{{index}}" wx:else>
<image src="/images/serch/gouxuan.png" />
</view>
<!--列表商品圖片-->
<view class="order_img">
<image src="{{item.image}}" />
</view>
<view class="order_text">
<view class="text_top">
<!--列表標(biāo)題-->
<view class="title">{{item.title}}</view>
<view class="detel" catchtap="deletes" data-index="{{index}}">
<image src="/images/serch/detel.png" />
</view>
</view>
<!--規(guī)格-->
<view class="size">規(guī)格:{{item.pro_name}}</view>
<view class="text_bottom">
<!--價(jià)格-->
<view class="money">¥{{item.price}}</view>
<!--商品數(shù)量加減-->
<view class="number">
<!--減按鈕-->
<view class="reduce" catchtap="btn_minus" data-obj="{{obj}}" data-index="{{index}}">
<!--按鈕圖片-->
<image src="/images/serch/jian-1.png" />
</view>
<!--數(shù)量-->
<view class="numb">{{item.num}}</view>
<!--加按鈕-->
<view class="add" catchtap="btn_add" data-index="{{index}}">
<!--按鈕圖片-->
<image src="/images/serch/add-1.png" />
</view>
</view>
</view>
</view>
</view>
</view>
<!--固定底部-->
<view class="buy">
<view class="buy_top">
<view class="top_left">
<view class="left_img" catchtap="selectAll" wx:if="{{selectAllStatus}}">
<image src="/images/serch/gouxuan.png" />
</view>
<view class="left_img" catchtap="selectAll" wx:else>
<image src="/images/serch/gouxuan.png" />
</view>
<view class="left_name">全選</view>
</view>
<view class="top_left">
<view class="left_img">
<image src="/images/serch/fenxiang.png" />
</view>
<view class="left_name">分享</view>
</view>
</view>
<view class="buy_bottom">
<view class="buy_left">
<view class="heji">合計(jì):¥{{totalPrice}}</view>
</view>
<view class="buy_right">
<!--提交訂單-->
<view class="liji " catchtap="btn_submit_order">立即購(gòu)買(mǎi)</view>
<view class="liji two active">預(yù)約試衣</view>
</view>
</view>
</view>
</view>
<!--購(gòu)物車(chē)沒(méi)訂單-->
<view wx:else>
<view class="list_none">購(gòu)物車(chē)是空的哦~</view>
</view>
二、樣式代碼
page {
background-color: rgba(238, 238, 238, 0.5);
}
.order {
height: 238rpx;
background-color: #fefeff;
margin: 27rpx;
border-radius: 4rpx;
display: flex;
align-items: center;
}
.xuanze {
width: 40rpx;
height: 40rpx;
/* background-color: darkgoldenrod; */
border-radius: 50%;
margin: 0 11rpx;
}
.xuanze image {
width: 100%;
height: 100%;
display: block;
border-radius: 50%;
}
.order_img {
width: 180rpx;
height: 180rpx;
}
.order_img image {
width: 100%;
height: 100%;
display: block;
}
.order_text {
margin-left: 20rpx;
width: 58%;
height: 180rpx;
}
.text_top {
display: flex;
justify-content: space-between;
align-items: center;
}
.title {
width: 70%;
font-size: 28rpx;
color: #4b5248;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
overflow: hidden;
}
.detel {
width: 30rpx;
height: 30rpx;
}
.detel image {
width: 100%;
height: 100%;
display: block;
}
.size {
font-size: 24rpx;
color: #a8ada6;
}
.text_bottom {
display: flex;
margin-top: 50rpx;
align-items: center;
justify-content: space-between;
}
.money {
font-size: 30rpx;
color: #a5937f;
}
.number {
display: flex;
justify-content: space-around;
align-items: center;
width: 170rpx;
}
.reduce {
width: 46rpx;
height: 46rpx;
}
.reduce image {
width: 100%;
height: 100%;
display: block;
}
.numb {
font-size: 30rpx;
color: #a5937f;
}
.add {
width: 46rpx;
height: 46rpx;
}
.add image {
width: 100%;
height: 100%;
display: block;
}
/*購(gòu)買(mǎi)按鈕*/
.buy {
height: 180rpx;
width: 696rpx;
position: fixed;
left: 27rpx;
bottom: 41rpx;
background-color: #555555f3;
border-radius: 4rpx;
}
.buy_top {
border-bottom: 1px solid rgb(98, 98, 99);
height: 75rpx;
display: flex;
align-items: center;
justify-content: space-between;
}
.top_left {
display: flex;
align-items: center;
}
.left_img {
width: 37rpx;
height: 37rpx;
margin: 11rpx;
}
.left_img image {
width: 100%;
height: 100%;
display: block;
}
.left_name {
font-size: 24rpx;
color: #fefeff;
margin-right: 29rpx;
}
.buy_bottom {
display: flex;
height: 104rpx;
justify-content: space-between;
align-items: center;
padding: 0rpx 30rpx 0rpx 12rpx;
}
.buy_left {
font-size: 26rpx;
color: #fff;
}
.buy_right {
display: flex;
align-items: center;
}
.liji {
width: 180rpx;
height: 70rpx;
border: 2rpx solid rgba(248, 248, 248, 1);
box-sizing: border-box;
border-radius: 4rpx;
line-height: 70rpx;
text-align: center;
font-size: 26rpx;
color: #FEFEFF;
}
.two{
margin-left: 12rpx;
}
.active{
background-color: #A5937F;
border: none;
}
三、js代碼模塊
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
hasList: true, //默認(rèn)展示列表數(shù)據(jù)
//商品列表數(shù)據(jù)
list: [{
id: 1,
title: '園藝大師抗皺精華露',
image: '/images/serch/2.png',
pro_name: "30ml",
num: 1,
price: 180,
selected: true
},
{
id: 2,
title: '伊芙琳玫瑰護(hù)手霜',
image: '/images/serch/1.png',
pro_name: "25g",
num: 1,
price: 62,
selected: true
},
{
id: 2,
title: '燕麥山羊乳舒緩護(hù)手霜',
image: '/images/serch/2.png',
pro_name: "75ml",
num: 1,
price: 175,
selected: true
}
],
//金額
totalPrice: 0, //總價(jià),初始為0
//全選狀態(tài)
selectAllStatus: true, // 全選狀態(tài),默認(rèn)全選
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function(options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function() {
wx.showToast({
title: '加載中',
icon: "loading",
duration: 1000
})
// 價(jià)格方法
this.count_price();
},
/** 當(dāng)前商品選中事件 */
selectList(e) {
var that = this;
//獲取選中的 radio索引
var index = e.currentTarget.dataset.index;
//獲取到商品列表數(shù)據(jù)
var list = that.data.list;
//默認(rèn)全選
that.data.selectAllStatus = true;
//循環(huán)數(shù)組數(shù)據(jù),判斷--選中/未選中[selected]
list[index].selected = !list[index].selected;
//如果數(shù)組數(shù)據(jù)全部為selected[true],全選
for (var i = list.length - 1; i >= 0; i--) {
if (!list[i].selected) {
that.data.selectAllStatus = false;
break;
}
}
// 重新渲染數(shù)據(jù)
that.setData({
list: list,
selectAllStatus: that.data.selectAllStatus
})
// 調(diào)用計(jì)算金額方法
that.count_price();
},
// 刪除
deletes(e) {
var that = this;
// 獲取索引
const index = e.currentTarget.dataset.index;
// 獲取商品列表數(shù)據(jù)
let list = this.data.list;
wx.showModal({
title: '提示',
content: '確認(rèn)刪除嗎',
success: function(res) {
if (res.confirm) {
// 刪除索引從1
list.splice(index, 1);
// 頁(yè)面渲染數(shù)據(jù)
that.setData({
list: list
});
// 如果數(shù)據(jù)為空
if (!list.length) {
that.setData({
hasList: false
});
} else {
// 調(diào)用金額渲染數(shù)據(jù)
that.count_price();
}
} else {
console.log(res);
}
},
fail: function(res) {
console.log(res);
}
})
},
/** 購(gòu)物車(chē)全選事件 */
selectAll(e) {
// 全選ICON默認(rèn)選中
let selectAllStatus = this.data.selectAllStatus;
// true ----- false
selectAllStatus = !selectAllStatus;
// 獲取商品數(shù)據(jù)
let list = this.data.list;
// 循環(huán)遍歷判斷列表中的數(shù)據(jù)是否選中
for (let i = 0; i < list.length; i++) {
list[i].selected = selectAllStatus;
}
// 頁(yè)面重新渲染
this.setData({
selectAllStatus: selectAllStatus,
list: list
});
// 計(jì)算金額方法
this.count_price();
},
/** 綁定加數(shù)量事件 */
btn_add(e) {
// 獲取點(diǎn)擊的索引
const index = e.currentTarget.dataset.index;
// 獲取商品數(shù)據(jù)
let list = this.data.list;
// 獲取商品數(shù)量
let num = list[index].num;
// 點(diǎn)擊遞增
num = num + 1;
list[index].num = num;
// 重新渲染 ---顯示新的數(shù)量
this.setData({
list: list
});
// 計(jì)算金額方法
this.count_price();
},
/**
* 綁定減數(shù)量事件
*/
btn_minus(e) {
// // 獲取點(diǎn)擊的索引
const index = e.currentTarget.dataset.index;
// const obj = e.currentTarget.dataset.obj;
// console.log(obj);
// 獲取商品數(shù)據(jù)
let list = this.data.list;
// 獲取商品數(shù)量
let num = list[index].num;
// 判斷num小于等于1 return; 點(diǎn)擊無(wú)效
if (num <= 1) {
return false;
}
// else num大于1 點(diǎn)擊減按鈕 數(shù)量--
num = num - 1;
list[index].num = num;
// 渲染頁(yè)面
this.setData({
list: list
});
// 調(diào)用計(jì)算金額方法
this.count_price();
},
// 提交訂單
btn_submit_order() {
var that = this;
console.log(that.data.totalPrice);
// 調(diào)起支付
// wx.requestPayment(
// {
// 'timeStamp': '',
// 'nonceStr': '',
// 'package': '',
// 'signType': 'MD5',
// 'paySign': '',
// 'success': function (res) { },
// 'fail': function (res) { },
// 'complete': function (res) { }
// })
wx.showModal({
title: '提示',
content: '合計(jì)金額-' + that.data.totalPrice + "暫未開(kāi)發(fā)",
})
},
/**
* 計(jì)算總價(jià)
*/
count_price() {
// 獲取商品列表數(shù)據(jù)
let list = this.data.list;
// 聲明一個(gè)變量接收數(shù)組列表price
let total = 0;
// 循環(huán)列表得到每個(gè)數(shù)據(jù)
for (let i = 0; i < list.length; i++) {
// 判斷選中計(jì)算價(jià)格
if (list[i].selected) {
// 所有價(jià)格加起來(lái) count_money
total += list[i].num * list[i].price;
}
}
// 最后賦值到data中渲染到頁(yè)面
this.setData({
list: list,
totalPrice: total.toFixed(2)
});
},
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 購(gòu)物車(chē)簡(jiǎn)單實(shí)例
- 微信小程序?qū)崙?zhàn)篇之購(gòu)物車(chē)的實(shí)現(xiàn)代碼示例
- 微信小程序?qū)崿F(xiàn)多選框全選與反全選及購(gòu)物車(chē)中刪除選中的商品功能
- 微信小程序?qū)崿F(xiàn)購(gòu)物車(chē)代碼實(shí)例詳解
- 微信小程序之購(gòu)物車(chē)功能
- 微信小程序?qū)崿F(xiàn)購(gòu)物車(chē)功能
- 微信小程序購(gòu)物車(chē)、父子組件傳值及calc的注意事項(xiàng)總結(jié)
- 小程序二次貝塞爾曲線實(shí)現(xiàn)購(gòu)物車(chē)商品曲線飛入效果
- 微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車(chē)商品刪除功能
- Python實(shí)現(xiàn)購(gòu)物車(chē)購(gòu)物小程序
相關(guān)文章
JavaScript其他類(lèi)型的值轉(zhuǎn)換為布爾值的規(guī)則詳解
在JavaScript中,所有的值在邏輯上下文中都會(huì)被轉(zhuǎn)換為布爾類(lèi)型(Boolean),理解這些轉(zhuǎn)換規(guī)則對(duì)開(kāi)發(fā)者來(lái)說(shuō)至關(guān)重要,尤其是在條件判斷、短路運(yùn)算、邏輯非(!)等場(chǎng)景下,正確掌握布爾轉(zhuǎn)換可以避免潛在的bug,本文將詳細(xì)介紹 JavaScript 其他類(lèi)型的值是如何轉(zhuǎn)換為布爾值的2025-04-04
前端使用crypto.js進(jìn)行加密的函數(shù)代碼
最近在使用Cookies加密保存數(shù)據(jù)的時(shí)候,接觸到crypto,使用還算簡(jiǎn)單,在這里記錄一下2020-08-08
ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用示例
這篇文章主要介紹了ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用,結(jié)合實(shí)例形式分析了ES6函數(shù)數(shù)組參數(shù)解構(gòu)賦值和默認(rèn)值的設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
vue(javaScript)操作字符串的一些常用方法總結(jié)
在平時(shí)前端開(kāi)發(fā)中,我們不難發(fā)現(xiàn)經(jīng)常會(huì)用到字符串操作,這篇文章主要給大家介紹了關(guān)于vue(javaScript)操作字符串的一些常用方法的相關(guān)資料,需要的朋友可以參考下2024-01-01
JS?for?in遍歷對(duì)象順序不對(duì)解決辦法
最近使用for-in語(yǔ)句遍歷對(duì)象屬性時(shí)發(fā)現(xiàn)遍歷順序并非屬性構(gòu)建順序,這篇文章主要給大家介紹了關(guān)于JS?for?in遍歷對(duì)象順序不對(duì)的解決辦法,需要的朋友可以參考下2023-11-11
js實(shí)現(xiàn)5秒倒計(jì)時(shí)重新發(fā)送短信功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)5秒倒計(jì)時(shí)重新發(fā)送短信功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
JS控制靜態(tài)頁(yè)面之間傳遞參數(shù)獲取參數(shù)并應(yīng)用的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇JS控制靜態(tài)頁(yè)面之間傳遞參數(shù)獲取參數(shù)并應(yīng)用的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
JS實(shí)現(xiàn)快速比較兩個(gè)字符串中包含有相同數(shù)字的方法
這篇文章主要介紹了JS實(shí)現(xiàn)快速比較兩個(gè)字符串中包含有相同數(shù)字的方法,涉及javascript字符串的遍歷、排序、比較等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

