当前位置: 代码网 > it编程>前端脚本>Vue.js > element plus tree拖动节点交换位置和改变层级问题(解决方案)

element plus tree拖动节点交换位置和改变层级问题(解决方案)

2024年05月26日 Vue.js 我要评论
图层list里有各种组件,用element plus的tree来渲染,可以把图片等组件到面板里,面板是容器,非容器组件,比如图片、文本等,就不能让其他组件拖进来。主要在于allow-drop属性的回调

图层list里有各种组件,用element plus的tree来渲染,可以把图片等组件到面板里,面板是容器,非容器组件,比如图片、文本等,就不能让其他组件拖进来。

主要在于allow-drop属性的回调函数编写,要理清楚思路,什么时候allow-drop返回true,什么时候返回false。

allow-drop回调函数参数里的type,有三个值:before、after、inner,这是解决问题的关键点。

比如把a节点向b节点拖动,before表示把a拖到b的前面,after表示把a拖到b的后面,inner表示把a拖到b里面去。

对于面板容器来说,这三种位置都是允许drop的,allow-drop的返回值一定是true;

对于其他组件来说,只有before和after是允许的,所以当type不等于inner的时候,allow-drop的返回值才是true,否则是false。

html代码:

关键点:draggable为true,设置allow-drop方法

<el-tree ref="treeref" style="width: 200px; margin-top: 10px" :data="designerstore.cachecomponents"
						draggable node-key="id" highlight-current v-if="flag"
						:current-node-key="designerstore.currentcpt ? designerstore.currentcpt.id : null"
						:allow-drop="allowdrop" :props="{ label: 'cpttitle', id: 'id', children: 'children' }"
						empty-text="无图层">
						<template #default="{ node, data }">
							<span class="custom-tree-node" @dblclick.stop="editcatename(data, node)"
								@mousedown="showconfigbar(data)" @contextmenu.prevent="showcontextmenu">
								<img class="selecteditem-icon"
									:src="require('@/assets/icon/components/' + geticon(data) + '.svg')" />
								<el-input v-model="data.cpttitle" v-if="isedit === data.id" :ref="data.id"
									placeholder="请输入" @blur="editsave()" @keyup.enter="editsave()" size="small" />
								<span v-else>{{ data.cpttitle }}</span>
							</span>
						</template>
					</el-tree>

allow-drop方法:

allowdrop(draggingnode, dropnode, type) {
				if (dropnode.data.cptkey === 'cpt-panel') {
					return true
				} else {
					return type !== 'inner'
				}
			}

最终效果:

到此这篇关于element plus tree拖动节点交换位置和改变层级的文章就介绍到这了,更多相关element plus tree拖动节点内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com