From 0754c5979af2e764aef448bacb1fd58cd05c7431 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 28 Dec 2020 11:38:23 +0800 Subject: [PATCH] fix: hide the paired linkTo field in the table/form view --- packages/app/src/components/views/index.tsx | 2 ++ packages/plugin-pages/src/actions/getView.ts | 25 +++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/app/src/components/views/index.tsx b/packages/app/src/components/views/index.tsx index 22b509971..e7d1d4db6 100644 --- a/packages/app/src/components/views/index.tsx +++ b/packages/app/src/components/views/index.tsx @@ -46,6 +46,7 @@ export default function ViewFactory(props: ViewProps) { associatedKey, resourceName, resourceTarget, + resourceFieldName, viewName, mode, reference, @@ -58,6 +59,7 @@ export default function ViewFactory(props: ViewProps) { values: { associatedKey: associatedKey, associatedName: associatedName, + resourceFieldName: resourceName, mode }, }; diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts index a08cb76e9..0c9f5d3cb 100644 --- a/packages/plugin-pages/src/actions/getView.ts +++ b/packages/plugin-pages/src/actions/getView.ts @@ -1,4 +1,4 @@ -import { Model, ModelCtor } from '@nocobase/database'; +import { Model, ModelCtor, BELONGSTOMANY } from '@nocobase/database'; import { get, set } from 'lodash'; import { Op } from 'sequelize'; @@ -130,7 +130,7 @@ const transforms = { }; export default async (ctx, next) => { - const { resourceName, resourceKey } = ctx.action.params; + const { resourceName, resourceKey, values = {} } = ctx.action.params; const [View, Collection, Field, Action] = ctx.db.getModels(['views', 'collections', 'fields', 'actions']) as ModelCtor[]; let view = await View.findOne(View.parseApiJson({ filter: { @@ -141,6 +141,16 @@ export default async (ctx, next) => { // appends: ['actions', 'fields'], // }, })); + let throughName; + const {associatedName, resourceFieldName} = values; + if (associatedName) { + const table = ctx.db.getTable(associatedName); + const resourceField = table.getField(resourceFieldName); + if (resourceField instanceof BELONGSTOMANY) { + console.log({associatedName, resourceField}); + throughName = resourceField.options.through; + } + } if (!view) { // 如果不存在 view,新建一个 view = new View({type: resourceKey, template: 'FilterForm'}); @@ -158,12 +168,20 @@ export default async (ctx, next) => { [Op.not]: 'sort', }; } - const fields = await collection.getFields({ + let fields = await collection.getFields({ where, order: [ ['sort', 'asc'], ] }); + fields = fields.filter(field => { + if (field.get('interface') === 'linkTo') { + if (throughName && throughName === field.get('through')) { + return false; + } + } + return true; + }) const actions = await collection.getActions({ where: { developerMode: ctx.state.developerMode, @@ -202,6 +220,7 @@ export default async (ctx, next) => { const viewType = view.get('type'); const actionDefaultParams:any = {}; const appends = []; + for (const field of fields) { if (!['subTable', 'linkTo', 'attachment'].includes(field.get('interface'))) { continue;