Skip to content

Commit

Permalink
perf: 导出支持dict
Browse files Browse the repository at this point in the history
  • Loading branch information
greper committed Jul 2, 2023
1 parent 043a0d6 commit dbb60bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 104 deletions.
2 changes: 0 additions & 2 deletions packages/fast-crud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { GlobalConfig, LoggerConfig, useDictDefine } from "./use";
import { App } from "vue";
import { FsSetupOptions } from "./d";
import _ from "lodash-es";
import { setupAppContext } from "./use/use-async";
export * from "./utils/index";
export * from "./use";
export * from "./components";
Expand Down Expand Up @@ -40,7 +39,6 @@ export const FastCrud = {
}

FsFormWrapper._context = app._context;
setupAppContext(app._context);

types.install();

Expand Down
1 change: 0 additions & 1 deletion packages/fast-crud/src/use/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ export * from "./use-columns";
export * from "./use-drag";
export * from "./use-form";
export * from "./config";
export * from "./use-async";
44 changes: 39 additions & 5 deletions packages/fast-crud/src/use/lib/fs-export/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash-es";
import { ColumnCompositionProps, CrudBinding } from "/src/d";
import { ColumnCompositionProps, ColumnProps, CrudBinding } from "/src/d";
import { ExcelParams, ExportColumn, ExportUtil } from "./lib/d";
import { Ref } from "vue";

Expand All @@ -15,6 +15,26 @@ export async function loadFsExportUtil(): Promise<ExportUtil> {
return lib.exportUtil;
}

export type DataFormatterContext = {
row: any;
originalRow: any;
key: string;
col: ColumnProps;
};
function defaultDataFormatter({ originalRow, row, key, col }: DataFormatterContext) {
if (col.component?.dict && originalRow[key] != null) {
//处理dict
const dict = col.component.dict;
const nodes = dict.getNodesFromDataMap(originalRow[key]);
if (nodes != null && nodes.length > 0) {
row[key] = _.map(nodes, (node) => {
return dict.getLabel(node);
}).join(",");
}
}
return row;
}

/**
* 导出配置
*/
Expand All @@ -25,9 +45,8 @@ export type ExportProps = {
server?: () => Promise<void>;
/**
* 数据mapping
* @param row
*/
dataMapping?: (row: any) => any;
dataFormatter?: (context: DataFormatterContext) => void;
/**
* 导出文件类型
*/
Expand All @@ -53,9 +72,24 @@ export async function exportTable(crudBinding: Ref<CrudBinding>, opts: ExportPro
//加载异步组件,不影响首页加载速度
const exportUtil: ExportUtil = await loadFsExportUtil();
const data = [];
const dataMapping = opts.dataMapping || ((row) => row);
for (const row of crudBinding.value.data) {
data.push(dataMapping(row));
const clone = _.cloneDeep(row);
_.each(crudBinding.value.table.columnsMap, (col: ColumnCompositionProps) => {
if (col.exportable !== false && col.key !== "_index") {
const mapping = {
row: clone,
originalRow: row,
key: col.key,
col
};
defaultDataFormatter(mapping);
if (opts.dataFormatter) {
opts.dataFormatter(mapping);
}
}
});

data.push(clone);
}
const expOpts = _.merge(
{
Expand Down
96 changes: 0 additions & 96 deletions packages/fast-crud/src/use/use-async.ts

This file was deleted.

0 comments on commit dbb60bf

Please sign in to comment.