mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
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:
parent
11fcc9d7ae
commit
aae75de70b
@ -344,4 +344,88 @@ describe('list-tree', () => {
|
|||||||
expect(response.status).toEqual(200);
|
expect(response.status).toEqual(200);
|
||||||
expect(response.body.rows).toMatchObject(values);
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
* 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 { Context } from '..';
|
||||||
import { getRepositoryFromParams, pageArgsToLimitArgs } from '../utils';
|
import { getRepositoryFromParams, pageArgsToLimitArgs } from '../utils';
|
||||||
import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../constants';
|
import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../constants';
|
||||||
@ -20,12 +20,16 @@ function findArgs(ctx: Context) {
|
|||||||
const resourceName = ctx.action.resourceName;
|
const resourceName = ctx.action.resourceName;
|
||||||
const params = ctx.action.params;
|
const params = ctx.action.params;
|
||||||
if (params.tree) {
|
if (params.tree) {
|
||||||
const [collectionName, associationName] = resourceName.split('.');
|
if (isValidFilter(params.filter)) {
|
||||||
const collection = ctx.db.getCollection(resourceName);
|
params.tree = false;
|
||||||
// tree collection 或者关系表是 tree collection
|
} else {
|
||||||
if (collection.options.tree && !(associationName && collectionName === collection.name)) {
|
const [collectionName, associationName] = resourceName.split('.');
|
||||||
const foreignKey = collection.treeParentField?.foreignKey || 'parentId';
|
const collection = ctx.db.getCollection(resourceName);
|
||||||
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
|
// tree collection 或者关系表是 tree collection
|
||||||
|
if (collection.options.tree && !(associationName && collectionName === collection.name)) {
|
||||||
|
const foreignKey = collection.treeParentField?.foreignKey || 'parentId';
|
||||||
|
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const { tree, fields, filter, appends, except, sort } = params;
|
const { tree, fields, filter, appends, except, sort } = params;
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
* 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 { pageArgsToLimitArgs } from './utils';
|
||||||
import { Context } from '@nocobase/actions';
|
import { Context } from '@nocobase/actions';
|
||||||
|
|
||||||
@ -20,11 +20,15 @@ function findArgs(ctx: Context) {
|
|||||||
const params = ctx.action.params;
|
const params = ctx.action.params;
|
||||||
|
|
||||||
if (params.tree) {
|
if (params.tree) {
|
||||||
const [collectionName, associationName] = resourceName.split('.');
|
if (isValidFilter(params.filter)) {
|
||||||
const collection = ctx.db.getCollection(resourceName);
|
params.tree = false;
|
||||||
if (collection.options.tree && !(associationName && collectionName === collection.name)) {
|
} else {
|
||||||
const foreignKey = collection.treeParentField?.foreignKey || 'parentId';
|
const [collectionName, associationName] = resourceName.split('.');
|
||||||
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
|
const collection = ctx.db.getCollection(resourceName);
|
||||||
|
if (collection.options.tree && !(associationName && collectionName === collection.name)) {
|
||||||
|
const foreignKey = collection.treeParentField?.foreignKey || 'parentId';
|
||||||
|
assign(params, { filter: { [foreignKey]: null } }, { filter: 'andMerge' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user