Skip to content

Commit 0f1a95d

Browse files
committed
adding vue 3 composition api
1 parent c2ddeec commit 0f1a95d

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

demo/vue3js.html

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,58 @@ <h1>How to integrate GridStack.js with Vue.js</h1>
3030
</main>
3131
<script type="module">
3232
import { createApp } from "https://cdn.jsdelivr.net/npm/vue@3.0.0/dist/vue.esm-browser.js";
33+
import { ref, onMounted } from "vue";
3334

3435
createApp({
35-
data() {
36+
setup() {
37+
let count = ref(0);
38+
let info = ref("");
39+
let grid = null;
40+
const items = [
41+
{ x: 2, y: 1, h: 2 },
42+
{ x: 2, y: 4, w: 3 },
43+
{ x: 4, y: 2 },
44+
{ x: 3, y: 1, h: 2 },
45+
{ x: 0, y: 6, w: 2, h: 2 },
46+
];
47+
48+
onMounted(() => {
49+
grid = GridStack.init({
50+
float: true,
51+
cellHeight: "70px",
52+
minRow: 1,
53+
});
54+
55+
// 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
56+
grid.on("dragstop", (event, element) => {
57+
const node = element.gridstackNode;
58+
// `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/
59+
info.value = `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`;
60+
});
61+
});
62+
63+
function addNewWidget() {
64+
const node = items[count.value] || {
65+
x: Math.round(12 * Math.random()),
66+
y: Math.round(5 * Math.random()),
67+
w: Math.round(1 + 3 * Math.random()),
68+
h: Math.round(1 + 3 * Math.random()),
69+
};
70+
node.id = node.content = String(count.value++);
71+
grid.addWidget(node);
72+
}
73+
3674
return {
37-
// grid: undefined, // <-- don't do that. see https://github.com/gridstack/gridstack.js/issues/1635
38-
count: 0,
39-
info: "",
75+
info,
76+
addNewWidget,
4077
};
4178
},
42-
items: [
43-
{ x: 2, y: 1, h: 2 },
44-
{ x: 2, y: 4, w: 3},
45-
{ x: 4, y: 2},
46-
{ x: 3, y: 1, h: 2 },
47-
{ x: 0, y: 6, w: 2, h: 2 }
48-
],
79+
4980
watch: {
5081
/**
5182
* Clear the info text after a two second timeout. Clears previous timeout first.
5283
*/
53-
info: function (newVal, oldVal) {
84+
info: function (newVal) {
5485
if (newVal.length === 0) return;
5586

5687
window.clearTimeout(this.timerId);
@@ -59,29 +90,6 @@ <h1>How to integrate GridStack.js with Vue.js</h1>
5990
}, 2000);
6091
},
6192
},
62-
mounted: function () {
63-
// Provides access to the GridStack instance across the Vue component.
64-
this.grid = GridStack.init({ float: true, cellHeight: '70px', minRow: 1 });
65-
66-
// 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
67-
this.grid.on("dragstop", (event, element) => {
68-
const node = element.gridstackNode;
69-
// `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/
70-
this.info = `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`;
71-
});
72-
},
73-
methods: {
74-
addNewWidget: function () {
75-
const node = this.$options.items[this.count] || {
76-
x: Math.round(12 * Math.random()),
77-
y: Math.round(5 * Math.random()),
78-
w: Math.round(1 + 3 * Math.random()),
79-
h: Math.round(1 + 3 * Math.random()),
80-
};
81-
node.id = node.content = String(this.count++);
82-
this.grid.addWidget(node);
83-
},
84-
},
8593
}).mount("#app");
8694
</script>
8795
</body>

0 commit comments

Comments
 (0)