feat: add full directory table export

This commit is contained in:
WuYongJie 2026-09-03 17:57:44 +08:00
parent d7c564d900
commit 2329fb7a5f
2 changed files with 69 additions and 4 deletions

View File

@ -225,6 +225,7 @@ return [
['name' => 'profit_center', 'label' => '店铺所属利润中心'],
['name' => 'account_type', 'label' => '账号类型'],
['name' => 'account', 'label' => '账号'],
['name' => 'password_configured', 'label' => '密码', 'kind' => 'secret'],
['name' => 'bound_phone', 'label' => '绑定手机号', 'kind' => 'secret'],
['name' => 'verification_contact', 'label' => '手机号验证码对接人'],
['name' => 'account_group_name', 'label' => '账号分组名称'],

View File

@ -115,7 +115,7 @@
background: var(--vip-content-bg, #f8f6f1);
}
.directory-table-tools input {
.directory-table-tools > [data-directory-search] {
flex: 1 1 16rem;
min-width: 12rem;
height: 2.25rem;
@ -136,6 +136,11 @@
cursor: pointer;
}
.directory-table-tools button:disabled {
cursor: wait;
opacity: 0.65;
}
.directory-field-picker {
position: relative;
display: inline-block;
@ -160,8 +165,29 @@
}
.directory-field-picker__panel[hidden] { display: none; }
.directory-field-picker__option { display: flex; align-items: center; gap: 0.4rem; color: var(--vip-content-ink, #171717); font-size: 0.75rem; }
.directory-field-picker__option input { flex: 0 0 auto; }
.directory-field-picker__option {
display: flex;
align-items: center;
gap: 0.4rem;
min-width: 0;
color: var(--vip-content-ink, #171717);
font-size: 0.75rem;
line-height: 1.35;
cursor: pointer;
}
.directory-field-picker__option input[type="checkbox"] {
flex: 0 0 auto;
width: 1rem;
min-width: 1rem;
height: 1rem;
margin: 0;
accent-color: var(--vip-content-accent, #b9954f);
}
.directory-field-picker__option span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.directory-table [data-column-key="sales_01"],
.directory-table [data-column-key="sales_02"],
.directory-table [data-column-key="sales_03"],
@ -316,7 +342,7 @@
}
</style>
<section class="directory-table-page" data-directory-page="{{ $page['slug'] ?? '' }}" aria-label="{{ $page['title'] ?? $section['title'] }}">
<section class="directory-table-page" data-directory-page="{{ $page['slug'] ?? '' }}" data-directory-last-page="{{ $directoryLastPage }}" aria-label="{{ $page['title'] ?? $section['title'] }}">
<header class="directory-table-head">
<div class="directory-table-title">
<span class="material-icons-outlined" aria-hidden="true">{{ $directory['icon'] ?? 'table_view' }}</span>
@ -368,6 +394,7 @@
</div>
</div>
<button type="button" data-directory-export>导出当前页</button>
<button type="button" data-directory-export-all>导出全部数据</button>
<button type="button" data-directory-template>下载表头模板</button>
</div>
<div class="directory-table-scroll">
@ -499,6 +526,43 @@
link.click();
URL.revokeObjectURL(link.href);
});
tools.querySelector('[data-directory-export-all]')?.addEventListener('click', async (event) => {
const button = event.currentTarget;
const lastPage = Number(section.dataset.directoryLastPage || 1);
const selected = new Set(checks.filter((check) => check.checked).map((check) => check.value));
const cellsForExport = (row) => [...row.children]
.filter((cell) => !cell.dataset.columnKey || selected.has(cell.dataset.columnKey));
const header = cellsForExport(table.tHead.rows[0]).map((cell) => csvEscape(cell.textContent.trim())).join(',');
const lines = [];
button.disabled = true;
const originalLabel = button.textContent;
try {
for (let page = 1; page <= lastPage; page += 1) {
button.textContent = `导出中 ${page}/${lastPage}`;
const url = new URL(window.location.href);
url.searchParams.set('page', String(page));
const response = await fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } });
if (!response.ok) throw new Error(`第 ${page} 页读取失败`);
const html = await response.text();
const parsed = new DOMParser().parseFromString(html, 'text/html');
parsed.querySelectorAll('.directory-table tbody tr:not(.is-empty)').forEach((row) => {
lines.push(cellsForExport(row).map((cell) => csvEscape(cell.textContent.trim())).join(','));
});
}
const csv = [header, ...lines].join('\n');
const blob = new Blob([`\ufeff${csv}`], { type: 'text/csv;charset=utf-8' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `${section.dataset.directoryPage || 'directory'}-all-export.csv`;
link.click();
URL.revokeObjectURL(link.href);
} catch (error) {
window.alert(error.message || '导出全部数据失败,请稍后重试');
} finally {
button.disabled = false;
button.textContent = originalLabel;
}
});
tools.querySelector('[data-directory-template]')?.addEventListener('click', () => {
const header = [...table.querySelectorAll('thead th')].map((cell) => csvEscape(cell.textContent.trim())).join(',');
const blob = new Blob([`\ufeff${header}\n`], { type: 'text/csv;charset=utf-8' });