el-tree实现全选/反选、默认全选

这篇博客介绍了如何在Vue.js应用中利用Element UI组件库的el-tree元素,实现字段选择的全选和反选功能。通过监听弹框的可见性来触发全选操作,并在checkAllChange方法中处理全选和反选的逻辑,确保数据同步。文章还展示了相关的watch和methods代码片段。
摘要由CSDN通过智能技术生成

 

<el-form-item label="字段选择" :label-width="formLabelWidth">
        <div class="propLists">
          <div class="propListHeader">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="checkAllChange">全部选择</el-checkbox>
          </div>
          <div class="propListBody">
            <el-tree ref="channelTree" :data="props" :props="defaultProps" node-key="id" show-checkbox @check-change="handleCheckChange"></el-tree>
          </div>

        </div>
      </el-form-item>

 

 

watch:{
  visibleExcel(newVal) {  //在弹框打开时默认执行全选方法,
      if (newVal) {
        this.checkAllChange()
      }
    },
} 

methods: {   
// 全选或反选
    checkAllChange() {
      if (this.checkAll) { // 全选
        this.$nextTick(() => {    //这个如果要默认全选就必须加,否则会报错“setCheckedNodes”未定义
          this.$refs.channelTree.setCheckedNodes(this.props)
        })
        // this.
      } else { // 取消选中
        this.$nextTick(() => {
          this.$refs.channelTree.setCheckedKeys([])
        })
      }
    },
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值