回答編集履歴

1

chousei

2023/07/07 04:04

投稿

yambejp
yambejp

スコア117674

test CHANGED
@@ -1 +1,73 @@
1
1
  データを分離し、データを絞り込んでからテーブルデータを作るのが賢明です
2
+
3
+ # 参考
4
+ 以下「data.json」という名前で保存してhtmlと同じフォルダにいれておく
5
+ ```json
6
+ [
7
+ {"pref":"埼玉", "cate":"飲食店", "address":"埼玉県〇〇市", "link":"http://saitama1.example.com"},
8
+ {"pref":"東京", "cate":"住宅", "address":"東京都〇〇区", "link":"http://tokyo1.example.com"},
9
+ {"pref":"神奈川","cate":"アパレル","address":"神奈川県〇〇市","link":"http://kanagawa1.example.com"},
10
+ {"pref":"東京", "cate":"住宅", "address":"東京都△△区", "link":"http://tokyo2.example.com"},
11
+ {"pref":"東京", "cate":"飲食店", "address":"東京都□□市", "link":"http://tokyo3.example.com"},
12
+ {"pref":"埼玉", "cate":"住宅", "address":"埼玉県〇〇市", "link":"http://saitama2.example.com"}
13
+ ]
14
+ ```
15
+ ■検索
16
+ 一旦フリーワード検索は保留
17
+ ```javascript
18
+ <script>
19
+ window.addEventListener('DOMContentLoaded', async()=>{
20
+ const data=await fetch('data.json').then(res=>res.json());
21
+ btn.addEventListener('click',e=>{
22
+ e.preventDefault();
23
+ const choose=data.filter(x=>{
24
+ const flg1=["",x.pref].includes(Pref.value);
25
+ const ctgr=[...document.querySelectorAll('[name=ctgr]:checked')];
26
+ const flg2=ctgr.length==0 || ctgr.map(x=>x.value).includes(x.cate);
27
+ return flg1 && flg2;
28
+ });
29
+ console.log(choose);
30
+ results.innerHTML="";
31
+ choose.forEach(x=>{
32
+ results.insertAdjacentHTML("beforeend",`<tr><td>${x.pref}の${x.cate}</td><td>${x.address}</td><td><a href="${x.link}">詳細サイト</a></td></tr>`)
33
+ });
34
+ });
35
+ });
36
+ </script>
37
+ <form id="Form">
38
+ <fieldset>
39
+ <legend>都道府県を選択<</legend>
40
+ <select name="pref" id="Pref">
41
+ <option value="">都道府県を選択</option>
42
+ <option value="東京">東京</option>
43
+ <option value="埼玉">埼玉</option>
44
+ <option value="神奈川">神奈川</option>
45
+ </select>
46
+ </fieldset>
47
+ <fieldset>
48
+ <legend>カテゴリーを選択</legend>
49
+ <ul>
50
+ <li><label><input type="checkbox" name="ctgr" value="飲食店">飲食店</label></li>
51
+ <li><label><input type="checkbox" name="ctgr" value="アパレル">アパレル</label></li>
52
+ <li><label><input type="checkbox" name="ctgr" value="住宅">住宅</label></li>
53
+ </ul>
54
+ </fieldset>
55
+ <div>
56
+ <input type="submit" value="検索" id="btn">
57
+ </div>
58
+ </form>
59
+ <hr>
60
+ <div>
61
+ <table>
62
+ <thead>
63
+ <tr>
64
+ <th>カテゴリ</th>
65
+ <th>住所</th>
66
+ <th>詳細</th>
67
+ </tr>
68
+ </thead>
69
+ <tbody id="results">
70
+ </tbody>
71
+ </table>
72
+ </div>
73
+ ```