第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

el-tree加載完成后默認觸發(fā)點擊事件非默認選中(下)支持自定義節(jié)點

標簽:
Vue.js

前面那篇选中默认节点,有朋友留言说能不能支持自定义节点,自己想了想认为可行,思路主要利用el-tree 的current-node-keyhighlight-current属性,如图

<el-tree
            :data="deptTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            current-node-key="723fcc371a1c54ad53d899cf2c0f8c125d3951db899665a3be43700e354ebb4d"
            highlight-current
            node-key="id"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
            @node-drop="handleDrop"
            draggable
          >

代码如下

watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    // 默认点击Tree第一个节点
    deptTree(val) {
      if (val) {
        this.$nextTick(() => {
			//this.$refs.tree.setCurrentKey('723fcc371a1c54ad53d899cf2c0f8c125d3951db899665a3be43700e354ebb4d');
            document.querySelector(".is-current").firstChild.click();
        });
      }
    }
  },

有几个点需要说明,当开启current-node-key ,前端生成的树节点元素会带有is-current样式,通过这个我们进行判断
注意不要使用setCurrentKey来设置选中节点,实测无效,原因应该是页面元素未加载完成,如图:
在这里插入图片描述

整体思路:

后台返回需要点击的节点ID,需要注意的是只能是单个,非数组,格式要求(string, number),通过current-node-key绑定返回值,然后在watch中监听,即可完成自定义节点点击


BUG

有朋友指出current-node-key="723fcc371a1c54ad53d899cf2c0f8c125d3951db899665a3be43700e354ebb4d" 在进行动态赋值的时候会出现 [Vue warn]: Error in nextTick: “TypeError: Cannot read property ‘firstChild’ of null” 错误,第一眼看到的时候我猜想肯定是current-node-key 设置出现问题了,经过自己的尝试,已解决,对原来的代码进行了部分修改,具体如下:

	<el-tree
    	:data="deptTree"
     	:props="defaultProps"
     	:expand-on-click-node="false"
     	:filter-node-method="filterNode"
     	highlight-current
     	node-key="id"
     	ref="tree"
     	default-expand-all
     	@node-click="handleNodeClick"
     	@node-drop="handleDrop"
     	draggable
   >

JavaScript:

watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    // 选中的key
    nodeKey(val) {
      if (val) {
        this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(val);
            this.$nextTick(() => {
                document.querySelector(".is-current").firstChild.click();
            });
        });
      }
    }
},

mounted() {
    // 获取部门树数据
    this.getTreeDept();
},
methods: {
    /** 查询部门下拉树结构 */
    getTreeDept() {
      DeptAPI.treeDept().then(response => {
        this.nodeKey = '723fcc371a1c54ad53d899cf2c0f8c125d3951db899665a3be43700e354ebb4d';
        this.deptTree = response.treeList;
        this.checkedSet = response.checkedSet;
      });
    },
}

以上即可实现自定义节点默认点击事件,核心在于$nextTick的使用,Vue官网说明:在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消