fix: the sub-table records can not be modified and deleted

This commit is contained in:
chenos 2022-04-13 22:50:02 +08:00
parent 5308210991
commit 1326e024ab
3 changed files with 49 additions and 6 deletions

View File

@ -44,8 +44,8 @@ export class TableFieldResource {
async list(options) { async list(options) {
this.field.data = this.field.data || {}; this.field.data = this.field.data || {};
if (this.field?.data?.dataSource?.length) { if (this.field.data.changed) {
console.log('list', this.field.data.dataSource); console.log('list.dataSource', this.field.data.dataSource);
return { return {
data: { data: {
data: this.field.data.dataSource, data: this.field.data.dataSource,
@ -85,12 +85,14 @@ export class TableFieldResource {
console.log('create', options); console.log('create', options);
const { values } = options; const { values } = options;
this.field.data.dataSource.push(values); this.field.data.dataSource.push(values);
this.field.data.changed = true;
} }
async update(options) { async update(options) {
console.log('update', options); console.log('update', options);
const { filterByTk, values } = options; const { filterByTk, values } = options;
this.field.data.dataSource[filterByTk] = values; this.field.data.dataSource[filterByTk] = values;
this.field.data.changed = true;
} }
async destroy(options) { async destroy(options) {
@ -102,6 +104,7 @@ export class TableFieldResource {
this.field.data.dataSource = this.field.data.dataSource.filter((item, index) => { this.field.data.dataSource = this.field.data.dataSource.filter((item, index) => {
return !filterByTk.includes(index); return !filterByTk.includes(index);
}); });
this.field.data.changed = true;
} }
} }

View File

@ -5,6 +5,7 @@ import { useHistory } from 'react-router-dom';
import { useActionContext } from '../../schema-component'; import { useActionContext } from '../../schema-component';
import { useBlockRequestContext, useFilterByTk } from '../BlockProvider'; import { useBlockRequestContext, useFilterByTk } from '../BlockProvider';
import { useFormBlockContext } from '../FormBlockProvider'; import { useFormBlockContext } from '../FormBlockProvider';
import { TableFieldResource } from '../TableFieldProvider';
export const usePickActionProps = () => { export const usePickActionProps = () => {
const form = useForm(); const form = useForm();
@ -75,13 +76,14 @@ export const useUpdateActionProps = () => {
return { return {
async onClick() { async onClick() {
await form.submit(); await form.submit();
await resource.update({ await resource.update({
filterByTk, filterByTk,
values: form.values, values: form.values,
}); });
__parent?.service?.refresh?.(); __parent?.service?.refresh?.();
if (!(resource instanceof TableFieldResource)) {
__parent?.__parent?.service?.refresh?.(); __parent?.__parent?.service?.refresh?.();
}
setVisible?.(false); setVisible?.(false);
}, },
}; };
@ -111,6 +113,7 @@ export const useBulkDestroyActionProps = () => {
await resource.destroy({ await resource.destroy({
filterByTk: field.data?.selectedRowKeys, filterByTk: field.data?.selectedRowKeys,
}); });
field.data.selectedRowKeys = [];
service?.refresh?.(); service?.refresh?.();
}, },
}; };

View File

@ -1,14 +1,15 @@
import { Collection } from '@nocobase/database';
import { Plugin } from '@nocobase/server'; import { Plugin } from '@nocobase/server';
import lodash from 'lodash';
import path from 'path'; import path from 'path';
import { CollectionRepository } from '.'; import { CollectionRepository } from '.';
import { import {
afterCreateForReverseField, afterCreateForReverseField,
beforeCreateForChildrenCollection, beforeCreateForChildrenCollection,
beforeCreateForReverseField, beforeCreateForReverseField,
beforeInitOptions, beforeInitOptions
} from './hooks'; } from './hooks';
import { CollectionModel, FieldModel } from './models'; import { CollectionModel, FieldModel } from './models';
import lodash from 'lodash';
export class CollectionManagerPlugin extends Plugin { export class CollectionManagerPlugin extends Plugin {
async beforeLoad() { async beforeLoad() {
@ -78,6 +79,42 @@ export class CollectionManagerPlugin extends Plugin {
await next(); await next();
}); });
this.app.resourcer.use(async (ctx, next) => {
const { resourceName, actionName } = ctx.action;
if (actionName === 'update') {
const { updateAssociationValues = [] } = ctx.action.params;
const [collectionName, associationName] = resourceName.split('.');
if (!associationName) {
const collection: Collection = ctx.db.getCollection(collectionName);
if (collection) {
for (const [, field] of collection.fields) {
if (field.options.interface === 'subTable') {
updateAssociationValues.push(field.name);
}
}
}
} else {
const association = ctx.db.getCollection(collectionName)?.getField?.(associationName);
if (association?.target) {
const collection: Collection = ctx.db.getCollection(association?.target);
if (collection) {
for (const [, field] of collection.fields) {
if (field.options.interface === 'subTable') {
updateAssociationValues.push(field.name);
}
}
}
}
}
if (updateAssociationValues.length) {
ctx.action.mergeParams({
updateAssociationValues,
});
}
}
await next();
});
this.app.acl.skip('collections', 'list', 'logged-in'); this.app.acl.skip('collections', 'list', 'logged-in');
} }