fix: export association table data (#561)
* fix: export association table data * fix: fix export * feat: add test case * feat: add test case
This commit is contained in:
parent
3d1ae8fcae
commit
41cb3da448
@ -0,0 +1,45 @@
|
|||||||
|
import { columns2Appends } from '../../utils/columns2Appends';
|
||||||
|
|
||||||
|
describe('utils', () => {
|
||||||
|
let columns = null;
|
||||||
|
beforeEach(async () => {});
|
||||||
|
afterEach(async () => {});
|
||||||
|
|
||||||
|
it('first columns2Appends', async () => {
|
||||||
|
columns = [
|
||||||
|
{ dataIndex: ['f_kp6gk63udss'], defaultTitle: '商品名称' },
|
||||||
|
{
|
||||||
|
dataIndex: ['f_brjkofr2mbt'],
|
||||||
|
enum: [
|
||||||
|
{ value: 'lzjqrrw2vdl', label: '节' },
|
||||||
|
{ value: 'i0qarqlm87m', label: '胡' },
|
||||||
|
{ value: '1fpb8x0swq1', label: '一一' },
|
||||||
|
],
|
||||||
|
defaultTitle: '工在在',
|
||||||
|
},
|
||||||
|
{ dataIndex: ['f_qhvvfuignh2', 'createdBy', 'id'], defaultTitle: 'ID' },
|
||||||
|
{ dataIndex: ['f_wu28mus1c65', 'roles', 'title'], defaultTitle: '角色名称' },
|
||||||
|
];
|
||||||
|
const appends = columns2Appends(columns);
|
||||||
|
expect(appends).toMatchObject(['f_qhvvfuignh2.createdBy', 'f_wu28mus1c65.roles']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('second columns2Appends', async () => {
|
||||||
|
columns = [
|
||||||
|
{ dataIndex: ['f_kp6gk63udss'], defaultTitle: '商品名称' },
|
||||||
|
{
|
||||||
|
dataIndex: ['f_brjkofr2mbt'],
|
||||||
|
enum: [
|
||||||
|
{ value: 'lzjqrrw2vdl', label: '节' },
|
||||||
|
{ value: 'i0qarqlm87m', label: '胡' },
|
||||||
|
{ value: '1fpb8x0swq1', label: '一一' },
|
||||||
|
],
|
||||||
|
defaultTitle: '工在在',
|
||||||
|
},
|
||||||
|
{ dataIndex: ['f_qhvvfuignh2', 'createdBy', 'id'], defaultTitle: 'ID' },
|
||||||
|
{ dataIndex: ['f_qhvvfuignh2', 'createdBy', 'nickname'], defaultTitle: '角色名称' },
|
||||||
|
];
|
||||||
|
const appends = columns2Appends(columns);
|
||||||
|
expect(appends).toMatchObject(['f_qhvvfuignh2.createdBy']);
|
||||||
|
});
|
||||||
|
});
|
@ -2,13 +2,15 @@ import { Context, Next } from '@nocobase/actions';
|
|||||||
import { Repository } from '@nocobase/database';
|
import { Repository } from '@nocobase/database';
|
||||||
import xlsx from 'node-xlsx';
|
import xlsx from 'node-xlsx';
|
||||||
import render from '../renders';
|
import render from '../renders';
|
||||||
|
import { columns2Appends } from '../utils';
|
||||||
|
|
||||||
export async function exportXlsx(ctx: Context, next: Next) {
|
export async function exportXlsx(ctx: Context, next: Next) {
|
||||||
let { title, columns, filter, fields, except, appends } = ctx.action.params;
|
let { title, columns, filter, fields, except } = ctx.action.params;
|
||||||
const { resourceName, resourceOf } = ctx.action;
|
const { resourceName, resourceOf } = ctx.action;
|
||||||
if (typeof columns === 'string') {
|
if (typeof columns === 'string') {
|
||||||
columns = JSON.parse(columns);
|
columns = JSON.parse(columns);
|
||||||
}
|
}
|
||||||
|
const appends = columns2Appends(columns);
|
||||||
columns = columns?.filter((col) => col?.dataIndex?.length > 0);
|
columns = columns?.filter((col) => col?.dataIndex?.length > 0);
|
||||||
const repository = ctx.db.getRepository<any>(resourceName, resourceOf) as Repository;
|
const repository = ctx.db.getRepository<any>(resourceName, resourceOf) as Repository;
|
||||||
const collection = repository.collection;
|
const collection = repository.collection;
|
||||||
|
@ -2,9 +2,30 @@ import moment from 'moment';
|
|||||||
|
|
||||||
export async function _(field, row, ctx, column?: any) {
|
export async function _(field, row, ctx, column?: any) {
|
||||||
if (column?.dataIndex.length > 1) {
|
if (column?.dataIndex.length > 1) {
|
||||||
return column.dataIndex.reduce((result, col) => {
|
const result = column.dataIndex.reduce((result, col) => {
|
||||||
|
if (Array.isArray(result)) {
|
||||||
|
const subResults = [];
|
||||||
|
for (const r of result) {
|
||||||
|
subResults.push(r?.[col]);
|
||||||
|
}
|
||||||
|
return subResults;
|
||||||
|
} else {
|
||||||
|
if (Array.isArray(result?.[col])) {
|
||||||
|
const subResults = [];
|
||||||
|
for (const r of result?.[col]) {
|
||||||
|
subResults.push(r);
|
||||||
|
}
|
||||||
|
return subResults;
|
||||||
|
} else {
|
||||||
return result?.[col];
|
return result?.[col];
|
||||||
|
}
|
||||||
|
}
|
||||||
}, row);
|
}, row);
|
||||||
|
if (Array.isArray(result)) {
|
||||||
|
return result.join(',');
|
||||||
|
} else {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return row.get(field.name);
|
return row.get(field.name);
|
||||||
}
|
}
|
||||||
|
13
packages/plugins/export/src/server/utils/columns2Appends.ts
Normal file
13
packages/plugins/export/src/server/utils/columns2Appends.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
export function columns2Appends(columns) {
|
||||||
|
const appends = new Set([]);
|
||||||
|
for (const column of columns) {
|
||||||
|
if (column.dataIndex.length > 1) {
|
||||||
|
const appendColumns = [];
|
||||||
|
for (let i = 0, iLen = column.dataIndex.length - 1; i < iLen; i++) {
|
||||||
|
appendColumns.push(column.dataIndex[i]);
|
||||||
|
}
|
||||||
|
appends.add(appendColumns.join('.'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Array.from(appends);
|
||||||
|
}
|
1
packages/plugins/export/src/server/utils/index.ts
Normal file
1
packages/plugins/export/src/server/utils/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './columns2Appends';
|
Loading…
Reference in New Issue
Block a user