zoukankan      html  css  js  c++  java
  • vue购物车功能源码

    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
    .cart {
    50px;
    height: 50px;
    background: orangered;
    text-align: center;
    font-size: 20px;
    position: fixed;
    top: 400px;
    right: 0;
    }

    .cart i {
    color: #fff
    }

    .cart span {
    display: block;
    color: #fff;
    }
    </style>
    </head>

    <body>
    <div id="app">
    <div class="navbar navbar-inverse">
    <div class="container-fluid">
    <div class="navbar-header">
    <a class="navbar-brand">珠峰购物车</a>
    </div>
    </div>
    </div>

    <div class="container">
    <div class="col-md-3 text-center" @dragstart="start($event,index)" draggable="true" v-for="(product,index) in products">
    <div class="panel panel-primary">
    <div class="panel-heading" v-html="product.productName"></div>
    <div class="panel-body">
    <img :src="product.productCover" width="130px" height="140px" ref="img">
    </div>
    <div class="panel-footer">
    价格 <span v-html="product.productPrice"></span>
    </div>
    </div>
    </div>
    <table class="table table-bordered">
    <tr>
    <th>
    <input type="checkbox" v-model="checkall"> 全选</th>
    <th>商品名称</th>
    <th>商品价格</th>
    <th>商品数量</th>
    <th>小计</th>
    <th>操作</th>
    </tr>
    <tr v-for="cart in carts">
    <td>
    <input type="checkbox" v-model="cart.isSelected">
    </td>
    <td v-html="cart.productName"></td>
    <td v-html="cart.productPrice"></td>
    <td>
    <input type="text" v-model="cart.productCount">
    </td>
    <td v-html="cart.productPrice*cart.productCount"></td>
    <td>
    <button @click="remove(cart)" class="btn">删除</button>
    </td>
    </tr>
    <tr>
    <td colspan="6">
    总价 <span v-html="sum | toInt"></span>
    </td>
    </tr>
    </table>
    </div>
    <div class="cart" @drop="drop" @dragover.prevent>
    <i class="glyphicon glyphicon-shopping-cart"></i>
    <span v-html="count"></span>
    </div>

    </div>
    <script src="js/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
    let vm = new Vue({
    el: '#app',
    data: {
    products: [],
    current: '', //当前拖动的是哪个元素
    carts: JSON.parse(localStorage.getItem('cartList')) || [],
    },

    filters: {
    toInt(input) { // input = sum
    return input.toFixed(2)
    }
    },
    computed: {
    count() {
    return this.carts.reduce((prev, next) => {
    return prev + parseInt(next.productCount);
    }, 0)
    },
    checkall: {
    get() { //如果products中有一个元素isSelected是false checkall则为false
    //如果其中有一个返回为false 结果就是false
    return this.carts.every(item => item.isSelected)
    },
    set(val) {
    this.carts.forEach(item => item.isSelected = val)
    }
    },
    sum() {
    return this.carts.reduce((prev, next) => { //es5
    if(next.isSelected) {
    return prev + next.productCount * next.productPrice
    } else {
    return prev;
    }
    }, 0);
    }
    },
    methods: {
    remove(cart) { //filter过滤 如果返回true 表示留下并且放到一个新的数组里
    this.carts = this.carts.filter(item => item != cart); //es5
    },
    start(e, index) { //e.dataTransfer.setDragImg
    //ref this.$refs.img
    // 低版本浏览器不识别 chrome 需要60+
    let product = { ...this.products[index],
    productCount: 1,
    isSelected: true
    };
    this.current = product; //保存当前拖动的那个元素
    e.dataTransfer.setDragImage(this.$refs.img[index], 0, 0)
    },
    drop() {
    //如果当前购物车中 有 则累加数量即可 this.current.id == this.carts其中一项如果相等
    if(this.carts.some(item => item.id == this.current.id)) {
    this.carts.forEach(item => {
    if(item.id == this.current.id) {
    item.productCount++;
    }
    });
    } else {
    this.carts.push(this.current);
    }
    }
    },
    created() { //created中的this 也是vm
    //箭头函数中没有this指向
    axios.get('./products.json').then(res => {
    this.products = res.data;
    })
    },
    });
    </script>
    </body>

    </html>

    不忘初心,方得始终,初心易得,始终难守。
  • 相关阅读:
    计算机组成原理期末第十章【计算部分】
    计算机组成原理期末第六章【计算部分】
    快速傅里叶变换总结
    Graph 题解
    NOI 赛前复习旧题 & 复习知识点计划
    Party 题解
    洛谷 P7054 [NWRRC2015]Graph 题解
    CF1542D Priority Queue 题解
    Codeforces Round #701 (Div. 2) E. Move and Swap
    常微分方程
  • 原文地址:https://www.cnblogs.com/chuxinsyn/p/8006559.html
Copyright © 2011-2022 走看看