优秀的编程知识分享平台

网站首页 > 技术文章 正文

Vue ElementUI el-tree 组件鼠标双击事件

nanyue 2024-08-28 19:04:56 技术文章 9 ℃

目前 element-ui 的 tree 控件不支持双击事件,但是这个时候有个需求,需要双击事件.

没办法,只能自己写一个咯,记录下自己写的方法

简单引用,添加@node-click事件

	<el-tree
            ref="tree"
            node-key="id"
            :data="treeData"
            :expand-on-click-node="false"
            @node-click="handleNodeClick">
    </el-tree>

在handleNodeClick事件中处理双击事件

data() {
    return {
    	//定义点击次数,默认0次
        treeClickCount: 0,
    }
},
methods: {
	//节点点击事件
	handleNodeClick(data, node) {
		//记录点击次数
		this.treeClickCount++;
		//单次点击次数超过2次不作处理,直接返回,也可以拓展成多击事件
		if (this.treeClickCount >= 2) {
			return;
		}
		//计时器,计算300毫秒为单位,可自行修改
		this.timer = window.setTimeout(() => {
			if (this.treeClickCount == 1) {
				//把次数归零
				this.treeClickCount = 0;
				//单击事件处理
				this.console('单击事件,可在此处理对应逻辑')
				
			} else if (this.treeClickCount > 1) {
				//把次数归零
				this.treeClickCount = 0;
				//双击事件
				this.console('双击事件,可在此处理对应逻辑')
			}
		}, 300);
	
	}
}
结束
最近发表
标签列表