首頁  >  文章  >  web前端  >  基於vue.js實作checkbox的全選和批次刪除功能實例程式碼

基於vue.js實作checkbox的全選和批次刪除功能實例程式碼

零下一度
零下一度原創
2017-05-02 10:00:292722瀏覽

這篇文章主要介紹了使用vue.js實作checkbox的全選和多個的刪除功能,需要的朋友可以參考下

template程式碼:

<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部分:

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

以上是基於vue.js實作checkbox的全選和批次刪除功能實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn