09 - Dynamic Add/Remove
vue
<script setup>
import { reactive } from 'vue';
import { GridLayout, GridItem } from 'vue-grid-layout-v3';
const state = reactive({
layout: [
{ 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: 6, y: 0, w: 2, h: 2, i: '3' },
{ x: 8, y: 0, w: 2, h: 2, i: '4' },
],
draggable: true,
resizable: true,
colNum: 12,
index: 0,
});
state.index = state.layout.length;
function addItem() {
// Add a new item. It must have a unique key!
state.layout.push({
x: (state.layout.length * 2) % (state.colNum || 12),
y: state.layout.length + (state.colNum || 12), // puts it at the bottom
w: 2,
h: 2,
i: state.index,
});
// Increment the counter to ensure key is always unique.
state.index++;
}
function removeItem(val) {
const index = state.layout.map(item => item.i).indexOf(val);
state.layout.splice(index, 1);
}
</script>
<template>
<div>
<div class="layoutJSON">
Displayed as <code>[x, y, w, h]</code>:
<div class="columns">
<div class="layoutItem" v-for="item in state.layout" :key="item.i">
<b>{{item.i}}</b>: [{{item.x}}, {{item.y}}, {{item.w}}, {{item.h}}]
</div>
</div>
</div>
<div class="controls">
<button @click="addItem">Add an item dynamically</button>
<input type="checkbox" v-model="state.draggable" /> Draggable
<input type="checkbox" v-model="state.resizable" /> Resizable
</div>
<GridLayout
v-model:layout="state.layout"
:col-num="state.colNum"
:row-height="30"
:is-draggable="state.draggable"
:is-resizable="state.resizable"
:vertical-compact="true"
:use-css-transforms="true"
>
<GridItem
v-for="item in state.layout"
:key="item.i"
:static="item.static"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
>
<span class="text">{{item.i}}</span>
<span class="remove" @click="removeItem(item.i)">x</span>
</GridItem>
</GridLayout>
</div>
</template>
<style scoped>
button {
white-space: nowrap;
text-align: center;
border: 1px solid #d9d9d9;
cursor: pointer;
padding: 0 4px;
font-size: 14px;
border-radius: 2px;
background: #fff;
}
.controls {
padding: 10px 0;
}
.vue-grid-layout {
background: #eee;
}
.vue-grid-item:not(.vue-grid-placeholder) {
background: #ccc;
border: 1px solid black;
}
.vue-grid-item .text {
font-size: 24px;
text-align: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
height: 100%;
width: 100%;
}
.vue-grid-item .remove {
position: absolute;
right: 2px;
top: 0;
cursor: pointer;
}
.layoutJSON {
background: #ddd;
border: 1px solid black;
margin-top: 10px;
padding: 10px;
}
.columns {
columns: 120px;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132