回答編集履歴

1

追記

2020/07/06 16:46

投稿

sousuke
sousuke

スコア3828

test CHANGED
@@ -187,3 +187,101 @@
187
187
 
188
188
 
189
189
  ```
190
+
191
+
192
+
193
+ ### 追記
194
+
195
+ 削除後に振りなおすしかないでしょうね。
196
+
197
+ ```javascript
198
+
199
+ // 「追加」ボタンがクリックされたときの処理を実装する //
200
+
201
+ addButton.addEventListener('click', () => {
202
+
203
+ // ここでidをとるとキャンセルしたときにおかしい
204
+
205
+ inputBox.focus();
206
+
207
+
208
+
209
+ // 空文字が入力されたときの処理
210
+
211
+ if (inputBox.value === '') {
212
+
213
+ alert('タスクを入力してください!');
214
+
215
+ return;
216
+
217
+ }
218
+
219
+ //todoがtrueの場合の処理
220
+
221
+ // この書き方はすべてtrueなので意味なし
222
+
223
+ //if (todo) {
224
+
225
+ // push直前でid取得
226
+
227
+ const todo = {
228
+
229
+ id: todos.length,
230
+
231
+ comment: inputBox.value,
232
+
233
+ status: '作業中'
234
+
235
+ }
236
+
237
+ todos.push(todo);
238
+
239
+ inputBox.value = '';
240
+
241
+ showTodos(todos);
242
+
243
+ filterTodos();
244
+
245
+ //}
246
+
247
+ });
248
+
249
+
250
+
251
+ //「削除機能」を管理するボタンを生成する関数
252
+
253
+ const createDeleteButton = (tableRecord, id) => {
254
+
255
+ let index = tableRecord.rowIndex - 1;
256
+
257
+ const deleteButton = document.createElement('button');
258
+
259
+ deleteButton.textContent = '削除';
260
+
261
+ deleteButton.addEventListener('click', () => {
262
+
263
+ const targetIndex = todos.findIndex(todo => {
264
+
265
+ return todo.id === id;
266
+
267
+ });
268
+
269
+ todos.splice(targetIndex, 1);
270
+
271
+ // 削除後に削除id以上を振りなおす
272
+
273
+ for (var i = targetIndex; i < todos.length; i++) {
274
+
275
+ todos[i].id = i
276
+
277
+ }
278
+
279
+ filterTodos();
280
+
281
+ });
282
+
283
+ return deleteButton;
284
+
285
+ };
286
+
287
+ ```