Vue购物车(全选和全不选)

news/2024/7/19 13:48:55 标签: vue.js, js, 前端

效果如下:

在这里插入图片描述


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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 500px;
            margin: 0 auto;
            border: 1px solid #000;
            border-collapse: collapse;
            text-align: center;
        }
        
        table th,
        td {
            border: 1px solid #000;
        }
    </style>
</head>

<body>
    <div id='app'>
        <table>
            <thead>
                <tr>
                    <th><input type="checkbox" v-model="isAll" @change="checkAll">全选/全不选</th>
                    <th>商品名称</th>
                    <th>商品价格</th>
                    <th>商品数量</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(v,i) in lists" ::key="v.id">
                    <td><input type="checkbox" v-model="checkGroup" :value="v" @change="checkOne"></td>
                    <td>{{v.name}}</td>
                    <td>{{v.price}}</td>
                    <td>
                        <button @click="v.num--" :disabled="v.num===1">-</button> {{v.num}}
                        <button @click="v.num++" :disabled="v.num===v.limit">+</button>
                    </td>
                    <td><button @click="del(i,v.id)">删除</button></td>
                </tr>
            </tbody>
        </table>
        {{checkGroup}}
        <div>总额:{{sum()}}</div>
    </div>
    <script src='/js>vue.js '></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                isAll: false,
                checkGroup: [],
                lists: [{
                    id: 1,
                    name: '商品1',
                    price: 10,
                    num: 1,
                    limit: 5
                }, {
                    id: 2,
                    name: '商品2',
                    price: 20,
                    limit: 5,
                    num: 1,
                }, {
                    id: 3,
                    name: '商品3',
                    price: 30,
                    limit: 5,
                    num: 1,
                }, {
                    id: 4,
                    name: '商品4',
                    price: 40,
                    limit: 5,
                    num: 1,
                }],
            },
            methods: {
                sum() {
                    // let sum = 0;
                    // this.checkGroup.forEach((value) => {
                    //     sum += value.price * value.num;
                    // });
                    // return sum;


                    return this.checkGroup.reduce((previousValue, value) => {
                        console.log(this.checkGroup);
                        console.log(value);
                        return previousValue += value.price * value.num;
                    }, 0);
                },
                del(i, id) {
                    // console.log(i);
                    //删除原数组
                    this.lists.splice(i, 1);
                    //得删除checkGroup数组中对应的元素
                    this.checkGroup = this.checkGroup.filter((value) => {
                        return value.id !== id;
                    });


                    //这个checkOne在点删除事件的时候也要判定一次
                    this.checkOne();
                },
                checkAll() {
                    if (this.isAll) {
                        this.checkGroup = this.lists;
                    } else {
                        this.checkGroup = [];
                    }
                },
                checkOne() {
                    if (this.checkGroup.length === this.lists.length && this.lists.length !== 0) {
                        this.isAll = true;
                    } else {
                        this.isAll = false;
                    }
                },
            },
        });
    </script>
</body>

</html>

http://www.niftyadmin.cn/n/1143448.html

相关文章

Vue中作用域插槽的妙用

昨天在学后台管理项目的时候遇到了有趣的东西-----作用域插槽 为什么scope.row可以获得本行的数据&#xff0c;说明这个大神在封装组件的时候&#xff0c;用了一个插槽<slot :rowdata></slot> 话又说回来&#xff0c;什么是作用域插槽&#xff1a;当我们在封装组件…

Vue报错:NavigationDuplicated: Avoided redundant navigation to current location(提示这是到当前位置的冗余导航)

今天在做电影app时&#xff0c;需求是&#xff1a;有一个长列表&#xff0c;当我们滑动后&#xff0c;进入了另外一个页面之后在返回列表页时&#xff0c;会依旧保持我们第一次滑动的位置。看图&#xff1a; 解决方法&#xff1a; 在router.js&#xff08;由于配置不同&#xf…

Vue中解决better-scroll的滚动问题以及刷新页面better-scroll不起作用问题

不得不说better-scroll是个很好用的第三方框架&#xff0c;但是使用恰当才能发挥它强大的功能哦&#xff01;具体使用步骤请移步→better-scroll链接Github 但是better-scroll用的恰当的话并不会出现在手机上滚动不了的情况 具体问题&#xff1a; 1.使用的时候为什么滚动不起作…

vue中报错Error in render: “TypeError: Cannot read property ‘length‘ of undefined“

通过查阅资料&#xff0c;其实这种报错会有两种&#xff1a; 1.在html标签模板上使用length会报错&#xff0c;报错原因为在这个时候你还拿不到某个数组的length&#xff0c;解决方案为&#xff1a;在此标签上加个v-if"array.lenth",如果你更想保险的话使用v-if"…

Vue报错[Vue warn]: Error in render: “TypeError: “checkedCount“ is read-only“

写vue时报出这样一个错误&#xff0c;某个东西是只读的&#xff1b; 报错原因&#xff1a;我在用一个变量的时候&#xff0c;我居然是用const声明的&#xff0c;真不细心。声明变量用let&#xff0c;常量用const&#xff1b; 复习一下let和const&#xff1a; 1.let &#xff08…

vue中keep-alive,include的缓存问题(玩好keep-alive)

前提&#xff1a;有A,B,C,D四个页面,A是按钮页(点击按钮进入B页面)&#xff0c;B是订单列表页&#xff0c;C是订单详情页&#xff0c;D是费用详情页 需求&#xff1a;顺序是A->B->C->D,每次都刷新页面&#xff0c;D->C->B时走缓存&#xff0c;但是每次从A到B都…

通过Vuex实现Input双向绑定

store.js: store new Vuex.store({state:{inputVal: },mutations:{setInput (state, newVal) {state.inputVal newVal}} }) 页面中绑定&#xff1a; <template><div><input v-model storeVal></div> </template> <script> export defa…

TypeScript泛型及其一些写法

Vue3拥抱Typescript之泛型 // 平常写ts可能会这样注解类型&#xff0c;但是这样有些麻烦&#xff0c;能不能简洁点呢 //function joi(first: string | number,second: string | number){ // return first second //} // JJ是泛型&#xff0c;在最简单的泛型用法中可以类比形参…