質問するログイン新規登録

回答編集履歴

1

追記

2020/07/06 16:46

投稿

sousuke
sousuke

スコア3830

answer CHANGED
@@ -92,4 +92,53 @@
92
92
  }
93
93
  };
94
94
 
95
+ ```
96
+
97
+ ### 追記
98
+ 削除後に振りなおすしかないでしょうね。
99
+ ```javascript
100
+ // 「追加」ボタンがクリックされたときの処理を実装する //
101
+ addButton.addEventListener('click', () => {
102
+ // ここでidをとるとキャンセルしたときにおかしい
103
+ inputBox.focus();
104
+
105
+ // 空文字が入力されたときの処理
106
+ if (inputBox.value === '') {
107
+ alert('タスクを入力してください!');
108
+ return;
109
+ }
110
+ //todoがtrueの場合の処理
111
+ // この書き方はすべてtrueなので意味なし
112
+ //if (todo) {
113
+ // push直前でid取得
114
+ const todo = {
115
+ id: todos.length,
116
+ comment: inputBox.value,
117
+ status: '作業中'
118
+ }
119
+ todos.push(todo);
120
+ inputBox.value = '';
121
+ showTodos(todos);
122
+ filterTodos();
123
+ //}
124
+ });
125
+
126
+ //「削除機能」を管理するボタンを生成する関数
127
+ const createDeleteButton = (tableRecord, id) => {
128
+ let index = tableRecord.rowIndex - 1;
129
+ const deleteButton = document.createElement('button');
130
+ deleteButton.textContent = '削除';
131
+ deleteButton.addEventListener('click', () => {
132
+ const targetIndex = todos.findIndex(todo => {
133
+ return todo.id === id;
134
+ });
135
+ todos.splice(targetIndex, 1);
136
+ // 削除後に削除id以上を振りなおす
137
+ for (var i = targetIndex; i < todos.length; i++) {
138
+ todos[i].id = i
139
+ }
140
+ filterTodos();
141
+ });
142
+ return deleteButton;
143
+ };
95
144
  ```