vue树结构el-tree重新加载后默认触发上次节点点击事件

template

<el-tree :data="treeData" node-key="id" default-expand-all :expand-on-click-node="false" highlight-current :props="defaultProps" @node-click="handleNodeClick" v-loading="treeLoading" style="min-width: 300px;"
          ref="tree">
        </el-tree>

data

currentTreeNode: '' // 初始化第一个点击节点为父节点

methods

// 组织机构树 部门树
    getTreeData() {
      this.treeLoading = true
      this.treeData = []
      this.$post(this.$apis.getAllOrgTreeNodes, {
        token: this.$store.state.userInfo.token
      }).then(res => {
        if (res.data && res.data.length !== 0) {
          this.treeData.push(res.data)
        }
        //'nextTick()' 在下次 DOM 更新循环结束之后执行延迟回调
        // 默认点击
        // this.$nextTick().then(() => {
        //   // 点击第一个节点
        //   // const firstNode = document.querySelector('.el-tree-node')
        //   // firstNode.click()
        // })
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.currentTreeNode !== '' ? this.currentTreeNode : this.treeData[0].id)
          this.$nextTick(() => {
            document.querySelector(".is-current").firstChild.click()
          })
        })
        this.treeLoading = false
        console.log(this.currentTreeNode, 'qq')
      }).catch(err => {
        this.treeLoading = false
      })
    },
    // 区域树节点点击
    handleNodeClick(data) {
      console.log(data, '当前点击的树节点')
      this.queryParams.organizationParentId = data.id
      this.queryParams.organizationName = '' // 清空查询条件 部门名称
      this.parentOrganiName = data.text // 上级区域
      this.currentTreeNode = data.id // 保存当前点击的树节点的id
      this.handleSearch('btn')
    },

参考:

https://blog.csdn.net/qq_27331631/article/details/104851801

https://blog.csdn.net/qq_27331631/article/details/107568589?utm_medium=distribute.pc_relevant.none-task-blog-baidulandingword-3&spm=1001.2101.3001.4242

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值