博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vue 复选框 checkbox 全选与取消全选
阅读量:4156 次
发布时间:2019-05-25

本文共 1994 字,大约阅读时间需要 6 分钟。

按照jQuery的思想来做的话,要选中全选checkbox和所有的checkbox项,分别注册选中事件,判断选中状态来给相关的checkbox设置对应的状态,这就涉及到很多的dom操作。

下面就看一下vue数据驱动dom的思想来实现这一功能。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<table class="table table-bordered">

    <tr>

      <td><input type="checkbox" id="checkbox" v-model="checked" @change="changeAllChecked()"></td>

      <td>{

{$t('account.name')}}</td>

      <td>{

{$t('account.model')}}</td>

      <td>{

{$t('account.onTime')}}</td>

      <td>{

{$t('account.prepared')}}</td>

      <td>{

{$t('account.Ip')}}</td>

      <td>{

{$t('account.status')}}</td>

   </tr>

   <tr v-for="item in collectionList">

      <td><input type="checkbox" :id="item.name" :value="item.name" v-model="checkedNames"></td>

      <td>{

{item.name}}</td>

      <td>{

{item.model}}</td>

      <td>{

{item.time}}</td>

      <td>{

{item.yn}}</td>

      <td>{

{item.ip}}</td>

      <td>{

{item.status}}</td>

    </tr>

</table>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

new Vue({

    el: '#app',

    data: {

        collectionList:[{name:'Collection1',model:'zy-asdsa21311231',status:'上线',yn:'y',time:'2019-6-30',ip:'192.168.8.78'},

                  {name:'Collection2',model:'zy-asdsa21311231',status:'失联',yn:'y',time:'2019-6-30',ip:'192.168.8.78'},

                  {name:'Collection3',model:'zy-asdsa21311231',status:'上线',yn:'n',time:'2019-6-30',ip:'192.168.8.78'},

                  {name:'Collection4',model:'zy-asdsa21311231',status:'上线',yn:'y',time:'2019-6-30',ip:'192.168.8.78'},

                  {name:'Collection5',model:'zy-asdsa21311231',status:'失联',yn:'n',time:'2019-6-30',ip:'192.168.8.78'}

          ],

      checkedNames: [],

      checked:false,

    },

    methods: {

        changeAllChecked: function() {

            if (this.checked) {

            var collectionList = this.collectionList

                for(let key in collectionList) {

                  console.log(collectionList[key]);

                  this.checkedNames.push(collectionList[key].name);

                }

            else {

                this.checkedNames = []

            }

        }

    },

    watch: {

        "checkedNames"function() {

                      console.log(this.checkedNames+"---"+this.collectionList+"----"+this.checked);

            if (this.checkedNames.length == this.collectionList.length) {

                this.checked = true

            else {

                this.checked = false

            }

        }

    }

})

效果:

`1IBX8E~%[LG5[~XT}]9H9S.png

 

转载地址:http://qhwxi.baihongyu.com/

你可能感兴趣的文章
数组分为两部分,使得其和相差最小
查看>>
有趣的排序——百度2017春招
查看>>
二叉树的最近公共祖先LCA
查看>>
数组中累加和为定值K的最长子数组长度
查看>>
素数对--腾讯2017校招编程
查看>>
JAVA集合--ArrayList实现原理
查看>>
synchronized与Lock
查看>>
数据库索引
查看>>
实现包含min,max,push,pop函数的栈
查看>>
实验2-6 字符型数据的输入输出
查看>>
实验3-5 编程初步
查看>>
实验4-1 逻辑量的编码和关系操作符
查看>>
实验5-2 for循环结构
查看>>
实验5-3 break语句和continue语句
查看>>
实验5-4 循环的嵌套
查看>>
实验5-5 循环的合并
查看>>
实验5-6 do-while循环结构
查看>>
实验5-7 程序调试入门
查看>>
实验5-8 综合练习
查看>>
第2章实验补充C语言中如何计算补码
查看>>