当前位置: 首页 > news >正文

vue-grid-layout元素交换位置及大小

div元素
  <grid-layout:layout="map":col-num="10":maxRows="6":row-height="rowHeight":is-draggable="true":is-resizable="true":autoSize="false":is-mirrored="false":vertical-compact="true":preventCollision = "false":margin="[10, 10]":use-css-transforms="true"><grid-item v-for="item in map":x="item.x":y="item.y":w="item.w":h="item.h":i="item.i":key="item.i"@move="moveEvent"@moved="movedEvent"@resized="resizeEvent"class="gridItem"><h1 >{{item.i}}</h1></grid-item></grid-layout>
demo数据:
	  windowHeight: 0,rowHeight:130,layout: [{ x: 0, y: 0, w: 3, h: 2, i: "0"},{ x: 3, y: 0, w: 4, h: 3, i: "1" },{ x: 7, y: 0, w: 3, h: 2, i: "2" },{ x: 0, y: 2, w: 3, h: 2, i: "10" },{ x: 3, y: 3, w: 4, h: 3, i: "11" },{ x: 7, y: 2, w: 3, h: 2, i: "12" },{ x: 0, y: 4, w: 3, h: 2, i: "20" },{ x: 7, y: 4, w: 3, h: 2, i: "22" },],map:[],curBox:'',
交换的主要方法:
	moveEvent(i, x, y) {this.curBox = i},movedEvent(i, x, y){//   1 根据移动位置,循环数据源当前位置是否有元素,不包含自己的位置if (y>11) this.map = JSON.parse(JSON.stringify(this.layout));const item1 = this.layout.find((item) => item.x <= x && x < (item.x+item.w) && item.y <=y && (y < item.y+item.h) && item.i != i);const item2 = this.layout.find((item) =>  item.i == i);// console.log(item1,item2)item1 && this.changePosition(item1,item2)if(!item1) this.layout = JSON.parse(JSON.stringify(this.map));localStorage.setItem("layout",JSON.stringify(this.layout))},changePosition(item1, item2) {// console.log(item1, item2);// 定义中间变量交换const temp = JSON.parse(JSON.stringify(item1));const temp2 = JSON.parse(JSON.stringify(item2));//   注意:修改的是影响显示的实际数组 map this.layout.forEach((item) => {// 找到 item1,将 item1 的信息换成item2if (item.i === item1.i) {item.x = temp2.x;item.y = temp2.y;item.w = temp2.witem.h = temp2.h}if (item.i === item2.i) {item.x = temp.x;item.y = temp.y;item.w = temp.w;item.h = temp.h;}});// 实现交换后,及时同步 数据映射this.map = JSON.parse(JSON.stringify(this.layout));},
扩展:动态行高、echarts图表动态渲染、指定元素可拖动

动态计算行高:

 mounted() {if (localStorage.getItem('layout')){this.layout = JSON.parse(localStorage.getItem('layout'))}this.map = JSON.parse(JSON.stringify(this.layout));localStorage.setItem("layout",JSON.stringify(this.layout))this.onWindowResize()window.addEventListener('resize', this.onWindowResize); // 添加事件监听器},beforeDestroy() {window.removeEventListener('resize', this.onWindowResize); // 移除事件监听器以避免内存泄漏},onWindowResize() {this.windowHeight = window.innerHeight; // 更新窗口高度this.rowHeight = (this.windowHeight-(10*7))/6 //margin的高度*(行数+1)},

echarts图表动态渲染:

//div中,注意data如果是方法中有更新需重新赋值给datalist
<component :is="item.chart" :chart-date="dataList[item.i]"></component>//实际echarts中动态更新视图内容const elementResizeDetectorMaker = require("element-resize-detector");let erd = elementResizeDetectorMaker();
//mounted中(ref对应div绑定的ref)this.$nextTick(() => {erd.listenTo(this.$refs.radarBasic, () => {this.$nextTick(function () {//使echarts尺寸重置this.myChart.resize();});});})

指定元素可拖动:drag-ignore-from=“.draggable” drag-allow-from=“.itemTitle”

<grid-item v-for="item in map":x="item.x":y="item.y":w="item.w":h="item.h":i="item.i":key="item.i"@move="moveEvent"@moved="movedEvent"@resized="resizeEvent"drag-ignore-from=".draggable"drag-allow-from=".itemTitle"class="cardItem_dark cardItem"> <div class="title-top"></div><div class="itemTitle centerItem">{{item.title}}</div><div class="draggable" v-if="['2','4','5','7'].includes(item.i)" :style="'width:'+ 9.3*item.w+'vw;'+'height:'+ (rowHeight*item.h+20*(item.h-1.1))+'px;padding-top:5%'"><component :is="item.chart" :chart-date="dataList[item.i]"></component></div><div class="draggable" v-else :style="'width:'+ 9*item.w+'vw;'+'height:'+ (rowHeight*item.h+20*(item.h-1.1))+'px;padding-top:5px'"><component :is="item.chart" :chart-date="dataList[item.i]"></component></div></grid-item>

参考链接:
Vue Grid Layout - 适用Vue.js的栅格布局系统(项目实例)
Vue Grid Layout -️ 适用Vue.js的栅格布局系统(保姆级使用教程)
Vue Grid Layout 官方文档

http://www.dtcms.com/a/302217.html

相关文章:

  • Python Pandas.merge函数解析与实战教程
  • 【数学建模论文学习笔记】基于历史数据的蔬菜类商品定价与补货决策模型
  • Java从入门到精通!第十八天(JDK17安装以及网络编程) 完结篇!!!
  • 7.29 技巧|
  • 【数据库】使用Sql Server将分组后指定字段的行数据转为一个字段显示,并且以逗号隔开每个值,收藏不迷路
  • C# 接口(interface 定义接口的关键字)
  • JVM 垃圾回收机制全景解析:从对象回收到收集算法
  • Spring Boot日志开发实战手册:集成/输出/级别控制/持久化精要
  • MySQL 锁机制 15 连问 · 面试速答版
  • openeuler24.03部署k8s1.32.7集群(一主两从)
  • C primer plus (第六版)第九章 编程练习第6题
  • 基于YOLO11的电梯电瓶车检测系统:让电梯更安全
  • Redis反弹Shell
  • 《Java 程序设计》第 8 章 - Java 常用核心类详解
  • 安装研华板卡驱动
  • 重庆地区通信安全员考试题库及答案
  • tsc命令深入全面讲解
  • 零基础学习性能测试第六章:性能难点-Jmeter文件上传场景压测
  • 【智慧物联网平台】编译jar环境 Linux 系统编译IOT物联网——仙盟创梦IDE
  • React Immer 不可变数据结构的处理
  • Jmeter 性能测试监控之ServerAgent
  • Jmeter的元件使用介绍:(九)监听器详解
  • 10、Docker Compose 安装 MySQL
  • Redis数据量过大的隐患:查询会变慢吗?如何避免?
  • CacheGen:用于快速大语言模型推理服务的 KV 缓存压缩与流式传输
  • 【linux】高可用集群Keepalived
  • 如何给电脑换个ip地址?电脑换ip几种方法
  • 【计算机网络】OSI七层模型
  • AR眼镜:工业4.0时代高风险作业的安全守护者
  • 设计模式(二十二)行为型:策略模式详解