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,
    });
  }
}
相关推荐
David凉宸1 小时前
Vue 3 项目的性能优化策略:从原理到实践
前端·vue.js·性能优化
小马_xiaoen2 小时前
Proxy 与 Reflect 从入门到实战:ES6 元编程核心特性详解
前端·javascript·ecmascript·es6
hoiii1872 小时前
MATLAB SGM(半全局匹配)算法实现
前端·算法·matlab
计算机学姐2 小时前
基于SpringBoot的民宿预定管理系统【三角色+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·mysql·信息可视化·intellij-idea·推荐算法
会编程的土豆3 小时前
新手前端小细节
前端·css·html·项目
广州华水科技3 小时前
单北斗GNSS在桥梁形变监测中的应用与技术进展分析
前端
我讲个笑话你可别哭啊3 小时前
鸿蒙ArkTS快速入门
前端·ts·arkts·鸿蒙·方舟开发框架
CherryLee_12103 小时前
基于poplar-annotation前端插件封装文本标注组件及使用
前端·文本标注
特立独行的猫a3 小时前
C++轻量级Web框架介绍与对比:Crow与httplib
开发语言·前端·c++·crow·httplib