账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Vue里面的多选反选单选问题
    41
    0

    题目描述

    完成购物车,完成对应商品的选择和取消的功能

    题目来源及自己的思路

    《Vue.js实战》第五章

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

    <!DOCTYPE html>
    <html lang="cmn-hans">
    
    <head>
        <meta charset="utf-8">
        <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
        <!--PC端必选   强制360浏览器使用webkit渲染 -->
        <meta name="renderer" content="webkit">
        <!--必选  for PC -->
        <link rel="stylesheet" type="text/css"  href="css.css">
        <title>购物车</title>
    </head>
    
    <body>
        <div>
            <p>购物车需求</p>
            <ol>
                <li>展示已经加入购物车的商品列表</li>
                <li>商品列表包含商品名称,商品单价,购买数量和操作能力</li>
                <li>能够实时显示所购买的总价</li>
                <li>商品数量可以增加,减少或者直接移除</li>
            </ol>
    
        </div>
        <hr>
        <!-- 页面挂载点 -->
        <div id="app" v-cloak>
            <template v-if="goods.length">
                <table>
                    <thead>
                        <tr>
                            <td>表头</td>
                            <td>商品名称</td>
                            <td>商品单价</td>
                            <td>购买数量</td>
                            <td>操作</td>
             <td>选择 | 全选<input type="checkbox" @click="selectAll" id="selectAll" :checked="isCheckedAll"></td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="good,index in goods">
                            <td>{{index+1}}</td>
                            <td>{{good.name}}</td>
                            <td>{{good.price}}</td>
                            <!-- 数量调整 -->
                            <td>
                                <button @click="reduce(index)" :disabled="good.count === 1">-</button>
                                {{good.count}}
                                <button @click="add(index)">+</button>
                            </td>
                            <!-- 操作 -->
                            <td>
                                <button @click="remove(index)">移除</button>
                            </td>
                            <td>
                 <input type="checkbox" @click="select(index,$event)" class="select">
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div><p> 全部总价:¥{{totalPrice}}</p>
    
                        <p>选中总价: ¥{{selectPrice}}</p>
    
                </div>
            </template>
            <div v-else>购物车是空的</div>
        </div>
        <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
        <script src="trolley.js"></script>
    </body>
    
    </html>

    css.css:

    table {
        border: 1px solid #ccc;
    }
    
    thead {
        background-color: #ddd;
    }
    
    td {
        border: 1px solid #ccc;
    }
    

    trolley.js:

    var app = new Vue({
        el: '#app',
        data: {
            //商品数据
            goods: [
                { id: 1, name: 'iphone 7', price: 6188, count: 1 },
                { id: 2, name: 'ipad Pro', price: 5888, count: 1 },
                { id: 3, name: 'MacBook Pro', price: 21488, count: 1 }
            ],
            selectPrice: 0,
            isCheckedAll: false ,//初始化没有全选择
            isChecked:false
        },
        computed: {
            totalPrice: function() {
                var total = 0;
                for (var i = 0; i < this.goods.length; i++) {
                    total += this.goods[i].price * this.goods[i].count;
                }
                return total;
            }
        },
        methods: {
            reduce: function(index) {
                //再次判断 reduce 减法的可靠性
                if (this.goods[index].count === 1) return;
                this.goods[index].count--;
            },
            add: function(index) {
                this.goods[index].count++;
            },
            remove: function(index) {
                //删除一条数组数据
                this.goods.splice(index, 1);
            },
            selectAll: function() {
                if (this.isCheckedAll) {
    
                    for (let i = 0; i < this.goods.length; i++) {
                        document.querySelectorAll(".select")[i].checked = false;
                    }
                } else {
    
                    for (let i = 0; i < this.goods.length; i++) {
                        document.querySelectorAll(".select")[i].checked = true;
                    }
                }
                this.isCheckedAll = !this.isCheckedAll;
            },
            select: function(index, event) {
    
                if (document.querySelectorAll(".select")[index].checked) {
                    document.querySelectorAll(".select")[index].checked = true;
    
                    //逐步全部选中及后续
    
    
    
    
    
    
                } else {
                    document.querySelectorAll(".select")[index].checked = false;
    
    
                          //逐步全部不选中及后续
    
                }
            }
        },
    });
    
    

    你期待的结果是什么?实际看到的错误信息又是什么?

    目前完成了单选和全选功能,但是实际在操作过程中应该在逐个选中之后,全选按钮选中,和逐个取消选中之后,全选按钮最后也取消选中状态,没有完成。
    我觉得思路应该是每次单选/取消单选的时候都要循环判断全部的checkbox是全部选中/全部不选中,然后再设置全选按钮的状态。但是不知道应该怎么写。

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 冷玉 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部