llhd/wwwroot/webnew/pages/header.js

70 lines
1.6 KiB
JavaScript

let allData = [];
async function loadSearchData() {
const regulasi = await fetch('/WebNew/Regulasi/GetRegulasi').then(r => r.json());
const berita = await fetch('/WebNew/Berita/GetAll').then(r => r.json());
allData = [
...regulasi.map(x => ({ ...x, type: 'regulasi' })),
...berita.map(x => ({ ...x, type: 'berita' })),
];
}
function searchData(keyword) {
keyword = keyword.toLowerCase();
const result = allData.filter(item =>
item.judul.toLowerCase().includes(keyword)
);
renderResult(result);
}
function renderResult(data) {
let html = "";
if (data.length === 0) {
html = "<p class='text-muted'>Tidak ditemukan</p>";
}
data.slice(0, 5).forEach(item => {
let link = "#";
if (item.type === "berita") {
link = `/WebNew/Berita/Detail?id=${item.id}`;
}
if (item.type === "regulasi") {
link = item.file;
}
html += `
<a href="${link}" class="search-item d-block mb-2 rounded p-2">
<strong>${item.judul}</strong><br>
<small class="text-muted">${item.type}</small>
</a>
`;
});
document.getElementById("searchResult").innerHTML = html;
}
document.addEventListener("DOMContentLoaded", () => {
loadSearchData();
document.getElementById("searchInput").addEventListener("keyup", function () {
const keyword = this.value;
if (keyword.length < 2) {
document.getElementById("searchResult").innerHTML = "";
return;
}
searchData(keyword);
});
});