Skip to content

Commit 1fadbc8

Browse files
manangAngelo Mantellini
andauthored
Vue3 example (#1404)
* add vue3 example Co-authored-by: Angelo Mantellini <angelo.mantellini@cisco.com>
1 parent ceeb552 commit 1fadbc8

File tree

3 files changed

+133
-19
lines changed

3 files changed

+133
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
.npmrc
55
coverage
66
dist
7-
node_modules
7+
node_modules
8+
.vscode

.vscode/settings.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

demo/vue3js.html

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Gridstack.js Vue integration example</title>
7+
</head>
8+
<body>
9+
<link
10+
rel="stylesheet"
11+
href="https://unpkg.com/gridstack@2.0.1/dist/gridstack.css"
12+
/>
13+
<script src="https://unpkg.com/gridstack@2.0.1/dist/gridstack.all.js"></script>
14+
<main id="app">
15+
<h1>How to integrate GridStack.js with Vue.js</h1>
16+
<p>
17+
As with any virtualDOM-based framework, you need to check if Vue has
18+
rendered the DOM (or any updates to it) <strong>before</strong> you
19+
initialize GridStack or call its methods. As a basic example, check this
20+
component's <code>mounted</code> hook.
21+
</p>
22+
<p>
23+
If your app requires more complex render logic than the inline template
24+
in `addWidget`, consider
25+
<a
26+
href="https://github.com/gridstack/gridstack.js/tree/develop/doc#makewidgetel"
27+
>makeWidget</a
28+
>
29+
to let Vue deal with DOM rendering.
30+
</p>
31+
<button type="button" @click="addNewWidget()">Add Widget</button> {{ info
32+
}}
33+
<section class="grid-stack"></section>
34+
</main>
35+
<script type="module">
36+
import { createApp } from "https://unpkg.com/vue@3.0.0/dist/vue.esm-browser.js";
37+
38+
createApp({
39+
data() {
40+
return {
41+
count: 0,
42+
info: "",
43+
};
44+
},
45+
items: [
46+
{ x: 2, y: 1, width: 1, height: 2 },
47+
{ x: 2, y: 4, width: 3, height: 1 },
48+
{ x: 4, y: 2, width: 1, height: 1 },
49+
{ x: 3, y: 1, width: 1, height: 2 },
50+
{ x: 0, y: 6, width: 2, height: 2 },
51+
],
52+
watch: {
53+
/**
54+
* Clear the info text after a two second timeout. Clears previous timeout first.
55+
*/
56+
info: function (newVal, oldVal) {
57+
if (newVal.length === 0) return;
58+
59+
window.clearTimeout(this.timerId);
60+
this.timerId = window.setTimeout(() => {
61+
this.info = "";
62+
}, 2000);
63+
},
64+
},
65+
mounted: function () {
66+
// Provides access to the GridStack instance across the Vue component.
67+
this.grid = GridStack.init({
68+
float: true,
69+
cellHeight: "70px",
70+
minRow: 1,
71+
});
72+
73+
// Use an arrow function so that `this` is bound to the Vue instance. Alternatively, use a custom Vue directive on the `.grid-stack` container element: https://vuejs.org/v2/guide/custom-directive.html
74+
this.grid.on("dragstop", (event, element) => {
75+
const node = element.gridstackNode;
76+
// `this` will only access your Vue instance if you used an arrow function, otherwise `this` binds to window scope. see https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/
77+
this.info = `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`;
78+
});
79+
},
80+
methods: {
81+
addNewWidget: function () {
82+
const node = this.$options.items[this.count] || {
83+
x: Math.round(12 * Math.random()),
84+
y: Math.round(5 * Math.random()),
85+
width: Math.round(1 + 3 * Math.random()),
86+
height: Math.round(1 + 3 * Math.random()),
87+
};
88+
this.count++;
89+
this.grid.addWidget(
90+
`<div><div class="grid-stack-item-content">${this.count}</div></div>`,
91+
{ id: this.count, ...node }
92+
);
93+
},
94+
},
95+
}).mount("#app");
96+
</script>
97+
<style>
98+
/* Optional styles for demos */
99+
.btn-primary {
100+
color: #fff;
101+
background-color: #007bff;
102+
}
103+
104+
.btn {
105+
display: inline-block;
106+
padding: 0.375rem 0.75rem;
107+
line-height: 1.5;
108+
border-radius: 0.25rem;
109+
}
110+
111+
a {
112+
text-decoration: none;
113+
}
114+
115+
h1 {
116+
font-size: 2.5rem;
117+
margin-bottom: 0.5rem;
118+
}
119+
120+
.grid-stack {
121+
background: #fafad2;
122+
}
123+
124+
.grid-stack-item-content {
125+
color: #2c3e50;
126+
text-align: center;
127+
background-color: #18bc9c;
128+
}
129+
</style>
130+
</body>
131+
</html>

0 commit comments

Comments
 (0)