国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Preface
Requirements
Summary
Home WeChat Applet WeChat Development Implementation of shopping cart function in WeChat mini program

Implementation of shopping cart function in WeChat mini program

Jul 03, 2020 am 11:15 AM
WeChat applet

Preface

In the past, shopping carts were basically implemented through a large number of DOM operations. The WeChat applet is actually very similar to the usage of vue.js. Next, let’s see how the applet can implement the shopping cart function.

Requirements

First, let’s figure out the needs of the shopping cart.

  • Single selection, all selection and cancellation, and the total price will be calculated according to the selected products

  • The purchase quantity of a single product increases and Reduce

  • Delete items. When the shopping cart is empty, the page will change to the layout of the empty shopping cart

According to the design drawing, we can first implement a static page. Next, let’s look at what kind of data a shopping cart needs.

  • First is a product list (carts). The items in the list need: product image (image), product name (title), unit price (price), quantity (num), Whether it is selected (selected), product id (id)

  • and then select all in the lower left corner, a field (selectAllStatus) is required to indicate whether all are selected

  • The total price in the lower right corner (totalPrice)

  • Finally, you need to know whether the shopping cart is empty (hasList)

I know what I need. Data, we define these first when the page is initialized.

Code implementation

Initialization

Page({
????data:?{
????????carts:[],???????????????//?購物車列表
????????hasList:false,??????????//?列表是否有數(shù)據(jù)
????????totalPrice:0,???????????//?總價,初始為0
????????selectAllStatus:true????//?全選狀態(tài),默認全選
????},
????onShow()?{
????????this.setData({
??????????hasList:?true,????????//?既然有數(shù)據(jù)了,那設(shè)為true吧
??????????carts:[
????????????{id:1,title:'新鮮芹菜?半斤',image:'/image/s5.png',num:4,price:0.01,selected:true},
????????????{id:2,title:'素米?500g',image:'/image/s6.png',num:1,price:0.03,selected:true}
??????????]
????????});
??????},
})

We usually get the shopping cart list data by requesting the server, so we assign values ??to carts in the life cycle function. I thought about getting the latest status of the shopping cart every time I enter the shopping cart, and onLoad and onReady are only executed once during initialization, so I need to put the request in the onShow function. (Let’s pretend to be some fake data here)

Layout wxml

Repair the static page written before and bind the data.

?<view class="cart-box">
????<!-- wx:for 渲染購物車列表 -->
????<view wx:for="{{carts}}">
????
????????<!-- wx:if 是否選擇顯示不同圖標 -->
????????<icon wx:if="{{item.selected}}" type="success" color="red" bindtap="selectList" data-index="{{index}}" />
????????<icon wx:else type="circle" bindtap="selectList" data-index="{{index}}"/>
????????
????????<!-- 點擊商品圖片可跳轉(zhuǎn)到商品詳情 -->
????????<navigator url="../details/details?id={{item.id}}">
????????????<image class="cart-thumb" src="{{item.image}}"></image>
????????</navigator>
????????
????????<text>{{item.title}}</text>
????????<text>¥{{item.price}}</text>
????????
????????<!-- 增加減少數(shù)量按鈕 -->
????????<view>
????????????<text bindtap="minusCount" data-index="{{index}}">-</text>
????????????<text>{{item.num}}</text>
????????????<text bindtap="addCount" data-index="{{index}}">+</text>
????????</view>
????????
????????<!-- 刪除按鈕 -->
????????<text bindtap="deleteList" data-index="{{index}}">?×?</text>
????</view>
</view>

<!-- 底部操作欄 -->
<view>
????<!-- wx:if 是否全選顯示不同圖標 -->
????<icon wx:if="{{selectAllStatus}}" type="success_circle" color="#fff" bindtap="selectAll"/>
????<icon wx:else type="circle" color="#fff" bindtap="selectAll"/>
????<text>全選</text>
????
????<!-- 總價 -->
????<text>¥{{totalPrice}}</text>
</view>

Calculate the total price

The total price = the price of the selected product 1* the price of the selected product 2* the quantity...
According to the formula, you can get

getTotalPrice()?{
????let?carts?=?this.data.carts;??????????????????//?獲取購物車列表
????let?total?=?0;
????for(let?i?=?0;?i<carts.length;?i++)?{?????????//?循環(huán)列表得到每個數(shù)據(jù)
????????if(carts[i].selected)?{???????????????????//?判斷選中才會計算價格
????????????total?+=?carts[i].num?*?carts[i].price;?????//?所有價格加起來
????????}
????}
????this.setData({????????????????????????????????//?最后賦值到data中渲染到頁面
????????carts:?carts,
????????totalPrice:?total.toFixed(2)
????});
}

Other operations on the page that will cause the total price to change need to call this method.

Selection event

It is selected when clicked, and becomes unselected when clicked again. This is actually changing the selected field. Pass data-index="{{index}}" to pass the index of the current product in the list array to the event.

