|
|
@ -3,78 +3,69 @@ |
|
|
<template #title> |
|
|
<template #title> |
|
|
<span>{{ t('common.export') }}</span> |
|
|
<span>{{ t('common.export') }}</span> |
|
|
</template> |
|
|
</template> |
|
|
<ExportOutlined @click="exportData" /> |
|
|
<DownloadOutlined @click="exportData" /> |
|
|
<ExpExcelModal @register="registerModal" @success="handleExport" /> |
|
|
<ExpExcelModal @register="registerModal" @success="handleExport" /> |
|
|
</Tooltip> |
|
|
</Tooltip> |
|
|
</template> |
|
|
</template> |
|
|
<script lang="ts"> |
|
|
<script lang="ts" setup> |
|
|
import { defineComponent } from 'vue'; |
|
|
|
|
|
import { Tooltip } from 'ant-design-vue'; |
|
|
import { Tooltip } from 'ant-design-vue'; |
|
|
import { ExportOutlined } from '@ant-design/icons-vue'; |
|
|
import { DownloadOutlined } from '@ant-design/icons-vue'; |
|
|
import { useI18n } from '/@/hooks/web/useI18n'; |
|
|
import { useI18n } from '/@/hooks/web/useI18n'; |
|
|
import { useTableContext } from '../../hooks/useTableContext'; |
|
|
import { useTableContext } from '../../hooks/useTableContext'; |
|
|
import { ExpExcelModal, jsonToSheetXlsx } from '/@/components/Excel'; |
|
|
import { ExpExcelModal, jsonToSheetXlsx } from '/@/components/Excel'; |
|
|
import { useModal } from '/@/components/Modal'; |
|
|
import { useModal } from '/@/components/Modal'; |
|
|
import { isString } from '/@/utils/is'; |
|
|
import { isString } from '/@/utils/is'; |
|
|
|
|
|
|
|
|
export default defineComponent({ |
|
|
const table = useTableContext(); |
|
|
name: 'TableExport', |
|
|
const { t } = useI18n(); |
|
|
components: { |
|
|
const [registerModal, { openModal }] = useModal(); |
|
|
ExpExcelModal, |
|
|
|
|
|
ExportOutlined, |
|
|
|
|
|
Tooltip, |
|
|
|
|
|
}, |
|
|
|
|
|
setup() { |
|
|
|
|
|
const table = useTableContext(); |
|
|
|
|
|
const { t } = useI18n(); |
|
|
|
|
|
const [registerModal, { openModal }] = useModal(); |
|
|
|
|
|
|
|
|
|
|
|
function exportData() { |
|
|
function exportData() { |
|
|
openModal(true); |
|
|
openModal(true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function handleExport(options) { |
|
|
function handleExport(options) { |
|
|
const dataSource = table.getDataSource(); |
|
|
table.setLoading(true); |
|
|
// 列排序过滤 |
|
|
try { |
|
|
const columns = table.getColumns() |
|
|
exportDataToExcel(options); |
|
|
.filter((col) => !col.flag || col.flag === 'DEFAULT') |
|
|
} finally { |
|
|
.filter((col) => isString(col.title) && isString(col.dataIndex)); |
|
|
table.setLoading(false); |
|
|
// 标题列 |
|
|
} |
|
|
const header: {[key:string]: string} = {}; |
|
|
} |
|
|
columns.forEach((col) => { |
|
|
|
|
|
header[String(col.dataIndex)] = String(col.title); |
|
|
|
|
|
}); |
|
|
|
|
|
// 数据列 |
|
|
|
|
|
const rows: {[key:string]: string}[] = []; |
|
|
|
|
|
dataSource.forEach((data) => { |
|
|
|
|
|
const row: {[key:string]: string} = {}; |
|
|
|
|
|
columns.forEach((col) => { |
|
|
|
|
|
const colName = String(col.dataIndex); |
|
|
|
|
|
if (Reflect.has(data, colName)) { |
|
|
|
|
|
row[colName] = data[colName]; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (Object.keys(row).length > 0) { |
|
|
|
|
|
rows.push(row); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
// 输出到excel |
|
|
|
|
|
jsonToSheetXlsx({ |
|
|
|
|
|
data: rows, |
|
|
|
|
|
header: header, |
|
|
|
|
|
filename: options.filename, |
|
|
|
|
|
write2excelOpts: { |
|
|
|
|
|
bookType: options.bookType, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
function exportDataToExcel(options) { |
|
|
registerModal, |
|
|
const dataSource = table.getDataSource(); |
|
|
exportData, |
|
|
// 列排序过滤 |
|
|
handleExport, |
|
|
const columns = table.getColumns() |
|
|
t, |
|
|
.filter((col) => !col.flag || col.flag === 'DEFAULT') |
|
|
}; |
|
|
.filter((col) => isString(col.title) && isString(col.dataIndex)); |
|
|
}, |
|
|
// 标题列 |
|
|
}); |
|
|
const header: {[key:string]: string} = {}; |
|
|
|
|
|
columns.forEach((col) => { |
|
|
|
|
|
header[String(col.dataIndex)] = String(col.title); |
|
|
|
|
|
}); |
|
|
|
|
|
// 数据列 |
|
|
|
|
|
const rows: {[key:string]: string}[] = []; |
|
|
|
|
|
dataSource.forEach((data) => { |
|
|
|
|
|
const row: {[key:string]: string} = {}; |
|
|
|
|
|
columns.forEach((col) => { |
|
|
|
|
|
const colName = String(col.dataIndex); |
|
|
|
|
|
if (Reflect.has(data, colName)) { |
|
|
|
|
|
row[colName] = data[colName]; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (Object.keys(row).length > 0) { |
|
|
|
|
|
rows.push(row); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
// 输出到excel |
|
|
|
|
|
jsonToSheetXlsx({ |
|
|
|
|
|
data: rows, |
|
|
|
|
|
header: header, |
|
|
|
|
|
filename: options.filename, |
|
|
|
|
|
write2excelOpts: { |
|
|
|
|
|
bookType: options.bookType, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|