feat(tree-block): support filtering child nodes (#4603)

* feat(tree-block): support filtering child nodes

* test: add list test

* test: remove only

* fix: use isValidFilter
This commit is contained in:
YANG QIA 2024-06-11 11:34:11 +08:00 committed by GitHub
parent 11fcc9d7ae
commit aae75de70b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 105 additions and 13 deletions

View File

@ -344,4 +344,88 @@ describe('list-tree', () => {
expect(response.status).toEqual(200);
expect(response.body.rows).toMatchObject(values);
});
it('should filter child nodes for tree', async () => {
const values = [
{
name: 'A1',
__index: '0',
children3: [
{
name: 'B',
__index: '0.children3.0',
children3: [
{
name: 'C',
__index: '0.children3.0.children3.0',
},
],
},
],
},
{
name: 'A2',
__index: '1',
children3: [
{
name: 'B',
__index: '1.children3.0',
},
],
},
];
const db = app.db;
db.collection({
name: 'categories',
tree: 'adjacency-list',
fields: [
{
type: 'string',
name: 'name',
},
{
type: 'string',
name: 'description',
},
{
type: 'belongsTo',
name: 'parent',
foreignKey: 'cid',
treeParent: true,
},
{
type: 'hasMany',
name: 'children3',
foreignKey: 'cid',
treeChildren: true,
},
],
});
await db.sync();
await db.getRepository('categories').create({
values,
});
const response = await app
.agent()
.resource('categories')
.list({
tree: true,
fields: ['id', 'name'],
appends: ['parent'],
filter: {
name: 'B',
},
});
expect(response.status).toEqual(200);
const rows = response.body.rows;
expect(rows.length).toEqual(2);
expect(rows[0].name).toEqual('B');
expect(rows[1].name).toEqual('B');
expect(rows[0].parent.name).toEqual('A1');
expect(rows[1].parent.name).toEqual('A2');
});
});

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { assign } from '@nocobase/utils';
import { assign, isValidFilter } from '@nocobase/utils';
import { Context } from '..';
import { getRepositoryFromParams, pageArgsToLimitArgs } from '../utils';
import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../constants';
@ -20,6 +20,9 @@ function findArgs(ctx: Context) {
const resourceName = ctx.action.resourceName;
const params = ctx.action.params;
if (params.tree) {
if (isValidFilter(params.filter)) {
params.tree = false;
} else {
const [collectionName, associationName] = resourceName.split('.');
const collection = ctx.db.getCollection(resourceName);
// tree collection 或者关系表是 tree collection
@ -28,6 +31,7 @@ function findArgs(ctx: Context) {
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
}
}
}
const { tree, fields, filter, appends, except, sort } = params;
return { tree, filter, fields, appends, except, sort };

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { assign } from '@nocobase/utils';
import { assign, isValidFilter } from '@nocobase/utils';
import { pageArgsToLimitArgs } from './utils';
import { Context } from '@nocobase/actions';
@ -20,6 +20,9 @@ function findArgs(ctx: Context) {
const params = ctx.action.params;
if (params.tree) {
if (isValidFilter(params.filter)) {
params.tree = false;
} else {
const [collectionName, associationName] = resourceName.split('.');
const collection = ctx.db.getCollection(resourceName);
if (collection.options.tree && !(associationName && collectionName === collection.name)) {
@ -27,6 +30,7 @@ function findArgs(ctx: Context) {
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
}
}
}
const { tree, fields, filter, appends, except, sort } = params;