vue使用event.dataTransfer实现A容器数据拖拽复制到到B容器

A容器代码

  • template
bash 复制代码
 <div class="field-list">
   <div
     v-for="col in columns"
     :key="col.columnName"
     class="field-item"
     draggable="true"
     @dragstart="onDragStart($event, col, 'field')"
   >
     <i class="el-icon-document"></i>
     <span class="field-name">{{ col.columnName }}</span>
     <span class="field-comment">{{ col.columnComment }}</span>
   </div>
 </div>
  • 方法
bash 复制代码
function onDragStart(event, item, type) {
  event.dataTransfer.setData("type", type);
  event.dataTransfer.setData("data", JSON.stringify(item));
}

B容器代码

bash 复制代码
 <div class="condition-drop" @dragover.prevent @drop="onDropCondition">
   <div
     v-for="(cond, idx) in form.whereConfig.conditions"
     :key="idx"
     class="condition-row"
   >
     <el-tag>{{ cond.field }}</el-tag>
   </div>
 </div>
  • 方法
bash 复制代码
function onDropCondition(event) {
  const type = event.dataTransfer.getData("type");
  if (type !== "field") return;

  const data = JSON.parse(event.dataTransfer.getData("data"));
  const exists = form.whereConfig.conditions.find((c) => c.field === data.columnName);
  if (!exists) {
    form.whereConfig.conditions.push({
      field: data.columnName,
    });
  }
}
相关推荐
高级程序源3 小时前
django校园寻物通失物招领平台32261-计算机课程设计、毕业设计
vue.js·python·mysql·小程序·django·flask·课程设计
WeiXin_DZbishe3 小时前
基于springboot宠物领养系统-计算机毕设 附源码【课程设计】94217
vue.js·spring boot·python·django·php·课程设计
架构技术专栏4 小时前
Agent 工具调用:从工具声明到执行反馈
前端·人工智能·后端
别动我齐刘海4 小时前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
志尊宝5 小时前
Vue3 零基础每日笔记(071):暗黑模式与主题切换——CSS 变量 + Element Plus 暗黑方案
前端·css·笔记·vue·html·前端开发·软件开发
WeiXin_DZbishe6 小时前
基于springboot城市公交实时运营与调度系统-计算机毕设 附源码【课程设计】66173
java·vue.js·spring boot·后端·python·php·课程设计
刘发财6 小时前
前端如何生成10000页PDF文件?
前端·javascript·开源
WeiXin_DZbishe6 小时前
基于Springboot+AI的kuall音乐广场的设计与实现-计算机毕设 附源码【课程设计】12715
vue.js·人工智能·spring boot·后端·python·php·课程设计
A接拉起0076 小时前
2026 年 JavaScript 开发者需要了解的知识
前端·javascript·后端
_馒头6 小时前
pixiJs游戏开发实践 - 跳棋小游戏
javascript·vue.js