绑定容器
设置bounded属性,可以限制部件拖动范围限制在容器内
Displayed as
[x, y, w, h] (第几列,第几行,宽度,高度)
: 0 : [0, 0, 2, 2]
1 : [2, 0, 2, 4]
2 : [4, 0, 2, 5]
3 : [6, 0, 2, 3]
4 : [8, 0, 2, 3]
5 : [10, 0, 2, 3]
6 : [0, 5, 2, 5]
7 : [2, 5, 2, 5]
8 : [4, 5, 2, 5]
9 : [6, 4, 2, 4]
10 : [8, 4, 2, 4]
11 : [10, 4, 2, 4]
12 : [0, 10, 2, 5]
13 : [2, 10, 2, 5]
14 : [4, 8, 2, 4]
15 : [6, 8, 2, 4]
16 : [8, 10, 2, 5]
17 : [10, 4, 2, 2]
18 : [0, 9, 2, 3]
19 : [2, 6, 2, 2]
<template>
<div class="container">
<DisplayLayout :layout="state.layout" />
<b-checkbox v-model="state.draggable">Draggable</b-checkbox>
<b-checkbox v-model="state.resizable">Resizable</b-checkbox>
<b-checkbox v-model="state.bounded">Bounded</b-checkbox>
<b-divider />
<b-grid-layout
v-model:layout="state.layout"
:col-num="12"
:row-height="30"
:is-draggable="state.draggable"
:is-resizable="state.resizable"
:is-bounded="state.bounded"
:vertical-compact="true"
:use-css-transforms="true"
>
<b-grid-item
v-for="item in state.layout"
:key="item.i"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
>
<div class="item-box">
<span class="text">{{ item.i }}</span>
</div>
</b-grid-item>
</b-grid-layout>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({
layout: [
{ x: 0, y: 0, w: 2, h: 2, i: '0' },
{ x: 2, y: 0, w: 2, h: 4, i: '1' },
{ x: 4, y: 0, w: 2, h: 5, i: '2' },
{ x: 6, y: 0, w: 2, h: 3, i: '3' },
{ x: 8, y: 0, w: 2, h: 3, i: '4' },
{ x: 10, y: 0, w: 2, h: 3, i: '5' },
{ x: 0, y: 5, w: 2, h: 5, i: '6' },
{ x: 2, y: 5, w: 2, h: 5, i: '7' },
{ x: 4, y: 5, w: 2, h: 5, i: '8' },
{ x: 6, y: 4, w: 2, h: 4, i: '9' },
{ x: 8, y: 4, w: 2, h: 4, i: '10' },
{ x: 10, y: 4, w: 2, h: 4, i: '11' },
{ x: 0, y: 10, w: 2, h: 5, i: '12' },
{ x: 2, y: 10, w: 2, h: 5, i: '13' },
{ x: 4, y: 8, w: 2, h: 4, i: '14' },
{ x: 6, y: 8, w: 2, h: 4, i: '15' },
{ x: 8, y: 10, w: 2, h: 5, i: '16' },
{ x: 10, y: 4, w: 2, h: 2, i: '17' },
{ x: 0, y: 9, w: 2, h: 3, i: '18' },
{ x: 2, y: 6, w: 2, h: 2, i: '19' }
],
draggable: true,
resizable: true,
bounded: true
})
</script>
<style scoped>
.container {
.item-box {
padding: 8px;
.text {
font-size: 18px;
text-align: center;
margin: auto;
height: 100%;
width: 100%;
}
.remove {
position: absolute;
right: 2px;
top: 0;
cursor: pointer;
}
}
}
</style>