Home  >  Article  >  Web Front-end  >  Example code for implementing checkbox selection and batch deletion functions based on vue.js

Example code for implementing checkbox selection and batch deletion functions based on vue.js

零下一度
零下一度Original
2017-05-02 10:00:292760browse

This article mainly introduces the use of vue.js to realize the full selection and multiple deletion functions of checkbox. Friends who need it can refer to the

template code:

<template> 
<p class="hello"> 
<ul> <li v-for="(item, index) in proData"> 
<label for=""> 
<input type="checkbox" :value="index" v-model="selectArr"> 
</label>{{item.name}} 
</li>: 
</ul> 
<button type="" @click="del">删除</button>{{selectArr}} 
<label> 
1
<input type="checkbox" class="checkbox" @click="selectAll" />全选 
</label> 
</p> 
</template>

script part:

<script>
var proData = [{
  "name": "j1ax"
}, {
  "name": "j2ax"
}, {
  "name": "j3ax"
}, {
  "name": "j4ax"
}]
export default {
  name: &#39;hello&#39;,
  data() {
    return {
      proData: proData,
      selectArr: []
    }
  },
  created() {
    this.$http.get(&#39;/api/home&#39;).then(function(response) {
      response = response.body;
      this.proData = response.data;
    })
  },
  methods: {
    del() {
      let arr = [];
      var len = this.proData.length;
      for (var i = 0; i < len; i++) {
        if (this.selectArr.indexOf(i)>=0) {
          console.log(this.selectArr.indexOf(i))
        }else{
          arr.push(proData[i])
        }
      }
      this.proData = arr;
      this.selectArr = []
    },
    selectAll(event) {
      var _this = this;
      console.log(event.currentTarget)
      if (!event.currentTarget.checked) {
        this.selectArr = [];
      } else { //实现全选
        _this.selectArr = [];
        _this.proData.forEach(function(item, i) {
          _this.selectArr.push(i);
        });
      }
    }
  }
}
</script>

The above is the detailed content of Example code for implementing checkbox selection and batch deletion functions based on vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn