VUE实践:用vue实现简单的购物车

先上效果图
购物车的页面,有点丑,大家将就看
在这里插入图片描述
勾选商品和修改商品的数量会显示相应的价格
在这里插入图片描述
全选和取消全选
在这里插入图片描述
删除商品
在这里插入图片描述
对商品的排序
在这里插入图片描述
对商品价格的过滤
在这里插入图片描述
好了,该上源码了

<!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>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <style>
    table{
        border: 1px solid red;
    }
    td{
        border: 1px solid red;
        text-align: center;
    }
    </style>
</head>

<body>
    <!-- 视图 -->
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>商品状态</th>
                    <th>商品ID
                        <!-- 排序的方法,在下边隐藏的是通过方法来改变的,没有隐藏的使用计算属性来控制 -->
                        <button @click='sorting'>排序</button>
                    </th>
                    <th>商品名称</th>
                    <th>商品价格
                        <!-- 这里是筛选,是通过方法来实现的 -->
                        <button @click='filter'>过滤小于3000</button>
                    </th>
                    <th>购买数量</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(list,index) in computedSorting">
                <!-- <tr v-for="(list,index) in  lists"> -->
                <!-- 上面隐藏的这条语句是通过方法来排序时的语句,这里是遍历的数据层中的原数组,计算属性则是遍历的他返回的数组 -->
                    <td>
                        <input type="checkbox" v-model="list.rel" />
                    </td>
                    <td>{{index+1}}</td>
                    <!-- index是下标 -->
                    <td>{{list.name}}</td>
                    <td>{{list.price}}</td>
                    <td>
                        <button @click='reduce(index)'>-</button>
                        {{list.count}}
                        <button @click='add(index)'>+</button>
                        <!-- 数量的加减 -->
                    </td>
                    <td>
                        <button @click='remove(index)'>移除</button>
                        <!-- 删除数据 -->
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="6" v-if="lists.length==0">购物车为空</td>
                    <!-- 当所有的商品都被移除后,显示这条语句 -->
                </tr>
                <tr>
                    <td v-if="lists.length>0">
                        全选:<input v-model="rels" type="checkbox" @change='allrels' />
                        <!-- 全选方法 -->
                    </td>
                    <td colspan="5" v-if="lists.length>0">总价为:{{total}}</td>
                </tr>
            </tfoot>
        </table>
    </div>
    <!-- 数据模型 -->
    <script>
        var vueApp = new Vue({
            el: '#app',  //绑定的元素节点
            data: {  //实例数据
                lists: [        //原数组
                    { id: 1321, name: "小米max2", price: 1500, count: 1, rel: true },
                    { id: 2763, name: "联想低配电脑", price: 3000, count: 1, rel: false },
                    { id: 332, name: "外星人中端电脑", price: 26000, count: 1, rel: false },
                    { id: 4000, name: "道德经珍藏版", price: 500, count: 1, rel: false },
                ],
                rels: false,//全选按钮的初始状态
                sort: true,//排序时使用的标识 true为升序 false为降序
                flag:0,//通过计算属性来排序数组时用的标识 0为默认 1为升序 2为降序
            },
            methods: {
                //这里的this是vueApp这个对象
                add: function (index) { //添加数量的方法
                    this.lists[index].count++;
                },
                reduce: function (index) {  //减少数量的方法
                    if (this.lists[index].count > 1) this.lists[index].count--;
                },
                remove: function (index) {  //移除商品的方法
                    this.lists.splice(index, 1);
                },
                allrels: function () { //全选和取消全选的方法
                    if (this.rels) {
                        this.lists.forEach(function (ele) {
                            ele.rel = true;
                        })
                    } else {
                        this.lists.forEach(function (ele) {
                            ele.rel = false;
                        })
                    }
                },
                sorting: function () {  //排序的方法 隐藏的部分是通过方法来排序的 没有隐藏的是修改flag标识
                    // if (this.sort) {
                    //     this.sort = false;
                    //     this.lists.sort(function (a1, a2) {
                    //         return a1.id - a2.id;
                    //     })
                    // } else {
                    //     this.sort = true;
                    //     this.lists.sort(function (a1, a2) {
                    //         return a2.id - a1.id;
                    //     })
                    // }
                    if(this.flag ==0){
                        this.flag =1;
                    }else if(this.flag == 1){
                        this.flag =2;
                    }else{
                        this.flag =1;
                    }

                },
                filter:function(){  //对价格的筛选

                   this.lists =  this.lists.filter(function(ele){
                        console.log(ele);
                        return ele.price>=3000;
                    })
                }

            },
            computed: { //计算属性
                total: function () {    //总价
                    var money = 0;
                    this.lists.forEach(element => {
                        if (element.rel) money += element.price * element.count;
                    });
                    return money;

                },
                computedSorting:function(){ //排序
                    if(this.flag ==1){
                        this.lists.sort(function (a1, a2) {
                            return a1.id - a2.id;
                        });
                        return this.lists;
                    }else if(this.flag ==2){
                        this.lists.sort(function (a1, a2) {
                            return a2.id - a1.id;
                        })
                        return this.lists;
                    }else{
                        return this.lists;
                    }
                }
            }

        })
    </script>
</body>
</html>

这里有个小问题,对价格的筛选最好也用vue的计算属性来实现,因为filter这个方法会改变原数组,相当于你筛选完了以后,原数组中的数据也被删除了,但用计算属性就不会有这个问题,就拿来给大家练手吧

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值