Add customizable tabular data exports

This commit is contained in:
biss committed 2026-11-22 21:07:16 +08:00
1 parent 5d5781e707
commit 3e6cd85662
15 files changed
+312 -40

No files matched your search

+2 -1
View File
@@ -31,6 +31,7 @@ export async function downloadApiFile(
path: string,
fallbackName: string,
config?: AxiosRequestConfig,
customName?: string,
) {
const response = await http.get(path, {
...config,
@@ -44,7 +45,7 @@ export async function downloadApiFile(
const url = URL.createObjectURL(response.data)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = responseFileName(
anchor.download = customName || responseFileName(
response.headers['content-disposition'], uniqueFallbackName(path, fallbackName),
)
document.body.appendChild(anchor)
+1
View File
@@ -18,6 +18,7 @@ declare module 'vue' {
ArchiveDialog: typeof import('./components/ArchiveDialog.vue')['default']
ChinesePostalCodeInput: typeof import('./components/ChinesePostalCodeInput.vue')['default']
CourseDiscussionPanel: typeof import('./components/CourseDiscussionPanel.vue')['default']
CustomExportDialog: typeof import('./components/CustomExportDialog.vue')['default']
ElAlert: typeof import('element-plus/es')['ElAlert']
ElBadge: typeof import('element-plus/es')['ElBadge']
ElButton: typeof import('element-plus/es')['ElButton']
+84
View File
@@ -0,0 +1,84 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const emit = defineEmits<{ export: [columns: number[], filename: string, format: string] }>()
const visible = ref(false)
const headers = ref<string[]>([])
const selected = ref<number[]>([])
const filename = ref('')
const format = ref('xlsx')
const dragging = ref<number | null>(null)
const available = computed(() => headers.value.map((label, index) => ({ label, index })).filter(item => !selected.value.includes(item.index)))
function open(columns: string[], defaultName: string) {
headers.value = columns
selected.value = columns.map((_, index) => index)
filename.value = defaultName.replace(/\.(xlsx|txt|dbf)$/i, '')
format.value = 'xlsx'
visible.value = true
}
function move(from: number, to: number) {
if (to < 0 || to >= selected.value.length || from === to) return
const [item] = selected.value.splice(from, 1)
if (item !== undefined) selected.value.splice(to, 0, item)
}
function submit() {
const name = filename.value.trim().replace(/[\\/:*?"<>|]/g, '_').replace(/\.(xlsx|txt|dbf)$/i, '')
if (!selected.value.length || !name) return
emit('export', [...selected.value], `${name}.${format.value}`, format.value)
visible.value = false
}
defineExpose({ open })
</script>
<template>
<el-dialog v-model="visible" :title="t('customExport.title')" width="760px" class="custom-export-dialog" destroy-on-close>
<p class="export-hint">{{ t('customExport.hint') }}</p>
<div class="export-columns">
<section class="column-panel">
<div class="panel-heading"><strong>{{ t('customExport.available') }}</strong><el-button link type="primary" @click="selected = headers.map((_, index) => index)">{{ t('customExport.addAll') }}</el-button></div>
<div class="column-list">
<button v-for="item in available" :key="item.index" class="column-item available-item" type="button" @click="selected.push(item.index)"><span>{{ item.label }}</span><span aria-hidden="true">+</span></button>
<p v-if="!available.length" class="empty">{{ t('customExport.noneAvailable') }}</p>
</div>
</section>
<section class="column-panel">
<div class="panel-heading"><strong>{{ t('customExport.selected') }} ({{ selected.length }})</strong><el-button link type="primary" @click="selected = []">{{ t('customExport.clear') }}</el-button></div>
<div class="column-list">
<div v-for="(index, position) in selected" :key="index" class="column-item" draggable="true" @dragstart="dragging = position" @dragend="dragging = null" @dragover.prevent @drop.prevent="dragging !== null && move(dragging, position); dragging = null">
<span class="column-label"><span aria-hidden="true" class="drag-handle">⠿</span> {{ headers[index] }}</span>
<span class="column-actions"><button type="button" :aria-label="t('customExport.up')" :disabled="position === 0" @click="move(position, position - 1)">↑</button><button type="button" :aria-label="t('customExport.down')" :disabled="position === selected.length - 1" @click="move(position, position + 1)">↓</button><button type="button" :aria-label="t('customExport.remove')" @click="selected.splice(position, 1)">×</button></span>
</div>
<p v-if="!selected.length" class="empty">{{ t('customExport.noneSelected') }}</p>
</div>
</section>
</div>
<el-form label-position="top" class="export-options">
<el-form-item :label="t('customExport.format')"><el-radio-group v-model="format"><el-radio value="xlsx">Excel (.xlsx)</el-radio><el-radio value="txt">TXT</el-radio><el-radio value="dbf">DBF</el-radio></el-radio-group></el-form-item>
<p v-if="format === 'dbf'" class="export-hint">{{ t('customExport.dbfHint') }}</p>
<el-form-item :label="t('customExport.filename')"><el-input v-model="filename" maxlength="120"><template #append>.{{ format }}</template></el-input></el-form-item>
</el-form>
<template #footer><el-button @click="visible = false">{{ t('businessLabels.cancel') }}</el-button><el-button type="primary" :disabled="!selected.length || !filename.trim()" @click="submit">{{ t('baseData.export') }}</el-button></template>
</el-dialog>
</template>
<style scoped>
.export-hint { margin: 0 0 16px; color: var(--el-text-color-secondary); }
.export-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.column-panel { border: 1px solid var(--el-border-color); border-radius: 8px; overflow: hidden; min-width: 0; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: var(--el-fill-color-light); }
.column-list { min-height: 210px; max-height: 320px; overflow-y: auto; padding: 10px; }
.column-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; margin-bottom: 8px; padding: 8px 10px; border: 1px solid var(--el-border-color); border-radius: 5px; background: var(--el-bg-color); color: var(--el-text-color-primary); text-align: left; }
.available-item { cursor: pointer; }
.available-item:hover { border-color: var(--el-color-primary); color: var(--el-color-primary); }
.column-label { min-width: 0; overflow-wrap: anywhere; }
.drag-handle { cursor: grab; color: var(--el-text-color-secondary); }
.column-actions { display: flex; flex-shrink: 0; gap: 3px; }
.column-actions button { border: 0; background: transparent; color: var(--el-color-primary); cursor: pointer; font-size: 16px; }
.column-actions button:disabled { color: var(--el-disabled-text-color); cursor: default; }
.empty { color: var(--el-text-color-placeholder); text-align: center; }
.export-options { margin-top: 16px; }
@media (max-width: 600px) { .export-columns { grid-template-columns: 1fr; } .column-list { min-height: 80px; max-height: 180px; } }
</style>
Loaded 3 of 15 files, more files were not shown because too many files have changed in this diff. Show more