美文网首页
element-UI checkbox 多选框

element-UI checkbox 多选框

作者: LostPaul | 来源:发表于2018-07-20 17:30 被阅读0次

    http://element-cn.eleme.io/#/zh-CN/component/checkbox
    这里的例子中,数据源是普通数组.

    这里为了需求,将普通数组改为对象数组.

    <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.id" :key="city.id">{{city.label}}</el-checkbox>
      </el-checkbox-group>
    </template>
    <script>
      const cityOptions = [{id:1,label:'上海'},{id:2,label:'北京'},{id:3,label:'广州'}, {id:4,label: '深圳'}];
      export default {
        data() {
          return {
            checkAll: false,
            checkedCities: [1,2],
            cities: cityOptions,
            isIndeterminate: true
          };
        },
        methods: {
          handleCheckAllChange(val) {
            this.checkAll =  !!this.checkAll;
            let checked = cityOptions.map(function(item){return item.id;});
            this.checkedCities = this.checkAll ? checked : [];
            this.isIndeterminate = false;
          },
          handleCheckedCitiesChange(value) {
            let checkedCount = value.length;
            this.checkAll = checkedCount === this.cities.length;
            this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
          }
        }
      };
    </script>
    

    相关文章

      网友评论

          本文标题:element-UI checkbox 多选框

          本文链接:https://www.haomeiwen.com/subject/gzeqmftx.html