vue3-drr-grid-layout 的单元格有move拖动中和moved拖动完成事件,通过移动事件我们可以获取最新的 vue3-drr-grid-layout布局并保存起来。
<!-- 绑定拖动事件 -->
<grid-item @move="moveEvent" @moved="movedEvent">
<template>
<div>{{ message }}</div>
<div id="content" style="width: 50%">
<grid-layout
v-model:layout="layout"
:col-num="6"
:row-height="30"
:margin="[10, 10]"
style="padding: 10px"
>
<template #default="{ gridItemProps }">
<grid-item
v-for="item in layout"
:key="item.i"
v-bind="gridItemProps"
:is-draggable="0 != item.i"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
:auto-size="false"
class="item"
@move="moveEvent"
@moved="movedEvent"
>
<div>
<span class="span">{{
item.i == 0 ? "不能移动" : "可移动"
}}</span>
</div>
</grid-item>
</template>
</grid-layout>
</div>
</template>
<script setup lang="ts">
/*
vue3-drr-grid-layout 中文文档
https://www.itxst.com/vue3-drr-grid-layout/tutorial.html
*/
import { getCurrentInstance, onMounted, ref } from "vue";
import { GridLayout, GridItem } from "vue3-drr-grid-layout";
import "vue3-drr-grid-layout/dist/style.css";
const layout = ref([
{ x: 0, y: 0, w: 2, h: 2, i: 0 },
{ x: 2, y: 0, w: 2, h: 2, i: 1 },
{ x: 4, y: 0, w: 2, h: 2, i: 2 },
{ x: 0, y: 1, w: 6, h: 2, i: 3 },
]);
const message = ref("");
const isDraggable = ref(false);
//单元格移动中的事件
const moveEvent = (i, newX, newY) => {
message.value = "移动中 i=" + i + ", X=" + newX + ", Y=" + newY;
};
//单元格移动后的事件
const movedEvent = (i, newX, newY) => {
message.value = "移动完成 i=" + i + ", X=" + newX + ", Y=" + newY;
};
</script>
<style scoped>
.droppable-element {
width: 120px;
text-align: center;
background: #fdd;
border: 1px solid black;
margin: 10px 0;
padding: 10px;
}
.item {
display: flex;
justify-content: space-between;
}
.item .span {
padding: 10px;
border: solid 1px #ddd;
background-color: rgb(171, 171, 171);
color: #000;
font-size: 11px;
}
</style>