微信小程序实战 购物车功能

运行效果


项目demo

一、准备工作

基本需求

显示图书基本信息:名称、作者、描述、价格、数量。
点击复选框进行toggle操作。当前选中,则变成未选中;当前未选中,则变成选中。
图书数量的加减操作。
根据选中商品进行价格汇总。
全选/全不选。

目录结构

二、程序实现步骤

复选框进行toggle操作。当前选中,则变成未选择;当前未选中,则变成选中。购物车商品全部选中,全选按钮为选中状态。购物车商品全部未选中,全选按钮为未选中状态。

  /**
   * 用户选择购物车商品
   */
  checkboxChange: function (e) {
    console.log('checkbox发生change事件,携带value值为:', e.detail.value);
    var checkboxItems = this.data.goodList;
    var values = e.detail.value;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
      for (var j = 0; j < values.length; ++j) {
        if (checkboxItems[i].isbn == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    var checkAll = false;
    if (checkboxItems.length == values.length) {
      checkAll = true;
    }

    this.setData({
      'goodList': checkboxItems,
      'checkAll': checkAll
    });
    this.calculateTotal();
  },

商品的加减操作。当前数量大于1,可以进行加减操作;当前数量为1时,只能进行加操作。

  /**
   * 用户点击商品减1
   */
  subtracttap: function (e) {
    var index = e.target.dataset.index;
    var goodList = this.data.goodList;
    var count = goodList[index].count;
    if (count <= 1) {
      return;
    } else {
      goodList[index].count--;
      this.setData({
        'goodList': goodList
      });
      this.calculateTotal();
    }
  },

  /**
   * 用户点击商品加1
   */
  addtap: function (e) {
    var index = e.target.dataset.index;
    var goodList = this.data.goodList;
    var count = goodList[index].count;
    goodList[index].count++;
    this.setData({
      'goodList': goodList
    });
    this.calculateTotal();
  },

用户点击全选/全不选,遍历购物车所有商品设置当前选中状态。

  /**
   * 用户点击全选
   */
  selectalltap: function (e) {
    console.log('用户点击全选,携带value值为:', e.detail.value);
    var value = e.detail.value;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
    }

    var goodList = this.data.goodList;
    for (var i = 0; i < goodList.length; i++) {
      var good = goodList[i];
      good['checked'] = checkAll;
    }

    this.setData({
      'checkAll': checkAll,
      'goodList': goodList
    });
    this.calculateTotal();
  }

选中商品数量发生改变时,进行商品总数量和总价格的计算。

  /**
   * 计算商品总数
   */
  calculateTotal: function () {
    var goodList = this.data.goodList;
    var totalCount = 0;
    var totalPrice = 0;
    for (var i = 0; i < goodList.length; i++) {
      var good = goodList[i];
      if (good.checked) {
        totalCount += good.count;
        totalPrice += good.count * good.price;
      }
    }
    totalPrice = totalPrice.toFixed(2);
    this.setData({
      'totalCount': totalCount,
      'totalPrice': totalPrice
    })
  },
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 离散数据 定义:数值可清晰计数,入NBA 得分,篮板、失误,点赞数等等; 常见的数据模型:柱状图最适合表现离散数据...
    4c786f652758阅读 500评论 0 0
  • 作为资深的云养猫玩家,也是因为学业的原因目前没有办法真正的拥有猫主子,但我还是相信我总会有猫的。 所以就有了这个和...
    槐安安阅读 1,286评论 0 0
  • 蝴蝶飞飞飞满街, 五彩斑斓焰似火。 小城不识春风面, 屠苏一醉万里歌!
    昊水长天阅读 203评论 0 0
  • 不知什么能够化作不朽, 苍旧 一个人怎么能挽留。 时间请不得谁为谁逗留, 未来还有几人泪流, 可惜我早已唱不出什...
    疯左阅读 203评论 -3 4
  • 亲爱的你,人生有没有很多这样的时刻?你惊心动魄,而世界一无所知。你翻山越岭,而天地寂静无声?人生说到底,是一场一个...
    金指尖的花园阅读 337评论 0 1