咔叽游戏

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 400|回复: 0

[JavaScript] vue.js实现简单购物车功能

[复制链接]
  • TA的每日心情
    无聊
    2019-6-2 14:11
  • 签到天数: 4 天

    [LV.2]圆转纯熟

    发表于 2020-7-6 19:01:29 | 显示全部楼层 |阅读模式
    本文实例为大家分享了vue.js实现简单购物车的具体代码,供大家参考,具体内容如下
    这次我将给大家带来一个vue.js实现购物车的小项目,如有不足请严厉指出。
    购物车功能有:全选和选择部分商品,选中商品总价计算,商品移除,以及调整购买数量等功能。
    js主要有以下方法
    加函数,减函数,手动修改数量判断库存函数,总价格计算函数,单选事件,全选事件,一共分为6个事件
    具体效果如下图
    vue.js实现简单购物车功能-1.jpg

    代码在这里
    main.js


    'use strict'

    var app = new Vue({
    el: '#app',
    data: {
    list: [
      {
      id: 1,
      name: 'iPhone 7',
      price: 6188,
      count: 1,
      check: true,
      },
      {
      id: 2,
      name: 'iPad Pro',
      price: 5888,
      count: 1,
      check: false,
      },
      {
      id: 3,
      name: 'MacBook Pro',
      price: 21488,
      count: 1,
      check: true,
      },
    ]
    },
    methods: {
    remove: function (index) { //移除商品
      this.list.splice(index, 1);
    },
    reduce: function (index) { //减少商品
      this.list[index].count --;
    },
    add: function (index) { //增加商品
      this.list[index].count ++;
    },
    selAll: function () { //商品全选
      let isAll = document.querySelector('#all');

      if (isAll.checked == true) {
      this.list.forEach(function(item, index) {
       item.check = true;
      })
      } else {
      this.list.forEach(function(item, index) {
       item.check = false;
      })
      }
    }
    },
    computed: {
    totalPrices: function () { //计算总价
      let totalPrices = 0;

      this.list.forEach(function (val, index) {
      if (val.check == true)
       totalPrices += parseFloat(val.price * val.count);
      })

      return totalPrices.toString().replace(/\B(?=(\d{3})+$)/g, ','); //每三位数中间加一个‘,'
    }
    }
    })index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="main.css" >
    </head>
    <body>
    <div id="app" v-cloak>
    <template v-if="list.length">
    <table>
      <thead>
      <tr>
       <th>全选<input id="all" @click="selAll" type="checkbox" checked></th>
       <th>商品名称</th>
       <th>商品单价</th>
       <th>购买数量</th>
       <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <template v-for="(item, index) in list">
       <tr>
       <td>
        <input type="checkbox" :checked="item.check" @click="item.check = !item.check">
       </td>
       <td>
        {{ item.name }}
       </td>
       <td>
        {{ item.price }}
       </td>
       <td>
        <button @click="reduce(index)" :disabled="item.count == 1">-</button>
        {{ item.count }}
        <button @click="add(index)">+</button>   
       </td>
       <td>
        <button @click="remove(index)">移除</button>
       </td>
       </tr>
      </template>
      </tbody>
    </table>
    <div>总价: ¥ {{ totalPrices }}</div>
    </template>
    <template v-else>
    购物车没有商品
    </template>
    </div>

    <script src="vue.js"></script>
    <script src="main.js"></script>
    </body>
    </html>main.css

    [v-cloak] {
    display: none;
    }

    #app {
    width: 500px;
    margin: 0 auto;
    }

    table {
    width: 100%;
    border: 1px solid #444;
    border-collapse: collapse;
    }

    th, td {
    padding: 8px 16px;
    border: 1px solid #444;
    text-align: left;
    }

    th {
    background: #89abd3;
    color: rgb(214, 224, 235);
    font-weight: 600;
    white-space: nowrap;
    }更多文章可以点击《Vue.js前端组件学习教程》学习阅读。
    关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。
    更多vue学习教程请阅读专题《vue实战教程》
    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持咔叽论坛。

    原文地址:https://www.jb51.net/article/187637.htm

    QQ|免责声明|小黑屋|手机版|Archiver|咔叽游戏

    GMT+8, 2024-3-29 03:49

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表