Simple example of shopping cart in WeChat mini program
Jun 22, 2018 pm 04:04 PMThis article mainly introduces relevant information about a simple example of the WeChat mini program shopping cart. Friends who need it can refer to
WeChat mini program. Here is a small demo that implements the shopping cart function. If you need this function Friends can refer to it.
Abstract: Add or subtract product quantity, summarize price, select all or none
Design ideas:
1. From the Internet Upload the array in the following Json data format 1. Shopping cart id: cid 2. Title title 3. Quantity num 4. Image address 5. Price price 6. Subtotal 7. Whether selected
is selected 2. Click Repeat If the marquee toggle operation is already selected, it will become unselected by clicking. On the contrary, the click will be based on index as the identifier instead of cid, which facilitates traversal
3. Select all operation The first click will select all, and then click again Click to unselect all, and the select all button itself will also follow the toggle transformation
4. Click the settlement button to take out the selected cid array for submission to the server through the network. Here is a toast as a demonstration of the result. .
5. Use stepper to perform addition and subtraction operations, also use index as the identifier, and write back the num value after clicking.
6. Layout, select all and align with the bottom of the checkout button, the shopping cart mall adaptive height, similar to Android weight.
Steps:
Initial data rendering
1.1 Layout and style sheet
The top is a product list, and the bottom It is a select all button and an immediate settlement button
The left part of the product list is the product thumbnail, the upper right is the product title, and the lower right is the product price and quantity. The product quantity uses WXStepper to implement addition and subtraction operations
js: Initialize a data source, which is often obtained from the network. For related interfaces, see: https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
Page({ data:{ carts: [ {cid:1008,title:'Zippo打火機(jī)',image:'https://img12.360buyimg.com/n7/jfs/t2584/348/1423193442/572601/ae464607/573d5eb3N45589898.jpg',num:'1',price:'198.0',sum:'198.0',selected:true}, {cid:1012,title:'iPhone7 Plus',image:'https://img13.360buyimg.com/n7/jfs/t3235/100/1618018440/139400/44fd706e/57d11c33N5cd57490.jpg',num:'1',price:'7188.0',sum:'7188.0',selected:true}, {cid:1031,title:'得力訂書(shū)機(jī)',image:'https://img10.360buyimg.com/n7/jfs/t2005/172/380624319/93846/b51b5345/5604bc5eN956aa615.jpg',num:'3',price:'15.0',sum:'45.0',selected:false}, {cid:1054,title:'康師傅妙芙蛋糕',image:'https://img14.360buyimg.com/n7/jfs/t2614/323/914471624/300618/d60b89b6/572af106Nea021684.jpg',num:'2',price:'15.2',sum:'30.4',selected:false}, {cid:1063,title:'英雄鋼筆',image:'https://img10.360buyimg.com/n7/jfs/t1636/60/1264801432/53355/bb6a3fd1/55c180ddNbe50ad4a.jpg',num:'1',price:'122.0',sum:'122.0',selected:true}, ] } })
Layout file
<view class="container carts-list"> <view wx:for="{{carts}}" class="carts-item" data-title="{{item.title}}" data-url="{{item.url}}" bindtap="bindViewTap"> <view> <image class="carts-image" src="{{item.image}}" mode="aspectFill"/> </view> <view class="carts-text"> <text class="carts-title">{{item.title}}</text> <view class="carts-subtitle"> <text class="carts-price">{{item.sum}}</text> <text>WXStepper</text> </view> </view> </view> </view>
Style sheet
/*外部容器*/ .container { display: flex; flex-direction: column; align-items: center; justify-content: space-between; box-sizing: border-box; } /*整體列表*/ .carts-list { display: flex; flex-direction: column; padding: 20rpx 40rpx; } /*每行單元格*/ .carts-item { display: flex; flex-direction: row; height:150rpx; /*width屬性解決標(biāo)題文字太短而縮略圖偏移*/ width:100%; border-bottom: 1px solid #eee; padding: 30rpx 0; } /*左部圖片*/ .carts-image { width:150rpx; height:150rpx; } /*右部描述*/ .carts-text { width: 100%; display: flex; flex-direction: column; justify-content: space-between; } /*右上部分標(biāo)題*/ .carts-title { margin: 10rpx; font-size: 30rpx; } /*右下部分價(jià)格與數(shù)量*/ .carts-subtitle { font-size: 25rpx; color:darkgray; padding: 0 20rpx; display: flex; flex-direction: row; justify-content:space-between; } /*價(jià)格*/ .carts-price { color: #f60; }
1.2 Integrate WXStepper
1.2.1 Copy component content
[2016-10-16]
Copy the content of stepper.wxss to cart.wxss
Copy the contents of stepper.wxml to cart.wxml
The difference from the previous single component is that the array minusStatuses needs to be defined here to correspond to each plus and minus button. Of course, there is no problem in merging it into carts.
?????? minusStatuses: ['disabled', 'disabled', 'normal', 'normal', 'disabled']
The original static character WXStepper is replaced with the following code
<view class="stepper"> <!-- 減號(hào) --> <text class="{{minusStatuses[index]}}" data-index="{{index}}" bindtap="bindMinus">-</text> <!-- 數(shù)值 --> <input type="number" bindchange="bindManual" value="{{item.num}}" /> <!-- 加號(hào) --> <text class="normal" data-index="{{index}}" bindtap="bindPlus">+</text> </view>
js code bindMinus and bindPlus are transformed as follows:
bindMinus: function(e) { var index = parseInt(e.currentTarget.dataset.index); var num = this.data.carts[index].num; // 如果只有1件了,就不允許再減了 if (num > 1) { num --; } // 只有大于一件的時(shí)候,才能normal狀態(tài),否則disable狀態(tài) var minusStatus = num <= 1 ? 'disabled' : 'normal'; // 購(gòu)物車(chē)數(shù)據(jù) var carts = this.data.carts; carts[index].num = num; // 按鈕可用狀態(tài) var minusStatuses = this.data.minusStatuses; minusStatuses[index] = minusStatus; // 將數(shù)值與狀態(tài)寫(xiě)回 this.setData({ carts: carts, minusStatuses: minusStatuses }); }, bindPlus: function(e) { var index = parseInt(e.currentTarget.dataset.index); var num = this.data.carts[index].num; // 自增 num ++; // 只有大于一件的時(shí)候,才能normal狀態(tài),否則disable狀態(tài) var minusStatus = num <= 1 ? 'disabled' : 'normal'; // 購(gòu)物車(chē)數(shù)據(jù) var carts = this.data.carts; carts[index].num = num; // 按鈕可用狀態(tài) var minusStatuses = this.data.minusStatuses; minusStatuses[index] = minusStatus; // 將數(shù)值與狀態(tài)寫(xiě)回 this.setData({ carts: carts, minusStatuses: minusStatuses }); },
The effect is as shown:
<!-- 復(fù)選框圖標(biāo) --> <icon wx:if="{{item.selected}}" type="success_circle" size="20" bindtap="bindCheckbox" data-index="{{index}}"/> <icon wx:else type="circle" size="20" bindtap="bindCheckbox" data-index="{{index}}"/>The check box is centered
/*復(fù)選框樣式*/ .carts-list icon { margin-top: 60rpx; margin-right: 20rpx; }Bind the click check box event to invert the selection state.
bindCheckbox: function(e) { /*綁定點(diǎn)擊事件,將checkbox樣式改變?yōu)檫x中與非選中*/ //拿到下標(biāo)值,以在carts作遍歷指示用 var index = parseInt(e.currentTarget.dataset.index); //原始的icon狀態(tài) var selected = this.data.carts[index].selected; var carts = this.data.carts; // 對(duì)勾選狀態(tài)取反 carts[index].selected = !selected; // 寫(xiě)回經(jīng)點(diǎn)擊修改后的數(shù)組 this.setData({ carts: carts }); }Rendering:
<view class="carts-footer"> <view bindtap="bindSelectAll"> <icon wx:if="{{selectedAllStatus}}" type="success_circle" size="20"/> <icon wx:else type="circle" size="20" /> <text>全選</text> </view> <view class="button">立即結(jié)算</view> </view>I used before, but I found that it was impossible to achieve dispersed alignment of all selected components and the settlement button, and it did not respond to the following styles
display: flex; flex-direction: row; justify-content: space-between;Style Table
/*底部按鈕*/ .carts-footer { width: 100%; height: 80rpx; display: flex; flex-direction: row; justify-content: space-between; } /*復(fù)選框*/ .carts-footer icon { margin-left: 20rpx; } /*全選字樣*/ .carts-footer text { font-size: 30rpx; margin-left: 8rpx; line-height: 10rpx; } /*立即結(jié)算按鈕*/ .carts-footer .button { line-height: 80rpx; text-align: center; width:220rpx; height: 80rpx; background-color: #f60; color: white; font-size: 36rpx; border-radius: 0; border: 0; }1.4.2 Select all and unselect all eventsImplement the bindSelectAll event and change the all-selected stateFirst define a data value to record the all-selected stateselectedAllStatus: false
bindSelectAll: function() { // 環(huán)境中目前已選狀態(tài) var selectedAllStatus = this.data.selectedAllStatus; // 取反操作 selectedAllStatus = !selectedAllStatus; // 購(gòu)物車(chē)數(shù)據(jù),關(guān)鍵是處理selected值 var carts = this.data.carts; // 遍歷 for (var i = 0; i < carts.length; i++) { carts[i].selected = selectedAllStatus; } this.setData({ selectedAllStatus: selectedAllStatus, carts: carts }); }
<toast hidden="{{toastHidden}}" bindchange="bindToastChange"> {{toastStr}} </toast>Bind the event bindCheckout for immediate settlement and pop up the cid pop-up window
bindCheckout: function() { // 初始化toastStr字符串 var toastStr = 'cid:'; // 遍歷取出已勾選的cid for (var i = 0; i < this.data.carts.length; i++) { if (this.data.carts[i].selected) { toastStr += this.data.carts[i].cid; toastStr += ' '; } } //存回data this.setData({ toastHidden: false, toastStr: toastStr }); }, bindToastChange: function() { this.setData({ toastHidden: true }); }1.5 Bottom floating fixed 1.5.1 Product list.carts-list Add margin-bottom: 80rpx; and modify the top margin to zero so that the bottom components and separation do not appear repeatedly. Padding: 0 40rpx;1.5.2 Bottom button.carts-footer Add background: white;1.5 .3 .carts-footer join
position: fixed; bottom: 0; border-top: 1px solid #eee;
##1.6 Summary
1.6.1 First define a data source and bury it in the layout file
total: ''
1.6.2 General summary function
sum: function() { var carts = this.data.carts; // 計(jì)算總金額 var total = 0; for (var i = 0; i < carts.length; i++) { if (carts[i].selected) { total += carts[i].num * carts[i].price; } } // 寫(xiě)回經(jīng)點(diǎn)擊修改后的數(shù)組 this.setData({ carts: carts, total: '¥' + total }); }
Then bindMinus This.sum() is called in bindPlus bindCheckbox bindSelectAll onLoad
As shown in the figure:
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
WeChat mini program makes takeout menu ordering function
How to implement it in the development of WeChat mini program E-commerce shopping cart logic
The above is the detailed content of Simple example of shopping cart in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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;

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

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

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

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 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

Practical tutorial: Detailed explanation of the shopping cart function with PHP and MySQL. The shopping cart function is one of the common functions in website development. Through the shopping cart, users can easily add the goods they want to buy to the shopping cart, and then proceed with settlement and payment. In this article, we will detail how to implement a simple shopping cart function using PHP and MySQL and provide specific code examples. To create a database and data table, you first need to create a data table in the MySQL database to store product information. The following is a simple data table

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
