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:
SemmyWong 2022-07-01 09:13:16 +08:00 committed by GitHub
parent 3d1ae8fcae
commit 41cb3da448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 3 deletions

View File

@ -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']);
});
});

View File

@ -2,13 +2,15 @@ import { Context, Next } from '@nocobase/actions';
import { Repository } from '@nocobase/database';
import xlsx from 'node-xlsx';
import render from '../renders';
import { columns2Appends } from '../utils';
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;
if (typeof columns === 'string') {
columns = JSON.parse(columns);
}
const appends = columns2Appends(columns);
columns = columns?.filter((col) => col?.dataIndex?.length > 0);
const repository = ctx.db.getRepository<any>(resourceName, resourceOf) as Repository;
const collection = repository.collection;

View File

@ -2,9 +2,30 @@ import moment from 'moment';
export async function _(field, row, ctx, column?: any) {
if (column?.dataIndex.length > 1) {
return column.dataIndex.reduce((result, col) => {
return 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];
}
}
}, row);
if (Array.isArray(result)) {
return result.join(',');
} else {
return result;
}
} else {
return row.get(field.name);
}

View 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);
}

View File

@ -0,0 +1 @@
export * from './columns2Appends';