/** * 公共分页工具函数 */ // 渲染分页导航 function renderPagination(pagination, pageChangeCallback) { const paginationEl = document.getElementById('pagination'); if (!paginationEl) return; // 验证分页数据 if (!pagination || typeof pagination !== 'object' || !Number.isFinite(pagination.total_pages) || pagination.total_pages <= 1) { paginationEl.innerHTML = ''; return; } let html = ''; // 上一页 if (pagination.has_prev) { html += `
  • 上一页
  • `; } // 页码 const startPage = Math.max(1, pagination.current_page - 2); const endPage = Math.min(pagination.total_pages, pagination.current_page + 2); if (startPage > 1) { html += `
  • 1
  • `; if (startPage > 2) { html += `
  • ...
  • `; } } for (let i = startPage; i <= endPage; i++) { html += `
  • ${i}
  • `; } if (endPage < pagination.total_pages) { if (endPage < pagination.total_pages - 1) { html += `
  • ...
  • `; } html += `
  • ${pagination.total_pages}
  • `; } // 下一页 if (pagination.has_next) { html += `
  • 下一页
  • `; } paginationEl.innerHTML = html; // 绑定分页点击事件 paginationEl.querySelectorAll('.page-link').forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); const page = parseInt(this.dataset.page); if (page && page !== pagination.current_page) { if (typeof pageChangeCallback === 'function') { pageChangeCallback(page); } } }); }); } // 获取URL参数 function getUrlParameter(name) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(name); } // 设置URL参数 function setUrlParameter(param, value) { const urlParams = new URLSearchParams(window.location.search); if (value === null || value === '') { urlParams.delete(param); } else { urlParams.set(param, value); } window.history.replaceState({}, '', `${window.location.pathname}?${urlParams.toString()}`); } // 批量更新复选框状态 function setupBatchSelection(selectAllId, itemCheckboxClass, selectedCountId, batchButtons) { const selectAll = document.getElementById(selectAllId); if (!selectAll) return; // 全选/取消全选 selectAll.addEventListener('change', function() { const checkboxes = document.querySelectorAll(itemCheckboxClass); checkboxes.forEach(checkbox => { checkbox.checked = this.checked; }); updateBatchButtons(selectedCountId, batchButtons); }); // 单个复选框事件 document.querySelectorAll(itemCheckboxClass).forEach(checkbox => { checkbox.addEventListener('change', () => { updateBatchButtons(selectedCountId, batchButtons); }); }); // 初始状态 updateBatchButtons(selectedCountId, batchButtons); } // 更新批量操作按钮状态 function updateBatchButtons(selectedCountId, batchButtons) { const selectedCount = document.querySelectorAll('.license-checkbox:checked, .product-checkbox:checked, .device-checkbox:checked').length; if (selectedCountId) { const countElement = document.getElementById(selectedCountId); if (countElement) { countElement.textContent = `已选择 ${selectedCount} 项`; } } if (batchButtons) { batchButtons.forEach(buttonId => { const button = document.getElementById(buttonId); if (button) { button.style.display = selectedCount > 0 ? 'inline-block' : 'none'; } }); } }