带全选按钮的小型购物车案例

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>

        //希望大家注意先引入jQuery.js文件,再引入vue.min.js文件
        <script src="js/jquery-3.5.1.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="js/vue.min.js" type="text/javascript" charset="utf-8"></script>
        <style type="text/css">
            #table{
                width: 900px;
                height: 100px;
                margin: 25px auto;
            }
            thead{
                background-color: #5BC0DE;
            }
            tr th,tr td{
                text-align: center;
                font-family: "宋体";
                font-size: 25px;
            }
            tr td{
                width: 50px;
                height: 80px;
            }
            #foot{
                font-family: "宋体";
                font-size: 25px;
            }
            #foot strong{
                font-size: 35px;
            }
        </style>
    </head>
    <body>
        <div id="table" >
            <div v-if="list.length">
                <table class="table table-bordered table-hover table-striped">
                    <thead>
                        <tr>
                            <!-- 全选按钮 -->
                            <th>全选 <input type="checkbox" id="all" @click="allClick()"  /></th>
                            <th>商品名称</th>
                            <th>商品图片</th>
                            <th>商品单价</th>
                            <th>购买数量</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="(item,index) in list">
                            <!-- 单选按钮 -->
                            <td><input type="checkbox" :checked="item.check" @click="select(index)" /></td>
                            <td>{{item.name}}</td>
                            <td><img :src="item.imag" class="img-responsive" ></td>
                            <td>{{item.price}}</td>
                            <td>
                                <button type="button" :disabled="item.num == 1" @click="reduct(index)">-</button>
                                {{item.num}}
                                <button type="button" @click="add(index)">+</button>
                            </td>
                            <td><button type="button" @click="del(index)" >移除</button></td>
                        </tr>
                    </tbody>
                </table>
                <div id="foot">
                    总价:<strong>¥{{allPrice}}</strong>
                </div>
            </div>
            <div v-else>
                <h1 style="font-family: '宋体'; color: crimson; margin: 30% 40%;">购物车内无商品</h1>
            </div>
        </div>
    </body>
    <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</html>

// index.js

var obj = new Vue({
    el:'#table',
    data:{
        list:[
            {
                name:'iPhone 7',
                price:6188,
                num:1,
                check:true,
                imag:'img/1.webp'
            },
            {
                name:'iPad Pro',
                price:5888,
                num:1,
                check:false,
                imag:'img/2.webp'
            },
            {
                name:'MacBook Pro',
                price:21488,
                num:1,
                check:false,
                imag:'img/3.webp'
            }
        ]
    },
    methods:{
        // 移除
        del:function(index){
            if(confirm('你确定要移除此商品吗?')){
                this.list.splice(index,1);
            }
        },
        // 增加
        add:function(index){
            this.list[index].num++;
        },
        // 减少
        reduct:function(index){
            this.list[index].num--;
        },
        // 全选
        allClick : function(){
            // 首先,获取到全选按钮,赋给变量isAll
            let isAll = document.getElementById('all');
            // 如果全选按钮被选中,单选按钮就全部选中  == >  list数组的check属性变为true
            if(isAll.checked == true){
                this.list.forEach(function(item,index){
                    item.check = true;
                })
            }else{
                this.list.forEach(function(item,index){
                    item.check = false;
                })
            }
        },
        // 单选
        /* 思路:
        1、先获取到全选按钮
         2、设一个变量n,循环list表中的数据,如果表内check为true是让n的数量自增加
         3、当n的数量长度等于list的数据长度时,全选按钮为true
         4、当n的数量长度不等于list的数据长度时,全选按钮为false
         */
        select:function(index){
            this.list[index].check = !this.list[index].check;
            let isAll = document.getElementById('all');
            let n = 0;
            for (var i = 0; i<this.list.length;i++){
                if(this.list[i].check == true){
                    n++
                }
            }
            if( n == this.list.length){
                isAll.checked = true
            }else{
                isAll.checked = false
            }
        }
        
    },

    computed:{
        allPrice:function(){
            let total = 0;
            this.list.forEach(function(val,index){
                if(val.check == true){
                    total += parseFloat(val.num * val.price);
                }
            })
            return total.toString().replace(/\B(?=(\d{3})+$)/g,',');//每三位数中间加个“,”
        }
        
    }
    
})

最后,希望可以得到大家的建议,谢谢大家,(本人的第一篇博客,谢谢大家支持)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

越过难题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值