|
| 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>Vue3 Gridstack: Gridstack DOM with Vue Rendering</title> |
| 7 | + <link rel="stylesheet" href="demo.css"/> |
| 8 | + <script src="../dist/gridstack-all.js"></script> |
| 9 | + </head> |
| 10 | + <body> |
| 11 | + <main id="app"> |
| 12 | + <a href="./index.html">Back to All Demos</a> |
| 13 | + <h1>Vue3: Gridstack Controls Vue Rendering Grid Item Content</h1> |
| 14 | + <p> |
| 15 | + <strong>Use Vue3 render functions to dynamically render only the grid item content.</strong><br /> |
| 16 | + GridStack is handles when items are added/removed, rendering grid item element, and Vue handles rendering <i>only</i> the item content. |
| 17 | + </p> |
| 18 | + <p> |
| 19 | + Helpful Resources: |
| 20 | + <ul> |
| 21 | + <li><a href="https://vuejs.org/guide/extras/render-function.html#render-functions-jsx" target="_blank">Vue Render Functions</a></li> |
| 22 | + </ul> |
| 23 | + </p> |
| 24 | + <p> |
| 25 | + Notes: |
| 26 | + <ul> |
| 27 | + <li>This implementation currently does not support nested grid</li> |
| 28 | + </ul> |
| 29 | + </p> |
| 30 | + <button type="button" @click="addNewWidget()">Add Widget</button> {{ info }} |
| 31 | + <div class="grid-stack"></div> |
| 32 | + </main> |
| 33 | + <script type="module"> |
| 34 | + import { createApp, ref, onMounted, onBeforeUnmount, h, render, toRefs } from "https://cdn.jsdelivr.net/npm/vue@3.0.11/dist/vue.esm-browser.js"; |
| 35 | + |
| 36 | + const GridContentComponent = { |
| 37 | + props: { |
| 38 | + itemId: { |
| 39 | + type: [String, Number], |
| 40 | + required: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + emits: ['remove'], |
| 44 | + setup(props, { emit }) { |
| 45 | + const { itemId } = toRefs(props) |
| 46 | + |
| 47 | + onBeforeUnmount(() => { |
| 48 | + console.log(`In vue onBeforeUnmount for item ${itemId.value}`) |
| 49 | + }); |
| 50 | + |
| 51 | + function handleRemove() { |
| 52 | + emit('remove', itemId.value) |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + itemId, |
| 57 | + handleRemove, |
| 58 | + } |
| 59 | + }, |
| 60 | + template: ` |
| 61 | + <div class="my-custom-grid-item-content"> |
| 62 | + <button @click=handleRemove>X</button> |
| 63 | + <p> |
| 64 | + Vue Grid Item Content {{ itemId }} |
| 65 | + </p> |
| 66 | + </div> |
| 67 | + ` |
| 68 | + } |
| 69 | + |
| 70 | + createApp({ |
| 71 | + setup() { |
| 72 | + let info = ref(""); |
| 73 | + let grid = null; // DO NOT use ref(null) as proxies GS will break all logic when comparing structures... see https://github.com/gridstack/gridstack.js/issues/2115 |
| 74 | + const items = [ |
| 75 | + { id: 1, x: 2, y: 1, h: 2 }, |
| 76 | + { id: 2, x: 2, y: 4, w: 3 }, |
| 77 | + { id: 3, x: 4, y: 2 }, |
| 78 | + { id: 4, x: 3, y: 1, h: 2 }, |
| 79 | + { id: 5, x: 0, y: 6, w: 2, h: 2 }, |
| 80 | + ]; |
| 81 | + let count = ref(items.length); |
| 82 | + |
| 83 | + onMounted(() => { |
| 84 | + grid = GridStack.init({ // DO NOT user grid.value = GridStack.init(), see above |
| 85 | + float: true, |
| 86 | + cellHeight: "70px", |
| 87 | + minRow: 1, |
| 88 | + }); |
| 89 | + |
| 90 | + grid.on('added', function(event, items) { |
| 91 | + for (const item of items) { |
| 92 | + const itemEl = item.el |
| 93 | + const itemElContent = itemEl.querySelector('.grid-stack-item-content') |
| 94 | + |
| 95 | + const itemId = item.id |
| 96 | + |
| 97 | + // Use Vue's render function to create the content |
| 98 | + // See https://vuejs.org/guide/extras/render-function.html#render-functions-jsx |
| 99 | + // Supports: emit, slots, props, attrs, see onRemove event below |
| 100 | + const itemContentVNode = h( |
| 101 | + GridContentComponent, |
| 102 | + { |
| 103 | + itemId: itemId, |
| 104 | + onRemove: (itemId) => { |
| 105 | + grid.removeWidget(itemEl) |
| 106 | + } |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + // Render the vue node into the item element |
| 111 | + render(itemContentVNode, itemElContent) |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + grid.on('removed', function(event, items) { |
| 116 | + for (const item of items) { |
| 117 | + const itemEl = item.el |
| 118 | + const itemElContent = itemEl.querySelector('.grid-stack-item-content') |
| 119 | + // Unmount the vue node from the item element |
| 120 | + // Calling render with null will allow vue to clean up the DOM, and trigger lifecycle hooks |
| 121 | + render(null, itemElContent) |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + grid.load(items); |
| 126 | + }); |
| 127 | + |
| 128 | + function addNewWidget() { |
| 129 | + const node = items[count.value] || { |
| 130 | + x: Math.round(12 * Math.random()), |
| 131 | + y: Math.round(5 * Math.random()), |
| 132 | + w: Math.round(1 + 3 * Math.random()), |
| 133 | + h: Math.round(1 + 3 * Math.random()), |
| 134 | + }; |
| 135 | + node.id = String(count.value++); |
| 136 | + grid.addWidget(node); |
| 137 | + } |
| 138 | + |
| 139 | + return { |
| 140 | + info, |
| 141 | + addNewWidget, |
| 142 | + }; |
| 143 | + }, |
| 144 | + |
| 145 | + watch: { |
| 146 | + /** |
| 147 | + * Clear the info text after a two second timeout. Clears previous timeout first. |
| 148 | + */ |
| 149 | + info: function (newVal) { |
| 150 | + if (newVal.length === 0) return; |
| 151 | + |
| 152 | + window.clearTimeout(this.timerId); |
| 153 | + this.timerId = window.setTimeout(() => { |
| 154 | + this.info = ""; |
| 155 | + }, 2000); |
| 156 | + }, |
| 157 | + }, |
| 158 | + }).mount("#app"); |
| 159 | + </script> |
| 160 | + </body> |
| 161 | +</html> |
0 commit comments