-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (71 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
85 lines (71 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
let enemyData = [];
// 1. JSONデータの読み込み
async function loadData() {
try {
const response = await fetch("boss.json");
enemyData = await response.json();
displayEnemies(enemyData); // 初期表示
} catch (error) {
console.error("データの読み込みに失敗しました:", error);
document.getElementById("enemyList").innerText =
"データの読み込みに失敗しました。";
}
}
// 2. データを画面に表示する関数
function displayEnemies(enemies) {
const listContainer = document.getElementById("enemyList");
listContainer.innerHTML = ""; // 一度リセット
if (enemies.length === 0) {
listContainer.innerHTML = "<p>該当するデータが見つかりません。</p>";
return;
}
enemies.forEach((enemy) => {
const card = document.createElement("div");
card.className = "enemy-card";
// 出現場所とアイテムをタグ化
const locationsHtml = enemy.locations
.map((loc) => `<span class="tag location-tag">${loc}</span>`)
.join("");
const itemsHtml = enemy.items
.map((item) => `<span class="tag item-tag">${item}</span>`)
.join("");
card.innerHTML = `
<div class="enemy-name">${enemy.name}</div>
<div><strong>出現場所:</strong> ${locationsHtml}</div>
<div style="margin-top: 5px;"><strong>アイテム:</strong> ${itemsHtml}</div>
`;
listContainer.appendChild(card);
});
}
// 3. 絞り込み検索ロジック
function filterEnemies() {
const nameQuery = document.getElementById("searchName").value.toLowerCase();
const locationQuery = document
.getElementById("searchLocation")
.value.toLowerCase();
const itemQuery = document.getElementById("searchItem").value.toLowerCase();
const filtered = enemyData.filter((enemy) => {
// 名前の一致確認
const matchName = enemy.name.toLowerCase().includes(nameQuery);
// 出現場所の一致確認(配列のどこかに含まれているか)
const matchLocation =
locationQuery === "" ||
enemy.locations.some((loc) =>
loc.toLowerCase().includes(locationQuery)
);
// アイテムの一致確認(配列のどこかに含まれているか)
const matchItem =
itemQuery === "" ||
enemy.items.some((item) => item.toLowerCase().includes(itemQuery));
return matchName && matchLocation && matchItem;
});
displayEnemies(filtered);
}
// イベントリスナーの設定(入力されたら即座にフィルタリング)
document.getElementById("searchName").addEventListener("input", filterEnemies);
document
.getElementById("searchLocation")
.addEventListener("input", filterEnemies);
document.getElementById("searchItem").addEventListener("input", filterEnemies);
// ページ読み込み時にデータロード
window.addEventListener("DOMContentLoaded", loadData);