📂 FileMgr
📍
/home/kientructhaiha/htdocs/kientructhaiha.com/admin/assets/js
✏️ Edit File: /home/kientructhaiha/htdocs/kientructhaiha.com/admin/assets/js/admin.js
⬅ Kembali
/* Admin Dashboard JavaScript */ document.addEventListener('DOMContentLoaded', function() { // Sidebar toggle const sidebarToggle = document.querySelector('.btn-sidebar-toggle'); const sidebar = document.querySelector('.sidebar'); if (sidebarToggle) { sidebarToggle.addEventListener('click', function() { sidebar.classList.toggle('active'); }); } // Close sidebar on mobile when clicking outside document.addEventListener('click', function(event) { if (!event.target.closest('.sidebar') && !event.target.closest('.btn-sidebar-toggle')) { sidebar?.classList.remove('active'); } }); // Initialize DataTables initializeDataTables(); // Toast notifications setupToasts(); }); // Initialize DataTables function initializeDataTables() { // Disable DataTables default alert modal for table errors if (window.DataTable && window.DataTable.ext) { window.DataTable.ext.errMode = 'none'; } if (window.jQuery && window.jQuery.fn && window.jQuery.fn.dataTable) { window.jQuery.fn.dataTable.ext.errMode = 'none'; } const tables = document.querySelectorAll('table.datatable'); tables.forEach(table => { try { const headerCols = table.querySelectorAll('thead tr:first-child th').length; const firstRowCols = table.querySelectorAll('tbody tr:first-child td, tbody tr:first-child th').length; if (headerCols > 0 && firstRowCols > 0 && headerCols !== firstRowCols) { console.warn('Skipping DataTable init due to column mismatch:', headerCols, firstRowCols, table); return; } new DataTable(table, { language: { url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/vi.json' }, responsive: true, pageLength: 10, lengthMenu: [10, 25, 50, 100], dom: '<"row"<"col-md-6"l><"col-md-6"f>><"row"<"col-md-12"t>><"row"<"col-md-6"i><"col-md-6"p>>' }); } catch (error) { console.warn('DataTable init failed:', error); } }); } // Show toast notification function showToast(message, type = 'success', duration = 3000) { const toastContainer = document.querySelector('.toast-container') || createToastContainer(); const toast = document.createElement('div'); toast.className = `toast bg-${type === 'error' ? 'danger' : type}`; toast.innerHTML = ` <div class="toast-body" style="color: white;"> <i class="fas fa-${getIconForType(type)}"></i> ${message} </div> `; toastContainer.appendChild(toast); // Show toast const bsToast = new bootstrap.Toast(toast); bsToast.show(); // Remove element after hidden toast.addEventListener('hidden.bs.toast', function() { toast.remove(); }); } function getIconForType(type) { const icons = { 'success': 'check-circle', 'error': 'exclamation-circle', 'warning': 'exclamation-triangle', 'info': 'info-circle' }; return icons[type] || 'info-circle'; } function createToastContainer() { const container = document.createElement('div'); container.className = 'toast-container'; document.body.appendChild(container); return container; } function setupToasts() { // This will be called to setup any existing toast elements } // Helper function for AJAX calls async function apiCall(url, method = 'GET', data = null) { const options = { method: method, headers: { 'Content-Type': 'application/json', } }; if (data) { options.body = JSON.stringify(data); } try { const response = await fetch(url, options); const json = await response.json(); return json; } catch (error) { console.error('API Error:', error); showToast('Lỗi kết nối', 'error'); return null; } } // Delete confirmation function confirmDelete(itemName = 'item') { return confirm(`Bạn chắc chắn muốn xóa ${itemName} này?`); } // Format date function formatDate(date) { const d = new Date(date); return d.toLocaleDateString('vi-VN') + ' ' + d.toLocaleTimeString('vi-VN'); } // Export functions for use in pages window.showToast = showToast; window.apiCall = apiCall; window.confirmDelete = confirmDelete; window.formatDate = formatDate;
💾 Simpan File
Batal
⬅ Naik ke assets
1 item
Nama
Tipe
Ukuran
Diubah
Aksi
📜
admin.js
js
4.6 KB
2026-05-21 05:51
✏️ Edit
👁️ View
🗑 Hapus