diff --git a/packages/core/client/src/block-provider/TableSelectorProvider.tsx b/packages/core/client/src/block-provider/TableSelectorProvider.tsx index 36a1b98cd..7be73ac31 100644 --- a/packages/core/client/src/block-provider/TableSelectorProvider.tsx +++ b/packages/core/client/src/block-provider/TableSelectorProvider.tsx @@ -9,7 +9,7 @@ import { useFormBlockContext } from './FormBlockProvider'; export const TableSelectorContext = createContext({}); const InternalTableSelectorProvider = (props) => { - const { params, rowKey } = props; + const { params, rowKey, extraFilter } = props; const field = useField(); const { resource, service } = useBlockRequestContext(); // if (service.loading) { @@ -22,6 +22,7 @@ const InternalTableSelectorProvider = (props) => { service, resource, params, + extraFilter, rowKey, }} > @@ -39,21 +40,23 @@ const useAssociationNames = (collection) => { }; const recursiveParent = (schema: Schema, component) => { - return schema['x-component'] === component - ? schema - : (schema.parent ? recursiveParent(schema.parent, component) : null); -} + return schema['x-component'] === component + ? schema + : schema.parent + ? recursiveParent(schema.parent, component) + : null; +}; export const TableSelectorProvider = (props) => { const fieldSchema = useFieldSchema(); - const ctx = useFormBlockContext() + const ctx = useFormBlockContext(); const { getCollectionJoinField, getCollectionFields } = useCollectionManager(); const record = useRecord(); const collectionFieldSchema = recursiveParent(fieldSchema, 'CollectionField'); // const value = ctx.form.query(collectionFieldSchema?.name).value(); const collectionField = getCollectionJoinField(collectionFieldSchema?.['x-collection-field']); - + console.log('TableSelectorProvider', collectionFieldSchema, collectionField, record); const params = { ...props.params }; const appends = useAssociationNames(props.collection); @@ -63,57 +66,74 @@ export const TableSelectorProvider = (props) => { if (!Object.keys(params).includes('appends')) { params['appends'] = appends; } + let extraFilter; if (collectionField) { if (['oho', 'o2m'].includes(collectionField.interface)) { if (record?.[collectionField.sourceKey]) { - params['filter'] = { - $or: [{ - [collectionField.foreignKey]: { - $is: null, - } - }, { - [collectionField.foreignKey]: { - $eq: record?.[collectionField.sourceKey], - } - }] - } + extraFilter = { + $or: [ + { + [collectionField.foreignKey]: { + $is: null, + }, + }, + { + [collectionField.foreignKey]: { + $eq: record?.[collectionField.sourceKey], + }, + }, + ], + }; } else { - params['filter'] = { + extraFilter = { [collectionField.foreignKey]: { $is: null, - } - } + }, + }; } } if (['obo'].includes(collectionField.interface)) { const fields = getCollectionFields(collectionField.target); - const targetField = fields.find(f => f.foreignKey && f.foreignKey === collectionField.foreignKey); + const targetField = fields.find((f) => f.foreignKey && f.foreignKey === collectionField.foreignKey); if (targetField) { if (record?.[collectionField.foreignKey]) { - params['filter'] = { - $or: [{ - [`${targetField.name}.${targetField.foreignKey}`]: { - $is: null, - } - }, { - [`${targetField.name}.${targetField.foreignKey}`]: { - $eq: record?.[collectionField.foreignKey], - } - }] - } + extraFilter = { + $or: [ + { + [`${targetField.name}.${targetField.foreignKey}`]: { + $is: null, + }, + }, + { + [`${targetField.name}.${targetField.foreignKey}`]: { + $eq: record?.[collectionField.foreignKey], + }, + }, + ], + }; } else { - params['filter'] = { + extraFilter = { [`${targetField.name}.${targetField.foreignKey}`]: { $is: null, - } - } + }, + }; } } } } + + if (extraFilter) { + if (params?.filter) { + params['filter'] = { + $and: [extraFilter, params['filter']], + }; + } else { + params['filter'] = extraFilter; + } + } return ( - + ); }; diff --git a/packages/core/client/src/schema-component/antd/table-v2/TableSelectorDesigner.tsx b/packages/core/client/src/schema-component/antd/table-v2/TableSelectorDesigner.tsx index a304f7233..eead66c1f 100644 --- a/packages/core/client/src/schema-component/antd/table-v2/TableSelectorDesigner.tsx +++ b/packages/core/client/src/schema-component/antd/table-v2/TableSelectorDesigner.tsx @@ -8,6 +8,7 @@ import { useCollectionFilterOptions, useSortFields } from '../../../collection-m import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { useSchemaTemplate } from '../../../schema-templates'; import { useDesignable } from '../../hooks'; +import { cloneDeep } from 'lodash'; export const TableSelectorDesigner = () => { const { name, title } = useCollection(); @@ -15,7 +16,7 @@ export const TableSelectorDesigner = () => { const fieldSchema = useFieldSchema(); const dataSource = useCollectionFilterOptions(name); const sortFields = useSortFields(name); - const { service } = useTableSelectorContext(); + const { service, extraFilter } = useTableSelectorContext(); const { t } = useTranslation(); const { dn } = useDesignable(); const defaultFilter = fieldSchema?.['x-decorator-props']?.params?.filter || {}; @@ -57,7 +58,17 @@ export const TableSelectorDesigner = () => { params.filter = filter; field.decoratorProps.params = params; fieldSchema['x-decorator-props']['params'] = params; - service.run({ ...service.params?.[0], filter, page: 1 }); + let serviceFilter = cloneDeep(filter); + if (extraFilter) { + if (serviceFilter) { + serviceFilter = { + $and: [extraFilter, serviceFilter], + }; + } else { + serviceFilter = extraFilter; + } + } + service.run({ ...service.params?.[0], filter: serviceFilter, page: 1 }); dn.emit('patch', { schema: { ['x-uid']: fieldSchema['x-uid'],