码迷,mamicode.com
首页 > 其他好文 > 详细

vue实现全选和取消全选

时间:2018-02-24 20:50:30      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:src   ted   log   简单   char   txt   type   list   html   

很简单使用的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>checkboxAll</title>
</head>
<body>
    <div id="app">
        <ul class="checkboxs">
            <li >
                <label>{{checkAllTxt}}<input type="checkbox" v-model="checked" @click= "checkboxAll($event)"></label> 
            </li>
            <li v-for=‘list,index in lists‘>
                <label>{{list.name}}<input type="checkbox" v-model="checkStatusArr[index]"></label> 
            </li>
        </ul>
    </div>
    <script src="../vue.js"></script>
    <script>
        window.onload = function () {
            var vm = new Vue({
                el: ‘#app‘,
                data: {
                    lists: [
                        {
                            ‘name‘: ‘张三‘
                        }, {
                            ‘name‘: ‘李四‘
                        }, {
                            ‘name‘: ‘王五‘
                        }, {
                            ‘name‘: ‘赵六‘
                        }
                    ],
                    checked: false,
                    checkStatusArr: []
                },
                methods: {
                    checkboxAll (event) {
                        this.checkStatusArr = [];
                        if (event.currentTarget.checked) {
                            let len = this.lists.length;
                            for (let i = 0; i < len; i++) {
                                this.checkStatusArr.push(1);// 1 ==true
                            }
                        } else {
                            this.checkStatusArr = [];
                        }
                    }
                },
                watch: {
                    checkStatusArr (val, oldVal) {
                        if (val.length > 0) {
                            this.checked = true;
                        } else {
                            this.checked = false;
                        }
                    }
                },
                computed: {
                    checkAllTxt () {
                        if (this.checked === true) {
                            return ‘全不选‘;
                        } else {
                            return ‘全选‘;
                        }
                    }
                }
            });
        };
    </script>
</body>
</html>

 

vue实现全选和取消全选

标签:src   ted   log   简单   char   txt   type   list   html   

原文地址:https://www.cnblogs.com/lm-it/p/8467400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!