質問編集履歴

1

neko_daisuki様のご回答を参考に修正したコードです。

2020/11/23 09:38

投稿

alpaca540
alpaca540

スコア18

test CHANGED
File without changes
test CHANGED
@@ -193,3 +193,257 @@
193
193
 
194
194
 
195
195
  v-bind:key="item.id"でボタンも個別に変更できるかと思ったのですが、他のボタンも変わってしまいます
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+ ### (11/23修正)neko_daisuki様のご回答を参考に修正したコードです。
204
+
205
+ ```html
206
+
207
+ <!DOCTYPE html>
208
+
209
+ <head>
210
+
211
+ <meta charset="UTF-8">
212
+
213
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
214
+
215
+ <script src="https://www.promisejs.org/polyfills/promise-7.0.4.min.js"></script>
216
+
217
+ <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
218
+
219
+ <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
220
+
221
+
222
+
223
+ <link rel="stylesheet" media="all" type="text/css" href="css/destyle.css" />
224
+
225
+ <link rel="stylesheet" media="all" type="text/css" href="css/flex.css">
226
+
227
+ <link rel="stylesheet" media="all" type="text/css" href="css/search.css">
228
+
229
+
230
+
231
+
232
+
233
+ <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">
234
+
235
+
236
+
237
+
238
+
239
+ <title>SEARCH</title>
240
+
241
+ <style>
242
+
243
+ .active{
244
+
245
+ color: red;
246
+
247
+ }
248
+
249
+ </style>
250
+
251
+
252
+
253
+ </head>
254
+
255
+
256
+
257
+
258
+
259
+ <body>
260
+
261
+ <div id="container">
262
+
263
+ <br>
264
+
265
+ <div id="app">
266
+
267
+ <item-list></item-list>
268
+
269
+ </div><!-- app -->
270
+
271
+
272
+
273
+ </div><!-- container -->
274
+
275
+
276
+
277
+
278
+
279
+ <script src="js/flowers.js"></script>
280
+
281
+ <script src="js/main.js"></script>
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+ </body>
290
+
291
+ </html>
292
+
293
+ ```
294
+
295
+
296
+
297
+ ```js
298
+
299
+ var app = new Vue ({
300
+
301
+ el: "#app",
302
+
303
+ });
304
+
305
+ ```
306
+
307
+
308
+
309
+ ```js
310
+
311
+ Vue.component ('item-list', {
312
+
313
+ template: `
314
+
315
+ <div>
316
+
317
+
318
+
319
+ <section class="flex">
320
+
321
+ <ul class="flex__list">
322
+
323
+ <li class="flex__item246" v-for="item in items" v-bind:key="item.id" >
324
+
325
+ <img v-bind:src="item.img">
326
+
327
+ <p class="white">{{ item.anotherword }}</p>
328
+
329
+ <p>{{ item.name }}</p>
330
+
331
+
332
+
333
+ <button v-on:click="addfavorites(item.id)" v-bind:class ="{active: isActive(item.id)}"><i class="far fa-heart"></i></button>
334
+
335
+
336
+
337
+ </li>
338
+
339
+ </ul>
340
+
341
+
342
+
343
+ </section>
344
+
345
+
346
+
347
+ <section class="flex">
348
+
349
+ <p>My favorites</p>
350
+
351
+ <ul class="flex__list">
352
+
353
+ <li class="flex__item246" v-for="(item, index) in favorites" v-bind:key="item.id" >
354
+
355
+ <img v-bind:src="item.img">
356
+
357
+ <p>{{ item.name }}</p>
358
+
359
+
360
+
361
+ <button @click="delfavorites(index)" v-bind:class ="{active: isActive(item.id)}"><i class="far fa-trash-alt"></i></button>
362
+
363
+ </li>
364
+
365
+ </ul>
366
+
367
+
368
+
369
+ </section>
370
+
371
+ </div>
372
+
373
+ `,
374
+
375
+ data: function(){
376
+
377
+ return {
378
+
379
+ "items": [
380
+
381
+ { "id": 0, "name": "アイテムA", "img": "image/items/a.jpg" },
382
+
383
+ { "id": 1, "name": "アイテムB", "img": "image/items/b.jpg" },
384
+
385
+ ],
386
+
387
+ favorites: [],
388
+
389
+
390
+
391
+ }
392
+
393
+ },
394
+
395
+ methods:{
396
+
397
+ addfavorites: function (id) {
398
+
399
+ var selecteditem = this.items.find(function(item) {
400
+
401
+ return (item.id === id);
402
+
403
+ });
404
+
405
+ let existence = false;
406
+
407
+
408
+
409
+ this.favorites.forEach(item => {
410
+
411
+ if(item.id === selecteditem.id){
412
+
413
+ existence = true;
414
+
415
+ alert("登録済みです")
416
+
417
+ }
418
+
419
+ })
420
+
421
+ if( existence === false){
422
+
423
+ this.favorites.push(selecteditem)
424
+
425
+ }
426
+
427
+ },
428
+
429
+ delfavorites: function (id) {
430
+
431
+ this.favorites.splice(id, 1)
432
+
433
+ } ,
434
+
435
+ isActive: function (id) {
436
+
437
+ return !!this.favorites.find(function (favo) {
438
+
439
+ return favo === id
440
+
441
+ })
442
+
443
+ }
444
+
445
+ }
446
+
447
+ });
448
+
449
+ ```