第一次提交
This commit is contained in:
134
static/js/pagination.js
Normal file
134
static/js/pagination.js
Normal file
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* 公共分页工具函数
|
||||
*/
|
||||
|
||||
// 渲染分页导航
|
||||
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 += `<li class="page-item">
|
||||
<a class="page-link" href="#" data-page="${pagination.current_page - 1}">上一页</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
// 页码
|
||||
const startPage = Math.max(1, pagination.current_page - 2);
|
||||
const endPage = Math.min(pagination.total_pages, pagination.current_page + 2);
|
||||
|
||||
if (startPage > 1) {
|
||||
html += `<li class="page-item"><a class="page-link" href="#" data-page="1">1</a></li>`;
|
||||
if (startPage > 2) {
|
||||
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = startPage; i <= endPage; i++) {
|
||||
html += `<li class="page-item ${i === pagination.current_page ? 'active' : ''}">
|
||||
<a class="page-link" href="#" data-page="${i}">${i}</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
if (endPage < pagination.total_pages) {
|
||||
if (endPage < pagination.total_pages - 1) {
|
||||
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
||||
}
|
||||
html += `<li class="page-item"><a class="page-link" href="#" data-page="${pagination.total_pages}">${pagination.total_pages}</a></li>`;
|
||||
}
|
||||
|
||||
// 下一页
|
||||
if (pagination.has_next) {
|
||||
html += `<li class="page-item">
|
||||
<a class="page-link" href="#" data-page="${pagination.current_page + 1}">下一页</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user