fix(pg): enable global server-side search across all pages with debounce and per-page selector
This commit is contained in:
@@ -54,10 +54,15 @@ class SecurityGuardController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index(Request $request): View
|
public function index(Request $request): View
|
||||||
{
|
{
|
||||||
|
$perPage = (int) $request->input('per_page', 20);
|
||||||
|
if (!in_array($perPage, [20, 50, 100])) {
|
||||||
|
$perPage = 20;
|
||||||
|
}
|
||||||
|
|
||||||
$guards = $this->getGuardsQuery($request)
|
$guards = $this->getGuardsQuery($request)
|
||||||
->orderBy('team')
|
->orderBy('team')
|
||||||
->orderBy('name')
|
->orderBy('name')
|
||||||
->paginate(20)
|
->paginate($perPage)
|
||||||
->withQueryString();
|
->withQueryString();
|
||||||
|
|
||||||
$gates = Gate::where('is_active', true)->orderBy('name')->get();
|
$gates = Gate::where('is_active', true)->orderBy('name')->get();
|
||||||
|
|||||||
@@ -62,7 +62,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Kotak Pencarian & Filter Regu + Penempatan (Selaras dengan Format Referensi) -->
|
<!-- 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 -->
|
<!-- Pencarian Cepat di Kiri -->
|
||||||
<div class="relative w-full sm:max-w-md">
|
<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">
|
<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">
|
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>
|
</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 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">
|
<div class="flex items-center gap-2 sm:gap-2.5 w-full sm:w-auto">
|
||||||
<!-- Filter Regu Satpam -->
|
<!-- Filter Regu Satpam -->
|
||||||
@@ -89,11 +89,25 @@
|
|||||||
<option value="2" {{ request('team') == '2' ? 'selected' : '' }}>Regu 2</option>
|
<option value="2" {{ request('team') == '2' ? 'selected' : '' }}>Regu 2</option>
|
||||||
<option value="3" {{ request('team') == '3' ? 'selected' : '' }}>Regu 3</option>
|
<option value="3" {{ request('team') == '3' ? 'selected' : '' }}>Regu 3</option>
|
||||||
</select>
|
</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>
|
</div>
|
||||||
|
|
||||||
<!-- Tombol Reset Filter (Sesuai Referensi Merah / Rose Clean Text) -->
|
<!-- Tombol Reset Filter (Sesuai Referensi Merah / Rose Clean Text) -->
|
||||||
<div id="guardResetContainer">
|
<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') }}"
|
<a href="{{ route('pg.guards.index') }}"
|
||||||
id="guardResetBtn"
|
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">
|
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');
|
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 searchInput = document.getElementById('guardSearchInput');
|
||||||
const resetContainer = document.getElementById('guardResetContainer');
|
const guardFilterForm = document.getElementById('guardFilterForm');
|
||||||
const tableBody = document.getElementById('guardTableBody');
|
let searchDebounce = null;
|
||||||
const hasServerFilters = {{ request()->anyFilled(['search', 'team', 'gate_id']) ? 'true' : 'false' }};
|
|
||||||
|
|
||||||
function escapeHtml(str) {
|
// Fokuskan kursor di ujung teks input jika ada kata kunci pencarian aktif
|
||||||
const div = document.createElement('div');
|
if (searchInput && searchInput.value) {
|
||||||
div.textContent = str;
|
searchInput.focus();
|
||||||
return div.innerHTML;
|
const len = searchInput.value.length;
|
||||||
|
searchInput.setSelectionRange(len, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
searchInput?.addEventListener('input', function(e) {
|
searchInput?.addEventListener('input', function() {
|
||||||
const rawValue = e.target.value;
|
clearTimeout(searchDebounce);
|
||||||
const query = rawValue.toLowerCase().trim();
|
searchDebounce = setTimeout(function() {
|
||||||
const rows = document.querySelectorAll('#guardTableBody tr.guard-row');
|
guardFilterForm?.submit();
|
||||||
let visibleCount = 0;
|
}, 500);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// Buka & Tutup Modal Panduan Halaman
|
// Buka & Tutup Modal Panduan Halaman
|
||||||
function openGuideModal() {
|
function openGuideModal() {
|
||||||
|
|||||||
Reference in New Issue
Block a user