実現したいこと
Vue.jsでToDoリストを作成しています。
作業中、完了の状態分けの機能を実装したのですがラジオボタンを切り替えると、タスクのidが再度割り振られています。
idは再度割り振られず保持している使用にするにはどのようにすれば良いでしょうか?
該当のソースコード
<template> <div class="hello"> <h1>ToDoリスト</h1> <li class="status" v-for="item in items" :key="item.id"> <input type="radio" name="show" :id="item.id" :value="item.value" v-model="current" > <label :for="item.id">{{item.label}}</label> </li> <table class="todo-list"> <thead> <tr> <td>ID</td> <td>コメント</td> <td>状態</td> <td></td> </tr> </thead> <thead> <tr v-for="(item,index) in computedTodos" :key="item.id"> <td>{{index + 1}}</td> <td>{{item.task}}</td> <td><button @click="change(item)">{{ labels[item.state] }}</button></td> <!-- <td><input type="button" value="作業中" v-model="tasks.done"></td> --> <td><button @click="del(index)">削除</button></td> </tr> </thead> </table> <h1>新規タスクの追加</h1> <div v-on:submit.prevent> <label for="input-todo-box"></label> <input type="text" v-model="value"> <button @click="add">追加</button> </div> </div> </template> <script> export default { name: 'HelloWorld', data: function () { return { items: [ {value: -1, label: 'すべて'}, {value: 0, label: '作業中'}, {value: 1, label: '完了'} ], tasks: [], value: '', current: -1, id: 0 } }, methods: { add() { this.tasks.push({ id: this.tasks.length, task: this.value, state: 0, }); this.value = ''; }, del(index) { this.tasks.splice(index, 1) }, change: function(item) { item.state = !item.state ? 1:0 }, }, computed: { labels() { return this.items.reduce(function (a, b) { return Object.assign(a, { [b.value]: b.label }) }, {}) }, computedTodos: function () { return this.tasks.filter(function (el) { return this.current < 0 ? true : this.current === el.state }, this) }, }, } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .status { display: inline; } </style>
試したこと
全てのタスクを保持したlengthをfilterするとできるのかなと検討していますが具体的な書き方がわかりません。
補足情報
プログラミング初心者です。なにか抜けているところがあればご指摘ください。
あなたの回答
tips
プレビュー