feat(PageContent): 支持前端导出

This commit is contained in:
cshaptx4869
2024-05-31 14:22:50 +08:00
parent 0878f4b845
commit 50d26ad29d
2 changed files with 59 additions and 5 deletions

View File

@@ -53,6 +53,7 @@
"color": "^4.2.3", "color": "^4.2.3",
"echarts": "^5.5.0", "echarts": "^5.5.0",
"element-plus": "^2.7.3", "element-plus": "^2.7.3",
"exceljs": "^4.4.0",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"net": "^1.0.2", "net": "^1.0.2",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",

View File

@@ -59,13 +59,18 @@
<template v-for="item in defaultToolbar" :key="item"> <template v-for="item in defaultToolbar" :key="item">
<!-- 刷新 --> <!-- 刷新 -->
<template v-if="item === 'refresh'"> <template v-if="item === 'refresh'">
<el-button icon="refresh" circle @click="handleToolbar(item)" /> <el-button
icon="refresh"
circle
title="刷新"
@click="handleToolbar(item)"
/>
</template> </template>
<!-- 设置 --> <!-- 筛选 -->
<template v-else-if="item === 'filter'"> <template v-else-if="item === 'filter'">
<el-popover placement="bottom" trigger="click"> <el-popover placement="bottom" trigger="click">
<template #reference> <template #reference>
<el-button icon="Operation" circle /> <el-button icon="Operation" circle title="筛选列" />
</template> </template>
<el-scrollbar max-height="350px"> <el-scrollbar max-height="350px">
<template v-for="col in cols" :key="col"> <template v-for="col in cols" :key="col">
@@ -78,9 +83,23 @@
</el-scrollbar> </el-scrollbar>
</el-popover> </el-popover>
</template> </template>
<!-- 导出 -->
<template v-else-if="item === 'exports'">
<el-button
icon="FolderOpened"
circle
title="导出"
@click="handleToolbar(item)"
/>
</template>
<!-- 搜索 --> <!-- 搜索 -->
<template v-else-if="item === 'search'"> <template v-else-if="item === 'search'">
<el-button icon="search" circle @click="handleToolbar(item)" /> <el-button
icon="search"
circle
title="搜索"
@click="handleToolbar(item)"
/>
</template> </template>
</template> </template>
</div> </div>
@@ -285,6 +304,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import ExcelJS from "exceljs";
import { ref, reactive } from "vue"; import { ref, reactive } from "vue";
import { useDateFormat } from "@vueuse/core"; import { useDateFormat } from "@vueuse/core";
import { hasAuth } from "@/plugins/permission"; import { hasAuth } from "@/plugins/permission";
@@ -352,7 +372,7 @@ export interface IContentConfig<T = any> {
} }
>; >;
// 表格工具栏右侧图标 // 表格工具栏右侧图标
defaultToolbar?: ("refresh" | "filter" | "search")[]; defaultToolbar?: ("refresh" | "filter" | "exports" | "search")[];
// table组件列属性(额外的属性templet,operat,slotName) // table组件列属性(额外的属性templet,operat,slotName)
cols: Array<{ cols: Array<{
type?: "default" | "selection" | "index" | "expand"; type?: "default" | "selection" | "index" | "expand";
@@ -417,6 +437,7 @@ const toolbar = props.contentConfig.toolbar ?? ["add", "delete"];
const defaultToolbar = props.contentConfig.defaultToolbar ?? [ const defaultToolbar = props.contentConfig.defaultToolbar ?? [
"refresh", "refresh",
"filter", "filter",
"exports",
"search", "search",
]; ];
// 表格列 // 表格列
@@ -487,12 +508,44 @@ function handleDelete(id?: number | string) {
} }
}); });
} }
// 导出
function handleExports() {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("sheet");
const columns: Partial<ExcelJS.Column>[] = [];
cols.value.forEach((col) => {
if (col.label && col.prop) {
columns.push({ header: col.label, key: col.prop });
}
});
worksheet.columns = columns;
worksheet.addRows(pageData.value);
workbook.xlsx
.writeBuffer()
.then((buffer) => {
const blob = new Blob([buffer], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const downloadUrl = window.URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = downloadUrl;
downloadLink.download = props.contentConfig.pageName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
window.URL.revokeObjectURL(downloadUrl);
})
.catch((error) => console.log("Error writing excel export", error));
}
// 操作栏 // 操作栏
function handleToolbar(name: string) { function handleToolbar(name: string) {
switch (name) { switch (name) {
case "refresh": case "refresh":
handleRefresh(); handleRefresh();
break; break;
case "exports":
handleExports();
break;
case "search": case "search":
emit("searchClick"); emit("searchClick");
break; break;