質問編集履歴

1

コードを追加

2022/10/29 08:32

投稿

kumamuu
kumamuu

スコア0

test CHANGED
File without changes
test CHANGED
@@ -21,52 +21,146 @@
21
21
 
22
22
  ### 発生している問題・エラーメッセージ
23
23
 
24
+ 試したことに記載しておりますが、
25
+ そのコードですと下記のエラーが発生してしまいます。
24
26
  ```
27
+ JavaScript.js:63 Uncaught TypeError: Cannot read properties of undefined (reading 'rowIndex')
28
+ at createRemoveButton (JavaScript.js:63:24)
25
- エラーメッセージ
29
+ at JavaScript.js:54:26
30
+ at Array.forEach (<anonymous>)
31
+ at showTaskList (JavaScript.js:36:9)
32
+ at addData (JavaScript.js:19:3)
33
+ at HTMLButtonElement.<anonymous> (JavaScript.js:81:3)
26
34
  ```
27
35
 
28
36
  ### 該当のソースコード
37
+ ```html
29
38
 
39
+ <!DOCTYPE html>
40
+ <html lang="ja">
41
+ <head>
42
+ <meta charset="UTF-8">
43
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
44
+ <title>Todoリスト</title>
45
+ </head>
46
+ <body>
47
+ <h1>Todoリスト</h1>
48
+ <p>
49
+ <input type="radio" name="status" value="1" checked="checked">全て
50
+ <input type="radio" name="status" value="2">作業中
51
+ <input type="radio" name="status" value="3">完了
52
+ </p>
53
+
54
+ <table>
55
+ <thead>
56
+ <th>ID</th>
57
+ <th>コメント</th>
58
+ <th>状態</th>
59
+ <th></th>
60
+ </thead>
61
+ <tbody id="todo_lists"></tbody>
62
+ </table>
63
+
64
+ <h2>新規タスクの追加</h2>
65
+ <input id="add_task" type="text"/>
66
+ <button id="add_button" type="button">追加</button>
67
+ <script src="JavaScript.js"></script>
68
+ </body>
69
+ </html>
70
+ ```
71
+
30
- ```ここに言語名を入力
72
+ ```Javascript
73
+
74
+ 'use strict'
75
+ // 必要なDOM要素を取得。
76
+ const addButton = document.getElementById('add_button') //htmlから追加ボタンの値を取得
77
+ const addTask = document.getElementById('add_task') //htmlから入力したタスクの値の取得
78
+ const todoLists = document.getElementById('todo_lists') //htmlからリストの値の取得
79
+
80
+ // デフォルト値で1を設定
81
+ let id = 1
82
+
31
- //削除ボタンの生成
83
+ // todoを保存する箱
84
+ const tasks = []
85
+
86
+ //配列にオブジェクトとしてデータを追加する
87
+ const addData = () => {
88
+ tasks.push({
89
+ id: id,
90
+ title: addTask.value,
91
+ })
92
+ showTaskList(tasks)
93
+ addTask.value = ''
94
+ id++
95
+ }
96
+
97
+ const createStatusButton = () => {
98
+ const status = document.createElement('td')
99
+ const statusButton = document.createElement('button')
100
+ statusButton.innerText = '作業中'
101
+ status.appendChild(statusButton)
102
+ return status;
103
+ }
104
+
32
105
  const createRemoveButton = () => {
33
- const remove = document.createElement('td')
106
+ const remove = document.createElement('td')
34
- const removeButton = document.createElement('button')
107
+ const removeButton = document.createElement('button')
35
- removeButton.innerText = '削除'
108
+ removeButton.innerText = '削除'
36
- remove.appendChild(removeButton)
109
+ remove.appendChild(removeButton)
37
- return remove;
110
+ return remove;
38
111
  }
39
112
 
40
113
 
41
114
  //タグを追加して出力する関数
42
115
  const showTaskList = () => {
43
- todoLists.innerHTML = '';
116
+ todoLists.innerHTML = '';
44
- tasks.forEach((task, index) => {
117
+ tasks.forEach((task, index) => {
45
- const todoItem = document.createElement('tr')
118
+ const todoItem = document.createElement('tr')
46
- const todoId = document.createElement('td')
119
+ const todoId = document.createElement('td')
47
- const todoTitle = document.createElement('td')
120
+ const todoTitle = document.createElement('td')
121
+
122
+ todoTitle.innerHTML = task.title
123
+ todoId.innerHTML = index+1
124
+
125
+ todoLists.appendChild(todoItem)
126
+ todoItem.appendChild(todoId)
127
+ todoItem.appendChild(todoTitle)
128
+
129
+ const statusButton = createStatusButton()
130
+ todoItem.appendChild(statusButton)
131
+ todoItem.appendChild(statusButton)
132
+
133
+ const removeButton = createRemoveButton()
134
+ todoItem.appendChild(removeButton)
135
+ todoItem.appendChild(removeButton)
136
+ })
137
+ }
48
138
 
49
- todoTitle.innerHTML = task.title
50
- todoId.innerHTML = index+1
139
+ //追加ボタンをクリック
51
-
52
- todoLists.appendChild(todoItem)
140
+ addButton.addEventListener('click', () => {
53
- todoItem.appendChild(todoId)
54
- todoItem.appendChild(todoTitle)
55
-
56
- const statusButton = createStatusButton()
141
+ const task = addTask.value
57
- todoItem.appendChild(statusButton)
142
+ addData(task)
58
- todoItem.appendChild(statusButton)
59
-
60
- const removeButton = createRemoveButton()
61
- todoItem.appendChild(removeButton)
62
- todoItem.appendChild(removeButton)
63
143
  })
64
- }
65
144
  ```
66
145
 
67
146
  ### 試したこと
147
+ 他の方のコードを拝見し、下記のようなコードも試しましたが
148
+ const createRemoveButton = () => { ~~~
149
+ の中に入れるのかもよくわからず・・・
68
150
 
151
+ const createRemoveButton = (todoItem) => {
152
+ var index = todoItem.rowIndex - 1;
153
+ const remove = document.createElement('td')
154
+ const removeButton = document.createElement('button')
155
+ removeButton.innerText = '削除'
156
+ remove.appendChild(removeButton(todoItem))
157
+ removeButton.addEventListener('click', () => {
69
- ここに問題に対して試したことを記載してください。
158
+ tasks.splice(index, 1);
159
+ showTaskList();
160
+ });
161
+ return remove;
162
+ };
163
+
70
164
 
71
165
  ### 補足情報(FW/ツールのバージョンなど)
72
166