fix(pg): enable global server-side search across all pages with debounce and per-page selector

This commit is contained in:
Ray1111-aa
2026-10-27 13:17:47 +07:00
parent 552cee267a
commit 500547116f
2 changed files with 36 additions and 94 deletions
+30 -93
View File
@@ -62,7 +62,7 @@
</div>
<!-- Kotak Pencarian & Filter Regu + Penempatan (Selaras dengan Format Referensi) -->
<form method="GET" action="{{ route('pg.guards.index') }}" class="flex flex-col sm:flex-row items-stretch sm:items-center justify-between gap-2.5 sm:gap-3">
<form method="GET" action="{{ route('pg.guards.index') }}" id="guardFilterForm" class="flex flex-col sm:flex-row items-stretch sm:items-center justify-between gap-2.5 sm:gap-3">
<!-- Pencarian Cepat di Kiri -->
<div class="relative w-full sm:max-w-md">
<span class="absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none text-slate-400">
@@ -76,7 +76,7 @@
class="w-full text-xs pl-10 pr-4 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-800 placeholder-slate-400 focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 shadow-sm transition">
</div>
<!-- Filter di Kanan (Pada Mobile: Sejajar 2 Kolom di Bawah Search Bar) -->
<!-- Filter di Kanan (Pada Mobile: Sejajar di Bawah Search Bar) -->
<div class="flex flex-col sm:flex-row items-stretch sm:items-center justify-end gap-2 sm:gap-2.5 w-full sm:w-auto">
<div class="flex items-center gap-2 sm:gap-2.5 w-full sm:w-auto">
<!-- Filter Regu Satpam -->
@@ -89,11 +89,25 @@
<option value="2" {{ request('team') == '2' ? 'selected' : '' }}>Regu 2</option>
<option value="3" {{ request('team') == '3' ? 'selected' : '' }}>Regu 3</option>
</select>
<!-- Filter Jumlah Tampilan Per Halaman -->
<select name="per_page"
onchange="this.form.submit()"
class="w-full sm:w-auto text-xs rounded-xl border border-slate-200 bg-white px-3 py-2.5 text-slate-700 font-semibold focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 shadow-sm transition truncate">
<option value="20" {{ request('per_page', '20') == '20' ? 'selected' : '' }}>20 / Hal</option>
<option value="50" {{ request('per_page') == '50' ? 'selected' : '' }}>50 / Hal</option>
<option value="100" {{ request('per_page') == '100' ? 'selected' : '' }}>Semua (100)</option>
</select>
<!-- Tombol Submit Pencarian -->
<button type="submit" class="px-4 py-2.5 bg-slate-900 hover:bg-slate-800 text-white text-xs font-bold rounded-xl transition shadow-sm cursor-pointer whitespace-nowrap">
Cari
</button>
</div>
<!-- Tombol Reset Filter (Sesuai Referensi Merah / Rose Clean Text) -->
<div id="guardResetContainer">
@if(request()->anyFilled(['search', 'team', 'gate_id']))
@if(request()->anyFilled(['search', 'team', 'gate_id', 'per_page']))
<a href="{{ route('pg.guards.index') }}"
id="guardResetBtn"
class="text-xs text-rose-600 hover:text-rose-700 font-semibold flex items-center justify-center sm:justify-start space-x-1.5 transition whitespace-nowrap py-1 sm:py-0 sm:ml-2">
@@ -618,100 +632,23 @@
document.getElementById('editGuardModal').classList.add('hidden');
}
// Filter Pencarian Instan Real-time & Pesan Tidak Ditemukan
// Pencarian Server-side Otomatis (Global ke seluruh database, bukan hanya halaman 1)
const searchInput = document.getElementById('guardSearchInput');
const resetContainer = document.getElementById('guardResetContainer');
const tableBody = document.getElementById('guardTableBody');
const hasServerFilters = {{ request()->anyFilled(['search', 'team', 'gate_id']) ? 'true' : 'false' }};
const guardFilterForm = document.getElementById('guardFilterForm');
let searchDebounce = null;
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
// Fokuskan kursor di ujung teks input jika ada kata kunci pencarian aktif
if (searchInput && searchInput.value) {
searchInput.focus();
const len = searchInput.value.length;
searchInput.setSelectionRange(len, len);
}
searchInput?.addEventListener('input', function(e) {
const rawValue = e.target.value;
const query = rawValue.toLowerCase().trim();
const rows = document.querySelectorAll('#guardTableBody tr.guard-row');
let visibleCount = 0;
rows.forEach(row => {
const text = row.textContent.toLowerCase();
const match = text.includes(query);
row.style.display = match ? '' : 'none';
if (match) visibleCount++;
});
const serverEmptyRow = document.getElementById('serverGuardEmptyRow');
if (serverEmptyRow) {
serverEmptyRow.style.display = query.length > 0 ? 'none' : '';
}
// Hapus baris pesan kosong pencarian jika ada sebelumnya
const existingEmptyRow = document.getElementById('guardEmptySearchRow');
if (existingEmptyRow) {
existingEmptyRow.remove();
}
// Tampilkan pesan jika tidak ada data yang cocok di tabel
if (visibleCount === 0 && query.length > 0) {
const emptyTr = document.createElement('tr');
emptyTr.id = 'guardEmptySearchRow';
emptyTr.innerHTML = `
<td colspan="6" class="py-12 text-center text-slate-400">
<i class="fa-solid fa-magnifying-glass text-3xl mb-3 block text-slate-300"></i>
<p class="text-sm font-semibold text-slate-700">
Data "<span class="text-rose-600 font-bold">${escapeHtml(rawValue)}</span>" tidak ditemukan di tabel
</p>
<p class="text-xs text-slate-400 mt-1">
Pastikan ejaan ID Satpam atau nama petugas yang dicari sudah benar.
</p>
<div class="mt-4">
<button type="button"
onclick="const inp = document.getElementById('guardSearchInput'); if(inp){ inp.value=''; inp.dispatchEvent(new Event('input')); inp.focus(); }"
class="inline-flex items-center space-x-1.5 px-3.5 py-2 rounded-xl text-xs font-semibold text-rose-600 bg-rose-50 border border-rose-200 hover:bg-rose-100 transition cursor-pointer">
<i class="fa-solid fa-rotate-left text-[11px]"></i>
<span>Hapus Pencarian</span>
</button>
</div>
</td>
`;
tableBody?.appendChild(emptyTr);
}
// Tampilkan/sembunyikan tombol Reset Filter secara real-time saat mengetik
let resetBtn = document.getElementById('guardResetBtn');
if (query.length > 0) {
if (!resetBtn && resetContainer) {
resetBtn = document.createElement('a');
resetBtn.id = 'guardResetBtn';
resetBtn.href = "{{ route('pg.guards.index') }}";
resetBtn.className = "text-xs text-rose-600 hover:text-rose-700 font-semibold flex items-center justify-center sm:justify-start space-x-1.5 transition whitespace-nowrap py-1 sm:py-0 sm:ml-2";
const resetIcon = document.createElement('i');
resetIcon.className = "fa-solid fa-rotate-left text-[11px]";
const resetText = document.createElement('span');
resetText.textContent = "Reset Filter";
resetBtn.appendChild(resetIcon);
resetBtn.appendChild(resetText);
resetContainer.appendChild(resetBtn);
}
} else if (!hasServerFilters && resetBtn) {
resetBtn.remove();
}
});
// Reset filter click handler saat hanya input client-side tanpa server filter
resetContainer?.addEventListener('click', function(e) {
const btn = e.target.closest('#guardResetBtn');
if (btn && !hasServerFilters) {
e.preventDefault();
if (searchInput) {
searchInput.value = '';
searchInput.dispatchEvent(new Event('input'));
searchInput.focus();
}
}
searchInput?.addEventListener('input', function() {
clearTimeout(searchDebounce);
searchDebounce = setTimeout(function() {
guardFilterForm?.submit();
}, 500);
});
// Buka & Tutup Modal Panduan Halaman
function openGuideModal() {