搜尋
首頁微信小程式小程式開發微信小程序之購物車的實現代碼

微信小程序之購物車的實現代碼

Jan 05, 2018 pm 01:13 PM
程式碼小程式購物車

本文主要介紹了微信小程式實戰篇之購物車的實現程式碼範例,詳細的介紹了購物車的功能實現,具有一定的參考價值,有興趣的可以了解一下,希望能幫助到大家。

購物車的介面實現到不是很難,難點是處理裡面的邏輯,無論是小程序,還是APP,購物車的邏輯都是最難的,下面開始教大家如何實現購物車了,先上效果圖:


##cart.wxml


##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?&#39;../../images/cart/comment_select.png&#39;:&#39;../../images/cart/comment_normal.png&#39;}}" />

   <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?&#39;../../images/cart/comment_select.png&#39;:&#39;../../images/cart/comment_normal.png&#39;}}" />
  <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

  1. // pages/cart/cart.js
    var Temp = require(&#39;../../template/contract.js&#39;);
    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: &#39;http://mz.djmall.xmisp.cn/files/product/20161201/148058301941.jpg&#39;,
        name: "倩碧焕妍活力精华露",
        price: 340,
        isSelect: false,
        // 数据设定
        count: {
         quantity: 1,
         min: 1,
         max: 20
        },
       },
       {
        pic: &#39;http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg&#39;,
        name: "特效润肤露",
        price: 390,
        isSelect: false,
        // 数据设定
        count: {
         quantity: 3,
         min: 1,
         max: 20
        },
       },
       {
        pic: &#39;http://mz.djmall.xmisp.cn/files/product/20161201/148058228431.jpg&#39;,
        name: "倩碧水嫩保湿精华面霜",
        price: 490,
        isSelect: false,
        // 数据设定
        count: {
         quantity: 1,
         min: 1,
         max: 20
        },
       },
       {
        pic: &#39;http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg&#39;,
        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: &#39;去结算&#39;,
       icon: &#39;success&#39;,
       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,
      });
     }
    }));

    介紹一下用到的參數
  2. isAllSelect:是否全選
  3. ##totalMoney:總金額

carts :購物車商品資料

  1. switchSelect 勾選按鈕需要做的邏輯處理

  2. 判斷是否達到全部勾選,如果全部勾選,底部的全選按鈕要點亮,判斷依據是,價錢是否等於總價,當然這只是一種判斷方式,讀者也可以透過勾選的數量判斷,

  3. 對勾選或取消的按鈕,進行總價的加減法計算

this.setData,更新數據,這個是重點,每次處理完數據,都要記得更新數據

  1. allSelect 全選按鈕的邏輯處理

  2. 全選就把每個item勾選圖示點亮,然後統計總價錢,不全選就置為灰色,總價錢為0

#this.setData更新資料

微信小程式資料處理


一、修改資料方式


data:{
 name:&#39;我是初始化的name&#39;
}

1、this.data. name


this.data.name=&#39;我是代码君data&#39;

2、this.setData

 this.setData({
   name:&#39;我是代码君setData&#39;
  })
這兩種方式都可以改變數據,this.setData的好處是可以有刷新的效果,即即時更新數據

##二、修改物件數組


data:{
person:{
 name:&#39;代码君&#39;,
 city:&#39;厦门&#39;
}
}

修改全部物件


this.setData({
   person:{
    name:&#39;新代码君&#39;,
    city:&#39;湖南&#39;
   }
  })

修改部分資料

this.setData({
   &#39;person.name&#39;: &#39;代码君只修改名字&#39;
  })
//多个数组用这个
this.setData({
   &#39;person[0].name&#39;: &#39;代码君只修改名字&#39;
  })

三、新增刪除資料

1、新增資料concat


#
//假设这一段是我们要新增的数组
var newarray = [{
    name:&#39;增加的数据--&#39;+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是如何處理資料邏輯的,也講解了資料的增刪改查,這是必備的知識項,回去要多多練習。好了今天就講到這,祝大家週末愉快~


相關推薦:

JS實現的簡單拖曳購物車功能

詳解angular.js實作購物車功能

AngularJS 實作購物車全選反選功能實例分享######

以上是微信小程序之購物車的實現代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。