Vue 复选框 checkbox 全选与取消全选

点我查看原文

按照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

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值