|
1 | 1 | # Using with Vue components
|
2 | 2 |
|
3 |
| -`gridjs-vue` comes with a [helper method](helpers.md), `$gridjs.helper()`, with which you can insert a Vue component directly into a |
4 |
| -table cell or row. |
| 3 | +`gridjs-vue` comes with a [helper method](helpers.md), `$gridjs.helper()`, with which you can insert a Vue component |
| 4 | +directly into a table cell or row. |
5 | 5 |
|
6 | 6 | ```html
|
7 | 7 | <template>
|
@@ -48,7 +48,7 @@ table cell or row.
|
48 | 48 | ## Handling child component events
|
49 | 49 |
|
50 | 50 | If the Vue component you are inserting into the Grid.js instance has events that bubble up, you can handle them by
|
51 |
| -passing a `methods` object to `$gridjs.helper()` containing your event handler. |
| 51 | +passing `methods` to `$gridjs.helper()` containing your event handler, just as you would normally. |
52 | 52 |
|
53 | 53 | ```html
|
54 | 54 | <template>
|
@@ -98,4 +98,84 @@ passing a `methods` object to `$gridjs.helper()` containing your event handler.
|
98 | 98 | </script>
|
99 | 99 | ```
|
100 | 100 |
|
| 101 | +## Using A Global Event Bus |
| 102 | + |
| 103 | +Because it is instantiating within a Preact component, each Vue component becomes its own Vue instance and therefore cannot |
| 104 | +communicate back with the main Vue application unless using a separate global event bus, as shown in the following implementation. |
| 105 | + |
| 106 | +```js |
| 107 | +import Emittery from 'https://cdn.skypack.dev/emittery' |
| 108 | +import faker from 'https://cdn.skypack.dev/faker' |
| 109 | +import { Grid } from '../../dist/index.esm.js' |
| 110 | + |
| 111 | +window.emitter = new Emittery() |
| 112 | + |
| 113 | +const TestComponent = { |
| 114 | + name: 'TestComponent', |
| 115 | + props: { |
| 116 | + content: { |
| 117 | + type: String, |
| 118 | + default: 'Hello, world!' |
| 119 | + }, |
| 120 | + emitter: { |
| 121 | + type: Object, |
| 122 | + default: null |
| 123 | + } |
| 124 | + }, |
| 125 | + data() { |
| 126 | + return { |
| 127 | + styling: 'color: #f00;' |
| 128 | + } |
| 129 | + }, |
| 130 | + methods: { |
| 131 | + emit(args) { |
| 132 | + return window.emitter.emit(args) |
| 133 | + } |
| 134 | + }, |
| 135 | + template: ` |
| 136 | + <div> |
| 137 | + <div :style="styling" v-html="content" @click="emit('sayHello')"></div> |
| 138 | + </div> |
| 139 | + ` |
| 140 | +} |
| 141 | + |
| 142 | +export default { |
| 143 | + name: 'SharedEventBus', |
| 144 | + components: { |
| 145 | + Grid |
| 146 | + }, |
| 147 | + data() { |
| 148 | + return { |
| 149 | + emitter: window.emitter, |
| 150 | + columns: [ |
| 151 | + { |
| 152 | + name: 'Name', |
| 153 | + formatter: cell => { |
| 154 | + return this.$gridjs.helper({ |
| 155 | + components: { TestComponent }, |
| 156 | + template: `<test-component :content="content"></test-component>`, |
| 157 | + data() { |
| 158 | + return { |
| 159 | + content: `🥳 ${cell}` |
| 160 | + } |
| 161 | + } |
| 162 | + }) |
| 163 | + } |
| 164 | + }, |
| 165 | + 'Email' |
| 166 | + ], |
| 167 | + rows: Array(5) |
| 168 | + .fill() |
| 169 | + .map(() => [faker.name.findName(), faker.internet.email()]) |
| 170 | + } |
| 171 | + }, |
| 172 | + mounted() { |
| 173 | + this.emitter.on('sayHello', () => console.log('hello')) |
| 174 | + }, |
| 175 | + template: ` |
| 176 | + <div><grid :columns="columns" :rows="rows"></grid></div> |
| 177 | + ` |
| 178 | +} |
| 179 | +``` |
| 180 | + |
101 | 181 | [< Back to contents](index.md)
|
0 commit comments