其他分享
首页 > 其他分享> > vue实现全选,反选,全不选功能?

vue实现全选,反选,全不选功能?

作者:互联网

<template>
	<div>
		<button @click="checkAnti">反选</button>
		<button @click="checkAll">全选</button>
		<button @click="checkNone">全不选</button>
		<input type="checkbox" value="" v-model="inputAll" @click="checkInputAll">

		喜好:
		<div v-for="item in inputArr">
			{{item.text}} : <input type="checkbox" value="" v-model="item.checked">
		</div>
		{{isCheckItemAll}}
	</div>
</template>

<script>

export default {
  name: "index",
  components:{},
  data(){
    return {
    	inputAll:false,
			inputArr: [
				{ text: '足球', checked: true },
				{ text: '篮球', checked: false },
				{ text: '羽毛球', checked: false },
				{ text: '游泳', checked: false },
			]
    }
  },
  watch: {

	},
  mounted(){

	},
  destroyed() {},
  activated() {},
	methods: {
		checkNone() {
			this.inputArr.forEach(item => {
				item.checked = false;
			});
			this.inputItemAll = false;
		},
		checkAll() {
			this.inputArr.forEach(item => {
				item.checked = true;
			});
			this.inputItemAll = true;
		},
		checkAnti() {
			this.inputArr.forEach(item => {
				item.checked = !item.checked;
			});
		},
		checkInputAll(){
			this.inputAll=!this.inputAll;
			if(this.inputAll){
				this.checkAll()
			}else{
				this.checkNone()
			}
		},
	},
  computed:{
		isCheckItemAll(){
			return this.inputArr.every((item)=>{
				return item.checked === true ? this.inputAll = true : this.inputAll = false
			})
		}
	},
}
</script>
<style scoped lang="less">

</style>

标签:vue,false,text,反选,item,全选,checked,true,inputAll
来源: https://blog.csdn.net/qq_44930379/article/details/122338770