欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue实现全选和反选功能

程序员文章站 2022-09-08 22:31:05
本文实例为大家分享了vue实现全选反选功能的具体代码,供大家参考,具体内容如下

本文实例为大家分享了vue实现全选反选功能的具体代码,供大家参考,具体内容如下

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
</head>
<script type="text/javascript" src = "vue.js"></script>
<body>
    <div id = "test">
      <input type='checkbox' v-model="checkbox.checked" class='input-checkbox' @click='checkedall'>全选
      <div v-for='checkb in checkboxdata'>
        <input  type='checkbox' class='input-checkbox' @click="checkitem" v-model='checkbox.items[checkb.id]'>
        {{checkb.value}}
      </div>

    </div>

</body>
<script>
  var vue = new vue({
    el:"#test",
    data:{
      checkboxdata:[
      {
        id:'1',
        value:'苹果'
      },{
        id:'2',
        value:'荔枝'
      },{
        id:'3',
        value:'香蕉'
      },{
        id:'4',
        value:'火龙果'
      }
      ],
      checkbox:{
        checked:false,
        items:{}
      }

    },
    methods:{
      checkedall: function() {
        var _this = this;
        console.log(_this.checkboxdata);
        console.log(this.checkbox.items);
        this.checkboxdata.foreach(function (item) {
          console.log(item.id);
          _this.checkbox.items[item.id] = _this.checkbox.checked;
          console.log(_this.checkbox.items);
        });
        //实现反选

      },
      checkitem:function(){
        var unchecked = 0;
        var _this = this;
        this.checkboxdata.foreach(
          function(item) {
            unchecked += (! _this.checkbox.items[item.id]) || 0;
          });
        _this.checkbox.checked = unchecked > 0 ? false : true;
      }

    }

  })
</script>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。