feat(web-admin): 月度样本总览支持多选导出 (#195)
列表序号前增加多选框;勾选后导出已选行,未勾选仍按当前筛选全量导出。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0f2d0c5b13
commit
cf8358a809
1 file changed
+58
-25
@@ -42,7 +42,10 @@
|
||||
|
||||
<div class="list-toolbar">
|
||||
<div class="list-toolbar-row">
|
||||
<div class="list-toolbar-selected">共 {{ pagination.total }} 条</div>
|
||||
<div class="list-toolbar-selected">
|
||||
已选 <span class="sel-num">{{ selectedRows.length }}</span> 项 / 共
|
||||
{{ pagination.total }} 条
|
||||
</div>
|
||||
<el-button
|
||||
v-if="canExport"
|
||||
class="btn-main"
|
||||
@@ -51,13 +54,21 @@
|
||||
@click="handleExport"
|
||||
>
|
||||
<el-icon><Download /></el-icon>
|
||||
导出
|
||||
{{ selectedRows.length > 0 ? '导出已选' : '导出' }}
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="list-body list-wrap">
|
||||
<el-table class="list-table" :data="tableData" v-loading="loading" stripe row-key="id">
|
||||
<el-table
|
||||
class="list-table"
|
||||
:data="tableData"
|
||||
v-loading="loading"
|
||||
stripe
|
||||
row-key="id"
|
||||
@selection-change="handleSelectionChange"
|
||||
>
|
||||
<el-table-column type="selection" min-width="55" align="center" header-align="center" />
|
||||
<el-table-column
|
||||
type="index"
|
||||
label="序号"
|
||||
@@ -140,6 +151,7 @@
|
||||
const loading = ref(false)
|
||||
const exporting = ref(false)
|
||||
const tableData = ref<MonthlySampleOverviewRow[]>([])
|
||||
const selectedRows = ref<MonthlySampleOverviewRow[]>([])
|
||||
const searchForm = reactive({
|
||||
receptionTimeRange: currentMonthRange() as [string, string] | null,
|
||||
keyword: ''
|
||||
@@ -166,8 +178,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
function handleSelectionChange(rows: MonthlySampleOverviewRow[]) {
|
||||
selectedRows.value = rows || []
|
||||
}
|
||||
|
||||
async function loadData() {
|
||||
loading.value = true
|
||||
selectedRows.value = []
|
||||
try {
|
||||
const res = await fetchMonthlySampleOverviewList(
|
||||
listParams(pagination.page, pagination.pageSize)
|
||||
@@ -201,6 +218,36 @@
|
||||
loadData()
|
||||
}
|
||||
|
||||
function buildExportRows(rows: MonthlySampleOverviewRow[]) {
|
||||
const headers = ['序号', ...columns.map((c) => c.label)]
|
||||
const sheetRows = rows.map((row, idx) => {
|
||||
const line: Record<string, string | number> = { 序号: idx + 1 }
|
||||
columns.forEach((c) => {
|
||||
const v = row[c.prop as keyof MonthlySampleOverviewRow]
|
||||
line[c.label] = v === null || v === undefined || v === '' ? '' : String(v)
|
||||
})
|
||||
return line
|
||||
})
|
||||
return { headers, sheetRows }
|
||||
}
|
||||
|
||||
async function fetchAllFilteredRows() {
|
||||
const pageSize = 200
|
||||
let page = 1
|
||||
let total = Number.POSITIVE_INFINITY
|
||||
const rows: MonthlySampleOverviewRow[] = []
|
||||
while (rows.length < total) {
|
||||
const res = await fetchMonthlySampleOverviewList(listParams(page, pageSize))
|
||||
total = Number(res?.total ?? 0)
|
||||
const batch = (res?.list || []) as MonthlySampleOverviewRow[]
|
||||
rows.push(...batch)
|
||||
if (!batch.length || batch.length < pageSize) break
|
||||
page += 1
|
||||
if (page > 500) break
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
async function handleExport() {
|
||||
if (!canExport.value) {
|
||||
ElMessage.warning('无导出权限')
|
||||
@@ -208,33 +255,19 @@
|
||||
}
|
||||
exporting.value = true
|
||||
try {
|
||||
const pageSize = 200
|
||||
let page = 1
|
||||
let total = Number.POSITIVE_INFINITY
|
||||
const rows: MonthlySampleOverviewRow[] = []
|
||||
while (rows.length < total) {
|
||||
const res = await fetchMonthlySampleOverviewList(listParams(page, pageSize))
|
||||
total = Number(res?.total ?? 0)
|
||||
const batch = (res?.list || []) as MonthlySampleOverviewRow[]
|
||||
rows.push(...batch)
|
||||
if (!batch.length || batch.length < pageSize) break
|
||||
page += 1
|
||||
if (page > 500) break
|
||||
const useSelected = selectedRows.value.length > 0
|
||||
const rows = useSelected ? [...selectedRows.value] : await fetchAllFilteredRows()
|
||||
if (!rows.length) {
|
||||
ElMessage.warning(useSelected ? '请先勾选要导出的记录' : '当前筛选无数据可导出')
|
||||
return
|
||||
}
|
||||
const headers = ['序号', ...columns.map((c) => c.label)]
|
||||
const sheetRows = rows.map((row, idx) => {
|
||||
const line: Record<string, string | number> = { 序号: idx + 1 }
|
||||
columns.forEach((c) => {
|
||||
const v = row[c.prop as keyof MonthlySampleOverviewRow]
|
||||
line[c.label] = v === null || v === undefined || v === '' ? '' : String(v)
|
||||
})
|
||||
return line
|
||||
})
|
||||
const { headers, sheetRows } = buildExportRows(rows)
|
||||
const sheet = XLSX.utils.json_to_sheet(sheetRows, { header: headers })
|
||||
const workbook = XLSX.utils.book_new()
|
||||
XLSX.utils.book_append_sheet(workbook, sheet, '月度样本信息总览')
|
||||
const stamp = new Date().toISOString().slice(0, 19).replace(/[:T]/g, '')
|
||||
XLSX.writeFile(workbook, `月度样本信息总览_${stamp}.xlsx`)
|
||||
const suffix = useSelected ? `已选${rows.length}条_` : ''
|
||||
XLSX.writeFile(workbook, `月度样本信息总览_${suffix}${stamp}.xlsx`)
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.message || '导出失败')
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user