<p>selectList(e) {<br> ? ?const index = e.currentTarget.dataset.index; ? ?// 獲取data- 傳進來的index<br> ? ?let carts = this.data.carts; ? ? ? ? ? ? ? ? ? ?// 獲取購物車列表<br> ? ?const selected = carts[index].selected; ? ? ? ? // 獲取當前商品的選中狀態(tài)<br> ? ?carts[index].selected = !selected; ? ? ? ? ? ? ?// 改變狀態(tài)<br> ? ?this.setData({<br> ? ? ? ?carts: carts<br> ? ?});<br> ? ?this.getTotalPrice(); ? ? ? ? ? ? ? ? ? ? ? ? ? // 重新獲取總價<br>}<br></p>

Select all events

Select all is to change the selected of each product according to the all-selected status selectAllStatus

<p>selectAll(e) {<br> ? ?let selectAllStatus = this.data.selectAllStatus; ? ?// 是否全選狀態(tài)<br> ? ?selectAllStatus = !selectAllStatus;<br> ? ?let carts = this.data.carts;<br><br> ? ?for (let i = 0; i < carts.length; i++) {<br> ? ? ? ?carts[i].selected = selectAllStatus; ? ? ? ? ? ?// 改變所有商品狀態(tài)<br> ? ?}<br> ? ?this.setData({<br> ? ? ? ?selectAllStatus: selectAllStatus,<br> ? ? ? ?carts: carts<br> ? ?});<br> ? ?this.getTotalPrice(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 重新獲取總價<br>}<br></p>

Increase or decrease the quantity

Click number, Add 1 to num, click the - sign. If num > 1, then subtract 1

<p>// 增加數(shù)量<br>addCount(e) {<br> ? ?const index = e.currentTarget.dataset.index;<br> ? ?let carts = this.data.carts;<br> ? ?let num = carts[index].num;<br> ? ?num = num + 1;<br> ? ?carts[index].num = num;<br> ? ?this.setData({<br> ? ? ?carts: carts<br> ? ?});<br> ? ?this.getTotalPrice();<br>},<br>// 減少數(shù)量<br>minusCount(e) {<br> ? ?const index = e.currentTarget.dataset.index;<br> ? ?let carts = this.data.carts;<br> ? ?let num = carts[index].num;<br> ? ?if(num <= 1){<br> ? ? ?return false;<br> ? ?}<br> ? ?num = num - 1;<br> ? ?carts[index].num = num;<br> ? ?this.setData({<br> ? ? ?carts: carts<br> ? ?});<br> ? ?this.getTotalPrice();<br>}<br></p>

Delete product

Click the delete button to delete the current element from the shopping cart list. After deletion, if the shopping cart is empty, change the shopping cart empty flag hasList to false

deleteList(e)?{
????const?index?=?e.currentTarget.dataset.index;
????let?carts?=?this.data.carts;
????carts.splice(index,1);??????????????//?刪除購物車列表里這個商品
????this.setData({
????????carts:?carts
????});
????if(!carts.length){??????????????????//?如果購物車為空
????????this.setData({
????????????hasList:?false??????????????//?修改標識為false,顯示購物車為空頁面
????????});
????}else{??????????????????????????????//?如果不為空
????????this.getTotalPrice();???????????//?重新計算總價格
????}???
}

Summary

Although the shopping cart function is relatively simple, there are still many knowledge points involved in the WeChat applet, which is suitable Newbies practice to master.

Recommended tutorial: "WeChat Mini Program"

The above is the detailed content of Implementation of shopping cart function in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Xianyu WeChat mini program officially launched Xianyu WeChat mini program officially launched Feb 10, 2024 pm 10:39 PM

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

Implement image filter effects in WeChat mini programs Implement image filter effects in WeChat mini programs Nov 21, 2023 pm 06:22 PM

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

What is the name of Xianyu WeChat applet? What is the name of Xianyu WeChat applet? Feb 27, 2024 pm 01:11 PM

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Use WeChat applet to achieve carousel switching effect Use WeChat applet to achieve carousel switching effect Nov 21, 2023 pm 05:59 PM

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the &lt;swiper&gt; tag to achieve the switching effect of the carousel. In this component, you can pass b

Implement image rotation effect in WeChat applet Implement image rotation effect in WeChat applet Nov 21, 2023 am 08:26 AM

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to

Implement the sliding delete function in WeChat mini program Implement the sliding delete function in WeChat mini program Nov 21, 2023 pm 06:22 PM

Implementing the sliding delete function in WeChat mini programs requires specific code examples. With the popularity of WeChat mini programs, developers often encounter problems in implementing some common functions during the development process. Among them, the sliding delete function is a common and commonly used functional requirement. This article will introduce in detail how to implement the sliding delete function in the WeChat applet and give specific code examples. 1. Requirements analysis In the WeChat mini program, the implementation of the sliding deletion function involves the following points: List display: To display a list that can be slid and deleted, each list item needs to include

See all articles