回答編集履歴
1
:keyについての補足追加
test
CHANGED
@@ -185,3 +185,199 @@
|
|
185
185
|
というエラーがでています。
|
186
186
|
|
187
187
|
サンプルコードだとは思いますが、一応:keyには一意になる値が必ずくるように実装するのをお忘れなく。
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
追記
|
192
|
+
|
193
|
+
---
|
194
|
+
|
195
|
+
Vue.jsについては詳しくないので、気になって:keyについて調べてみました。
|
196
|
+
|
197
|
+
[https://qrunch.net/@rokujiro/entries/wkreYi4Opa4Wa3iU](https://qrunch.net/@rokujiro/entries/wkreYi4Opa4Wa3iU)
|
198
|
+
|
199
|
+
上記の説明によるとなるべく配列の中に一意の値をもつプロパティを持たせたほうがいいようですね。
|
200
|
+
|
201
|
+
そうなると、clickイベントでindexを渡すよりも、要素のkeyになるプロパティを渡して、
|
202
|
+
|
203
|
+
それに一致すつ配列の要素を探してshowを切り替えたほうがいいみたいですね。
|
204
|
+
|
205
|
+
```nuxt
|
206
|
+
|
207
|
+
<template>
|
208
|
+
|
209
|
+
<div>
|
210
|
+
|
211
|
+
<b-table-simple class="card">
|
212
|
+
|
213
|
+
<b-thead>
|
214
|
+
|
215
|
+
<b-tr>
|
216
|
+
|
217
|
+
<b-th v-for="field in fields" :key="field.key">{{ field.label }}</b-th>
|
218
|
+
|
219
|
+
</b-tr>
|
220
|
+
|
221
|
+
</b-thead>
|
222
|
+
|
223
|
+
<b-tbody v-for="item in items" :key="item.id">
|
224
|
+
|
225
|
+
<b-tr @click="click(item.id)">
|
226
|
+
|
227
|
+
<b-td>{{ item.sample1 }}</b-td>
|
228
|
+
|
229
|
+
<b-td>{{ item.sample2 }}</b-td>
|
230
|
+
|
231
|
+
<b-td>{{ item.sample3 }}</b-td>
|
232
|
+
|
233
|
+
</b-tr>
|
234
|
+
|
235
|
+
<b-tr v-show="item.show" v-for="subItem in item.subItems" :key="subItem.id">
|
236
|
+
|
237
|
+
<b-td>{{ subItem.sample1 }}</b-td>
|
238
|
+
|
239
|
+
<b-td>{{ subItem.sample2 }}</b-td>
|
240
|
+
|
241
|
+
<b-td>{{ subItem.sample3 }}</b-td>
|
242
|
+
|
243
|
+
</b-tr>
|
244
|
+
|
245
|
+
</b-tbody>
|
246
|
+
|
247
|
+
</b-table-simple>
|
248
|
+
|
249
|
+
</div>
|
250
|
+
|
251
|
+
</template>
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
<script>
|
256
|
+
|
257
|
+
export default {
|
258
|
+
|
259
|
+
data () {
|
260
|
+
|
261
|
+
return {
|
262
|
+
|
263
|
+
fields: [
|
264
|
+
|
265
|
+
{key: 'sample1', label: 'サンプルタイトル1'},
|
266
|
+
|
267
|
+
{key: 'sample2', label: 'サンプルタイトル2'},
|
268
|
+
|
269
|
+
{key: 'sample3', label: 'サンプルタイトル3'},
|
270
|
+
|
271
|
+
],
|
272
|
+
|
273
|
+
items: [
|
274
|
+
|
275
|
+
{
|
276
|
+
|
277
|
+
id: 1123,
|
278
|
+
|
279
|
+
show : false,
|
280
|
+
|
281
|
+
sample1: 'サンプル1',
|
282
|
+
|
283
|
+
sample2: 'サンプル2',
|
284
|
+
|
285
|
+
sample3: 'サンプル3',
|
286
|
+
|
287
|
+
subItems: [
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
id: 1,
|
292
|
+
|
293
|
+
sample1: 'サンプル1',
|
294
|
+
|
295
|
+
sample2: 'サンプル2',
|
296
|
+
|
297
|
+
sample3: 'サンプル3',
|
298
|
+
|
299
|
+
},
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
id: 2,
|
304
|
+
|
305
|
+
sample1: 'サンプル1',
|
306
|
+
|
307
|
+
sample2: 'サンプル2',
|
308
|
+
|
309
|
+
sample3: 'サンプル3',
|
310
|
+
|
311
|
+
},
|
312
|
+
|
313
|
+
]
|
314
|
+
|
315
|
+
},
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
id: 1124,
|
320
|
+
|
321
|
+
show : false,
|
322
|
+
|
323
|
+
sample1: 'サンプル1',
|
324
|
+
|
325
|
+
sample2: 'サンプル2',
|
326
|
+
|
327
|
+
sample3: 'サンプル3',
|
328
|
+
|
329
|
+
subItems: [
|
330
|
+
|
331
|
+
{
|
332
|
+
|
333
|
+
id: 1,
|
334
|
+
|
335
|
+
sample1: 'サンプル1',
|
336
|
+
|
337
|
+
sample2: 'サンプル2',
|
338
|
+
|
339
|
+
sample3: 'サンプル3',
|
340
|
+
|
341
|
+
},
|
342
|
+
|
343
|
+
{
|
344
|
+
|
345
|
+
id: 2,
|
346
|
+
|
347
|
+
sample1: 'サンプル1',
|
348
|
+
|
349
|
+
sample2: 'サンプル2',
|
350
|
+
|
351
|
+
sample3: 'サンプル3',
|
352
|
+
|
353
|
+
},
|
354
|
+
|
355
|
+
]
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
],
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
},
|
364
|
+
|
365
|
+
methods: {
|
366
|
+
|
367
|
+
click(id) {
|
368
|
+
|
369
|
+
this.items.forEach(item => {
|
370
|
+
|
371
|
+
if (item.id == id) item.show = !item.show
|
372
|
+
|
373
|
+
})
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
</script>
|
382
|
+
|
383
|
+
```
|