element复选框全选和取消

文章描述:

element复选框全选和取消以及获取选择的值和默认选中效果

载入

<!-- 引入样式 -->
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="vue/dist/vue.js"></script>
<script src="element-ui/lib/index.js"></script>

html

<div id="app">
    <template>
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
            <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
        </el-checkbox-group>

        <el-button type="primary" @click="changeCheckbox">获取值</el-button>
    </template>
</div>

script

const cityOptions = ['上海', '北京', '广州', '深圳'];
var App = new Vue({
	el:'#app',
    data(){
		return{
			checkAll: false,
            checkedCities: ['上海', '北京'],
            cities: cityOptions,
            isIndeterminate: true
        }
    },
	methods: {
		changeCheckbox(){
                
			console.log(this.checkedCities);
		},
		handleCheckAllChange(val) {
			this.checkedCities = val ? cityOptions : [];
			this.isIndeterminate = false;
		},
		handleCheckedCitiesChange(value) {
			let checkedCount = value.length;
			this.checkAll = checkedCount === this.cities.length;
			this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;

			console.log(value)
		}
	}
})

 

 

发布时间:2021/11/18

发表评论