购物车

作者: 一叶扁舟丶 | 来源:发表于2018-10-18 19:22 被阅读40次

index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>购物车示例</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>


    <div id="app" v-cloak>
        <template v-if="list.length">
            <thead>
                <tr>
                    <th></th>
                    <th>商品名称</th>
                    <th>购买单价</th>
                    <th>购买数量</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item, index) in list">
                    <td>{{ index + 1 }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.price }}</td>
                    <td>
                        <button
                            @click="handleReduce(index)"
                            :disabled="item.count === 1">-</button>
                        {{ item.count }}
                        <button @click="handleAdd(index)">+</button>
                    </td>
                    <td>
                        <button @click="handleReduce(index)">移除</button>
                    </td>
                </tr>
            </tbody>
            <div>总价: $ {{ totalPrice }}</div>
        </template>
        <div v-else>购物车为空</div>
    </div>

    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
    <script src="index.js"></script>

</body>
</html>

index.js:

var app = new Vue ({
    el: '#app',
    data: {
        list: [
            {
                id: 1,
                name: 'iPhone x',
                price: 6799,
                count: 1
            },
            {
                id: 2,
                name: 'iPad Pro',
                price: 9999,
                count: 1
            },
            {
                id: 3,
                name: 'MacBkko Pro',
                price: 25999,
                count: 1
            }
        ],
        a: 5
    },

    computed: {
        totalPrice: function () {
            var total = 0;
            for (var i = 0; i < this.list.length; i++) {
                var item = this.list[i];
                total += item.price * item.count;
            }

            return total.toString().replace(/\B(?=(\d{3})+$)/g,',');
        }
    },
    methods: {
        handleReduce: function (index) {
            if (this.list[index].count === 1) return;
            this.list[index].count--;

        },
        handleAdd: function (index) {
            this.list[index].count++;
        },
        handleRemove: function () {
            this.list.splice(index, 1);
        }
    }

})

style.css:

[v-cloak] {
    disPlay: none;
}
table {
    border: 1px solid #e9e9e9;
    border-collapse: collapse;
    border-spacing: 0;
    empty-cells: show;
}
th,td {
    padding: 8px 16px;
    border: 1px solid #e9e9e9;
    text-align: left;
}
th {
    background: #f7f7f7;
    color: #5c6b77;
    font-weight: 600;
    white-space: nowrap;
}

相关文章

  • 商城之购物车

    购物车管理: 包含功能:提交商品到购物车、显示购物车列表、删除购物车里商品、修改购物车、清空购物车等等 1、购...

  • 购物车模块实现

    1、购物车列表功能实现 点击加入购物车或者点击购物车图标后进入购物车页面,在购物车页面中首先渲染cartList的...

  • SSM框架学习日记(6)——购物车模块

    购物车相关接口 添加购物车,购物车列表,更新商品数量,删除购物车先新建CartController和CartSer...

  • 8.5-高并发下的互联网电商购物车实战-加入购物车接口开发—小滴

    高并发下的互联网电商购物车实战-加入购物车接口开发 简介:电商购物车实现案例-加入购物车接口开发 添加购物车接口 ...

  • day11购物车10-细节完善

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车08-代理的简单实现

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11-购物车06-清空和购买

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车07-KVO的应用

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车09-代理设计模式

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • 8.购物车管理

    购物车管理模块是属于用户侧模块,主要有7个接口:添加商品到购物车、更新购物车商品数、移除购物车商品、查看购物车当中...

网友评论

    本文标题:购物车

    本文链接:https://www.haomeiwen.com/subject/djwszftx.html