el-tree 拖拽事件隔离:实现句柄独立控制,区域分离

html 复制代码
<template>
  <el-tree
    ref="treeRef"
    class="tree-container"
    :data="treeList"
    node-key="courseContentId"
    default-expand-all
    draggable
    :expand-on-click-node="true"
    :allow-drop="allowDrop"
    :allow-drag="allowDrag"
    @node-drag-start="handleNodeDragStart"
    @node-drag-end="handleNodeDragEnd"
    @node-drop="handleNodeDrop"
  >
    <template #default="{ data }">
      <div class="drag-icon" @mousedown="handleDragHandleMouseDown">⋮⋮</div>
      {{ data.label }}
    </template>
  </el-tree>
</template>

<script setup lang="ts">
import { ref } from "vue";

const treeList = ref([
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1",
          },
        ],
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 3",
    children: [
      {
        label: "Level two 3-1",
        children: [
          {
            label: "Level three 3-1-1",
          },
        ],
      },
      {
        label: "Level two 3-2",
        children: [
          {
            label: "Level three 3-2-1",
          },
        ],
      },
    ],
  },
]);

const isDragHandleClicked = ref(false);

const handleDragHandleMouseDown = () => {
  isDragHandleClicked.value = true;
};

const handleNodeDragEnd = () => {
  isDragHandleClicked.value = false;
};

const allowDrag = () => {
  if (!isDragHandleClicked.value) {
    return false;
  }
  return true;
};

const handleNodeDragStart = (node, event) => {
  const target = event.target;

  const dragIconElement = target.closest(".drag-icon");
  if (!dragIconElement) {
    if (event.dataTransfer) {
      event.dataTransfer.dropEffect = "none";
      event.dataTransfer.effectAllowed = "none";
    }
    isDragHandleClicked.value = false;
    return false;
  }
  return true;
};

const allowDrop = () => {
  return true;
};

const handleNodeDrop = async () => {
  isDragHandleClicked.value = false;
};
</script>

<style lang="less" scoped>
.drag-icon {
  margin-right: 10px;
}
</style>
相关推荐
Csvn7 小时前
原型链、this 与闭包内存:三座深水区一次打通(含内存泄漏排查实战)
前端·javascript
万维易源8 小时前
趣味项目素材|免费脑筋急转弯查询接口实战接入教程
javascript·免费api·脑筋急转弯查询·脑筋急转弯接口
SLD_Allen10 小时前
Go语言runtime全景
开发语言·javascript·golang
风萧何11 小时前
架构艺术,是权衡的艺术。
前端·javascript·架构
偷偷挖矿的牛马矿工11 小时前
execa NodeJs新一代child_process的强力替代品
javascript
水獭比特12 小时前
工作流都公开了,为什么还不能继承 Owner 权限?
javascript·人工智能·node.js
ly768914 小时前
Vue 3 从入门到工程实践:基础语法、组件化、路由、状态管理与项目部署
前端·javascript·vue.js
四千岁14 小时前
抓包LangChain-了解LangChain的原理
前端·javascript·后端
用户2986985301415 小时前
使用 JavaScript 在 React 中实现 Word 转 PDF
前端·javascript·react.js