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

回答編集履歴

1

テスト用のものを掲載しました。

2021/12/19 05:19

投稿

babu_babu_baboo
babu_babu_baboo

スコア616

answer CHANGED
@@ -48,4 +48,274 @@
48
48
  }
49
49
  }
50
50
 
51
+ ```
52
+ >出来ませんでした
53
+
54
+ 危うく消すところでした。
55
+ テストに使ったコードです。
56
+
57
+ 関数 add が機能を兼用しているのがダメですよ
58
+
59
+ ```html&js
60
+ <!DOCTYPE html>
61
+ <html lang="ja">
62
+ <head>
63
+ <meta charset="UTF-8" />
64
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
65
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
+
67
+ <!-- CSS -->
68
+ <link
69
+ href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css"
70
+ rel="stylesheet"
71
+ integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1"
72
+ crossorigin="anonymous"
73
+ />
74
+
75
+ <title>条件分岐の練習</title>
76
+ </head>
77
+ <body class="bg-light">
78
+ <div class="container w-100">
79
+ <h1 class="text-center text-info my-4">納期管理アプリ</h1>
80
+
81
+ <form id="all_radio">
82
+ <div class="responsibility">
83
+ <div class="form-check">
84
+ <input
85
+ class="form-check-input"
86
+ type="radio"
87
+ name="deadline"
88
+ id="t_t"
89
+ value="0"
90
+ checked
91
+ />
92
+ <label class="form-check-label" for="flexRadioDefault2">
93
+ 通常業務、通常配送
94
+ </label>
95
+ </div>
96
+ <div class="form-check">
97
+ <input
98
+ class="form-check-input"
99
+ type="radio"
100
+ name="deadline"
101
+ id="t_k"
102
+ value="1"
103
+ />
104
+ <label class="form-check-label" for="flexRadioDefault1">
105
+ 通常業務、緊急配送
106
+ </label>
107
+ </div>
108
+ </div>
109
+
110
+ <div class="deadline mt-3">
111
+ <div class="form-check">
112
+ <input
113
+ class="form-check-input"
114
+ type="radio"
115
+ name="deadline"
116
+ id="j_t"
117
+ value="2"
118
+ />
119
+ <label class="form-check-label" for="flexRadioDefault2">
120
+ 重要業務、通常配送
121
+ </label>
122
+ </div>
123
+ <div class="form-check">
124
+ <input
125
+ class="form-check-input"
126
+ type="radio"
127
+ name="deadline"
128
+ id="j_k"
129
+ value="3"
130
+ />
131
+ <label class="form-check-label" for="flexRadioDefault1">
132
+ 重要業務、緊急配送
133
+ </label>
134
+ </div>
135
+ </div>
136
+ </form>
137
+
138
+ <form id="form" class="mt-2 mb-5">
139
+ <input
140
+ id="input"
141
+ class="form-control"
142
+ placeholder="納期、送り先を入力"
143
+ autocomplete="off"
144
+ />
145
+ </form>
146
+
147
+ <div id="all_ul" class="f-container">
148
+ <ul class="list-group text-secondary" id="ul_3">
149
+ 重要、緊急
150
+ </ul>
151
+ <ul class="list-group text-secondary" id="ul_2">
152
+ 重要、通常
153
+ </ul>
154
+ <ul class="list-group text-secondary" id="ul_1">
155
+ 通常、緊急
156
+ </ul>
157
+ <ul class="list-group text-secondary" id="ul_0">
158
+ 通常、通常
159
+ </ul>
160
+ </div>
161
+ </div>
162
+
163
+ <!-- Resetボタン -->
164
+ <div class="d-grid gap-2 col-1 mx-auto pt-5">
165
+ <button id="Reset" class="btn btn-primary" type="button">Reset</button>
166
+ </div>
167
+
168
+ <script>
169
+ const form = document.getElementById("form");
170
+ const input = document.getElementById("input");
171
+ const ul_0 = document.getElementById("ul_0");
172
+ const ul_1 = document.getElementById("ul_1");
173
+ const ul_2 = document.getElementById("ul_2");
174
+ const ul_3 = document.getElementById("ul_3");
175
+ const all_radio = document.getElementById("all_radio");
176
+
177
+ loadData ();
178
+ // ローカルデータ読み込み
179
+ /*
180
+ let todos = JSON.parse(localStorage.getItem("todos"));
181
+ if (todos) {
182
+ // 配列の全要素に対して反復処理
183
+ todos.forEach((todo) => {
184
+ add(todo);
185
+ });
186
+ }
187
+ */
188
+ // フォームがエンター(submit)された時処理
189
+ form.addEventListener("submit", function (event) {
190
+ // デフォルトのイベント中止(エンター後のリロード)
191
+ event.preventDefault();
192
+ // 下記の関数submit後に行いたい処理
193
+ add();
194
+ });
195
+
196
+ function add(todo) {
197
+ // フォームに入力された値を取る
198
+ let todoText = input.value;
199
+
200
+ if (todo) {
201
+ todoText = todo.text;
202
+ }
203
+ // フォームが空の状態でのliタグの追加を防ぐ
204
+ if (todoText.length > 0) {
205
+ // liタグの作成
206
+ const li = document.createElement("li");
207
+ // liにフォームに入力された値を入れる
208
+ li.innerText = todoText;
209
+ // liタグにlist-group-itemクラスの追加、デザインの為
210
+ li.classList.add("list-group-item");
211
+
212
+ // ラジオボタンの選択された値
213
+ let radioNodeList = all_radio.deadline;
214
+ let radiobtn = radioNodeList.value;
215
+
216
+ switch (radiobtn) {
217
+ case "0":
218
+ // ul_0の子要素として追加する
219
+ ul_0.appendChild(li);
220
+ // フォームを空にする
221
+ input.value = "";
222
+ // ローカルストレージに保存する
223
+ saveData();
224
+ break;
225
+ case "1":
226
+ ul_1.appendChild(li);
227
+ input.value = "";
228
+ saveData();
229
+ break;
230
+ case "2":
231
+ ul_2.appendChild(li);
232
+ input.value = "";
233
+ saveData();
234
+ break;
235
+ case "3":
236
+ ul_3.appendChild(li);
237
+ input.value = "";
238
+ saveData();
239
+ }
240
+
241
+ if (todo && todo.completed) {
242
+ li.classList.add("text-decoration-line-through");
243
+ }
244
+
245
+ li.addEventListener("contextmenu", function (event) {
246
+ // 右クリックイベントブロック
247
+ event.preventDefault();
248
+ li.remove();
249
+ saveDate();
250
+ });
251
+
252
+ li.addEventListener("click", function () {
253
+ li.classList.toggle("text-decoration-line-through");
254
+ saveDate();
255
+ });
256
+ }
257
+ }
258
+ // li要素を取得して、配列としてまとめておく
259
+ function saveData() {
260
+ const lists = document.querySelectorAll("li");
261
+ // 空の配列を定義
262
+ const todos = { };
263
+ // 配列の全要素に対して反復処理
264
+ lists.forEach((li) => {
265
+ // todosの配列に追加していく処理
266
+ let parent = li.parentNode, id = parent.id;
267
+ if (id) {
268
+ if (! todos[id])
269
+ todos[id] = [ ];
270
+
271
+ todos[id].push ({
272
+ // 入力された値
273
+ text: li.innerText,
274
+ completed: li.classList.contains("text-decoration-line-through"),
275
+ });
276
+ }
277
+ });
278
+ // jsonでローカルデータ保存
279
+ localStorage.setItem("todos", JSON.stringify(todos));
280
+ }
281
+
282
+ function loadData () {
283
+ let todos = JSON.parse(localStorage.getItem("todos"));
284
+ if (! todos)
285
+ return;
286
+
287
+ let LI = document.createElement ('li');
288
+ LI.classList.add ("list-group-item");
289
+
290
+ for (let [id, group] of Object.entries (todos)) {
291
+ let ul = document.getElementById (id);
292
+ if (ul){
293
+ for (let {text, completed} of group) {
294
+ let li = LI.cloneNode (false);
295
+ li.textContent = text;
296
+ if (completed)
297
+ li.classList.add("text-decoration-line-through");
298
+ ul.appendChild (li);
299
+ }
300
+ }
301
+ }
302
+ }
303
+
304
+ // リセットボタン
305
+ const Reset = document.getElementById("Reset");
306
+
307
+ Reset.addEventListener("click", function () {
308
+ // 子要素全消去
309
+ all_ul.innerHTML = "";
310
+ window.location.reload();
311
+ saveDate();
312
+ });
313
+
314
+ input.addEventListener ('change', function () {
315
+ add();
316
+ }, false);
317
+
318
+ </script>
319
+ </body>
320
+ </html>
51
321
  ```