fix(search): replace client-side card filtering with server-side debounced search in p2h and mobilisasi

This commit is contained in:
Ray1111-aa
2026-10-27 08:49:21 +07:00
parent dd4a460c53
commit 75c830c75d
2 changed files with 46 additions and 181 deletions
+25 -92
View File
@@ -197,105 +197,38 @@
<script> <script>
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
// Search Real-Time Client-Side & Smart Server Query // Search Server-Side dengan Debounce Pintar & Submit Cepat
const searchInput = document.getElementById('lvSearchInput'); const searchInput = document.getElementById('lvSearchInput');
const resetContainer = document.getElementById('lvResetContainer');
const cardsContainer = document.getElementById('lvCardsContainer');
const paginationContainer = document.getElementById('lvPaginationContainer');
const serverEmptyCard = document.getElementById('lvServerEmptyCard');
const filterForm = document.getElementById('lvFilterForm'); const filterForm = document.getElementById('lvFilterForm');
const hasServerFilters = {{ request()->filled('search') ? 'true' : 'false' }}; const hasServerSearch = {{ request()->filled('search') ? 'true' : 'false' }};
let debounceTimer = null;
function escapeHtml(str) { // Otomatis cari ke seluruh database server setelah jeda mengetik 550ms
const div = document.createElement('div'); searchInput?.addEventListener('input', function(e) {
div.textContent = str; clearTimeout(debounceTimer);
return div.innerHTML; const val = e.target.value.trim();
// Jika input dikosongkan setelah sebelumnya ada pencarian, langsung reload reset
if (val === '' && hasServerSearch) {
debounceTimer = setTimeout(() => {
filterForm.submit();
}, 300);
return;
} }
searchInput?.addEventListener('input', function(e) { // Auto submit setelah berhenti mengetik minimal 2 karakter
const rawValue = e.target.value; if (val.length >= 2) {
const query = rawValue.toLowerCase().trim(); debounceTimer = setTimeout(() => {
const cleanQuery = query.replace(/[\s\-_]/g, ''); filterForm.submit();
const items = document.querySelectorAll('#lvCardsContainer .lv-card'); }, 550);
let visibleCount = 0; }
items.forEach(item => {
const text = item.textContent.toLowerCase();
const cleanText = text.replace(/[\s\-_]/g, '');
const match = text.includes(query) || (cleanQuery.length > 0 && cleanText.includes(cleanQuery));
item.style.display = match ? '' : 'none';
if (match) visibleCount++;
}); });
// Sembunyikan pesan SSR kosong saat mengetik // Submit langsung seketika saat tombol Enter ditekan di keyboard
if (serverEmptyCard) { searchInput?.addEventListener('keydown', function(e) {
serverEmptyCard.style.display = query.length > 0 ? 'none' : ''; if (e.key === 'Enter') {
} clearTimeout(debounceTimer);
filterForm.submit();
// Hapus pesan kosong pencarian jika ada sebelumnya
const existingEmptyCard = document.getElementById('lvEmptySearchCard');
if (existingEmptyCard) {
existingEmptyCard.remove();
}
// Tampilkan notifikasi jika tidak ada data yang cocok di halaman ini
if (visibleCount === 0 && query.length > 0) {
const emptyDiv = document.createElement('div');
emptyDiv.id = 'lvEmptySearchCard';
emptyDiv.className = 'col-span-full bg-white rounded-2xl border border-slate-200 p-8 sm:p-12 text-center shadow-sm';
emptyDiv.innerHTML = `
<div class="w-16 h-16 rounded-full bg-rose-50 text-rose-500 flex items-center justify-center mx-auto mb-3">
<i class="fa-solid fa-magnifying-glass text-2xl"></i>
</div>
<h3 class="text-base font-bold text-slate-800">
Unit "<span class="text-rose-600 font-bold">${escapeHtml(rawValue)}</span>" tidak ada di halaman ini
</h3>
<p class="text-xs text-slate-500 mt-1 max-w-md mx-auto">
Unit mungkin berada di halaman lain. Tekan tombol <strong>Cari di Semua Halaman</strong> untuk mencari di seluruh database kendaraan.
</p>
<div class="mt-4 flex flex-wrap items-center justify-center gap-2">
<button type="submit" form="lvFilterForm"
class="inline-flex items-center space-x-1.5 px-4 py-2 rounded-xl text-xs font-bold text-white bg-[#1b5e20] hover:bg-[#144918] active:scale-95 transition shadow-sm cursor-pointer">
<i class="fa-solid fa-magnifying-glass text-[11px]"></i>
<span>Cari di Semua Halaman</span>
</button>
<button type="button"
onclick="const inp = document.getElementById('lvSearchInput'); 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>
`;
cardsContainer?.appendChild(emptyDiv);
}
// Sembunyikan pagination saat client-side search aktif
if (paginationContainer) {
paginationContainer.style.display = query.length > 0 ? 'none' : '';
}
// Tampilkan / sembunyikan tombol Reset Filter secara real-time
let resetBtn = document.getElementById('lvResetBtn');
if (query.length > 0) {
if (!resetBtn && resetContainer) {
resetBtn = document.createElement('a');
resetBtn.id = 'lvResetBtn';
resetBtn.href = "{{ route('mobilisasi.index', ['type' => $type ?? 'all']) }}";
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";
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();
} }
}); });
}); });
+25 -93
View File
@@ -170,106 +170,38 @@
<script> <script>
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
// Search Real-Time Client-Side & Smart Server Query // Search Server-Side dengan Debounce Pintar & Submit Cepat
const searchInput = document.getElementById('p2hSearchInput'); const searchInput = document.getElementById('p2hSearchInput');
const resetContainer = document.getElementById('p2hResetContainer');
const cardsContainer = document.getElementById('p2hCardsContainer');
const paginationContainer = document.getElementById('p2hPaginationContainer');
const serverEmptyCard = document.getElementById('p2hServerEmptyCard');
const filterForm = document.getElementById('p2hFilterForm'); const filterForm = document.getElementById('p2hFilterForm');
const hasServerFilters = {{ request()->anyFilled(['search', 'unit_category']) ? 'true' : 'false' }}; const hasServerSearch = {{ request()->filled('search') ? 'true' : 'false' }};
let debounceTimer = null;
function escapeHtml(str) { // Otomatis cari ke seluruh database server setelah jeda mengetik 550ms
const div = document.createElement('div'); searchInput?.addEventListener('input', function(e) {
div.textContent = str; clearTimeout(debounceTimer);
return div.innerHTML; const val = e.target.value.trim();
// Jika input dikosongkan setelah sebelumnya ada pencarian, langsung reload reset
if (val === '' && hasServerSearch) {
debounceTimer = setTimeout(() => {
filterForm.submit();
}, 300);
return;
} }
searchInput?.addEventListener('input', function(e) { // Auto submit setelah berhenti mengetik minimal 2 karakter
const rawValue = e.target.value; if (val.length >= 2) {
const query = rawValue.toLowerCase().trim(); debounceTimer = setTimeout(() => {
const cleanQuery = query.replace(/[\s\-_]/g, ''); filterForm.submit();
// Pencarian multi-field ke kartu/baris unit }, 550);
const items = document.querySelectorAll('#p2hCardsContainer .p2h-card, #p2hCardsContainer tr.p2h-row'); }
let visibleCount = 0;
items.forEach(item => {
const text = item.textContent.toLowerCase();
const cleanText = text.replace(/[\s\-_]/g, '');
const match = text.includes(query) || (cleanQuery.length > 0 && cleanText.includes(cleanQuery));
item.style.display = match ? '' : 'none';
if (match) visibleCount++;
}); });
// Sembunyikan pesan SSR kosong saat mengetik // Submit langsung seketika saat tombol Enter ditekan di keyboard
if (serverEmptyCard) { searchInput?.addEventListener('keydown', function(e) {
serverEmptyCard.style.display = query.length > 0 ? 'none' : ''; if (e.key === 'Enter') {
} clearTimeout(debounceTimer);
filterForm.submit();
// Hapus pesan kosong pencarian jika ada sebelumnya
const existingEmptyCard = document.getElementById('p2hEmptySearchCard');
if (existingEmptyCard) {
existingEmptyCard.remove();
}
// Tampilkan pesan kosong jika tidak ada data yang cocok di halaman ini
if (visibleCount === 0 && query.length > 0) {
const emptyDiv = document.createElement('div');
emptyDiv.id = 'p2hEmptySearchCard';
emptyDiv.className = 'col-span-full bg-white rounded-2xl border border-slate-200 p-8 sm:p-12 text-center shadow-sm';
emptyDiv.innerHTML = `
<div class="w-16 h-16 rounded-full bg-rose-50 text-rose-500 flex items-center justify-center mx-auto mb-3">
<i class="fa-solid fa-magnifying-glass text-2xl"></i>
</div>
<h3 class="text-base font-bold text-slate-800">
Unit "<span class="text-rose-600 font-bold">${escapeHtml(rawValue)}</span>" tidak ada di halaman ini
</h3>
<p class="text-xs text-slate-500 mt-1 max-w-md mx-auto">
Unit mungkin berada di halaman lain atau kategori lain. Tekan tombol <strong>Cari di Semua Halaman</strong> untuk mencari di seluruh database alat berat.
</p>
<div class="mt-4 flex flex-wrap items-center justify-center gap-2">
<button type="submit" form="p2hFilterForm"
class="inline-flex items-center space-x-1.5 px-4 py-2 rounded-xl text-xs font-bold text-white bg-[#1b5e20] hover:bg-[#144918] active:scale-95 transition shadow-sm cursor-pointer">
<i class="fa-solid fa-magnifying-glass text-[11px]"></i>
<span>Cari di Semua Halaman</span>
</button>
<button type="button"
onclick="const inp = document.getElementById('p2hSearchInput'); 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>
`;
cardsContainer?.appendChild(emptyDiv);
}
// Sembunyikan pagination saat client-side search aktif
if (paginationContainer) {
paginationContainer.style.display = query.length > 0 ? 'none' : '';
}
// Tampilkan/sembunyikan tombol Reset Filter (merah fa-rotate-left) secara real-time
let resetBtn = document.getElementById('p2hResetBtn');
if (query.length > 0) {
if (!resetBtn && resetContainer) {
resetBtn = document.createElement('a');
resetBtn.id = 'p2hResetBtn';
resetBtn.href = "{{ route('p2h.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();
} }
}); });
}); });