• Element-ui Tree组件实现单选


      下午遇到一个需求:要求树只能单选(我们用的Element-ui 的tree组件,官方文档默认支持的是多选),现将实现单选的方法记录下:

      template中新增如下:

    <el-tree
      @check-change="handleCheckChange"
      :props="defaultProps">
    </el-tree>
    

      script中新增如下

    methods: {
          handleCheckChange (data, checked, indeterminate) {
            let {id} = data
    
            let index = this.checked.indexOf(id)
    
    // 当前节点不在this.checked中,且当前节点为选中状态 if (index < 0 && this.checked.length && checked) { this.$message.warning('只能选中一个节点') this.$refs.tree.setChecked(data, false) // 取消当前节点的选中状态 return }
    // 当前节点在this.checked中,当前节点为未选中状态(主动去掉当前选中状态) if (!checked && index >= 0 && this.checked.length) { this.checked = [] return }
    // 当前节点不在this.checked(长度为0)中,当前节点为选中状态,this.checked中存储当前节点id if (index < 0 && !this.checked.length && checked) { this.checked.push(id) } }, }, data () { return { checked: [], // 存储选中节点的id } }

      在线demo地址为:https://codepen.io/pen/?&editable=true

  • 相关阅读:
    成员对象和封闭类
    静态成员
    this指针
    类型构造构造函数
    拷贝构造函数
    C++
    矩阵快速幂求递推式
    对浅拷贝和深拷贝的基本理解
    传引用作为形参和返回值_节省参数拷贝的时间
    namespace的基本知识
  • 原文地址:https://www.cnblogs.com/hanshuai/p/12435613.html
Copyright © 2020-2023  润新知