WeChat applet shopping cart implementation code
This article mainly introduces the implementation code example of the shopping cart in the WeChat Mini Program Practical Chapter. It introduces the functional implementation of the shopping cart in detail. It has certain reference value. Those who are interested can learn about it. I hope it can help everyone.
The interface of the shopping cart is not difficult to implement. The difficulty is to deal with the logic inside. Whether it is a small program or an APP, the logic of the shopping cart is the most difficult. Now I will teach you how to implement the shopping cart. , first the renderings:
Shopping cart implementation
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>
The layout is not very complicated. It is a circular list that loops out the shopping cart items, plus a bottom control for settlement. It should also be reminded that a layer of scroll-view should be added outside the circular list, so that when there is a lot of data , you can scroll. If you are not familiar with scroll-view, please read the previous articles by yourself, which have explanations
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; }
// pages/cart/cart.js var 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, }); } }));
- isAllSelect: Whether to select all
- totalMoney:Total amount
- carts: shopping cart product data
- Determine whether all are checked. If all are checked, the select all button at the bottom should light up. The basis for judgment is whether the price is equal to the total price. Of course, this is only a way of judging. Readers can also judge by the number of checks.
- For the checked or canceled buttons, perform addition and subtraction calculations of the total price
- this.setData, update the data, this is the key point, Every time after processing the data, remember to update the data
- To select all, check each item The selected icon lights up, and then the total price is calculated. If not all are selected, it will be grayed out. The total price is 0
- this.setData updates the data
WeChat applet data processing
1. Modify data method
data:{ name:'我是初始化的name' }
this.data.name='我是代码君data'
this.setData({ name:'我是代码君setData' })
2. Modify the object array
##
data:{ person:{ name:'代码君', city:'厦门' } }
Modify all objects
this.setData({ person:{ name:'新代码君', city:'湖南' } })
Modify some data
this.setData({ 'person.name': '代码君只修改名字' }) //多个数组用这个 this.setData({ 'person[0].name': '代码君只修改名字' })
三, add and delete data
1, add data 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, delete data splice() deletes the data, and then returns the Deleted data
//删除 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 data
//清空 clear:function (){ //其实就是让数组变成一个空数组即可 this.setData({ list:{} }); }
Summary
Today I mainly explain how js handles data logic, and also explains the addition, deletion, modification and query of data. This is a necessary knowledge item. You need to practice more when you go back. Well, that’s it for today, I wish you all a happy weekend~
Related recommendations:
The above is the detailed content of WeChat applet shopping cart implementation code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Some users encountered errors when installing the device, prompting error code 28. In fact, this is mainly due to the driver. We only need to solve the problem of win7 driver code 28. Let’s take a look at what should be done. Do it. What to do with win7 driver code 28: First, we need to click on the start menu in the lower left corner of the screen. Then, find and click the "Control Panel" option in the pop-up menu. This option is usually located at or near the bottom of the menu. After clicking, the system will automatically open the control panel interface. In the control panel, we can perform various system settings and management operations. This is the first step in the nostalgia cleaning level, I hope it helps. Then we need to proceed and enter the system and

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

The win10 system is a very excellent high-intelligence system. Its powerful intelligence can bring the best user experience to users. Under normal circumstances, users’ win10 system computers will not have any problems! However, it is inevitable that various faults will occur in excellent computers. Recently, friends have been reporting that their win10 systems have encountered frequent blue screens! Today, the editor will bring you solutions to different codes that cause frequent blue screens in Windows 10 computers. Let’s take a look. Solutions to frequent computer blue screens with different codes each time: causes of various fault codes and solution suggestions 1. Cause of 0×000000116 fault: It should be that the graphics card driver is incompatible. Solution: It is recommended to replace the original manufacturer's driver. 2,

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

Termination Code 0xc000007b While using your computer, you sometimes encounter various problems and error codes. Among them, the termination code is the most disturbing, especially the termination code 0xc000007b. This code indicates that an application cannot start properly, causing inconvenience to the user. First, let’s understand the meaning of termination code 0xc000007b. This code is a Windows operating system error code that usually occurs when a 32-bit application tries to run on a 64-bit operating system. It means it should

How to implement a simple shopping cart function in Java? The shopping cart is an important feature of an online store, which allows users to add items they want to purchase to the shopping cart and manage the items. In Java, we can implement a simple shopping cart function by using object-oriented approach. First, we need to define a product category. This class contains attributes such as product name, price, and quantity, as well as corresponding Getter and Setter methods. For example: publicclassProduct

Blue screen is a problem we often encounter when using the system. Depending on the error code, there will be many different reasons and solutions. For example, when we encounter the problem of stop: 0x0000007f, it may be a hardware or software error. Let’s follow the editor to find out the solution. 0x000000c5 blue screen code reason: Answer: The memory, CPU, and graphics card are suddenly overclocked, or the software is running incorrectly. Solution 1: 1. Keep pressing F8 to enter when booting, select safe mode, and press Enter to enter. 2. After entering safe mode, press win+r to open the run window, enter cmd, and press Enter. 3. In the command prompt window, enter "chkdsk /f /r", press Enter, and then press the y key. 4.

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button
