refactor: set sorting rule

This commit is contained in:
katherinehhh 2025-07-01 15:45:45 +08:00
parent 40ee889135
commit 1ef49c8717
4 changed files with 118 additions and 1 deletions

View File

@ -49,6 +49,6 @@ export const dataScope = defineAction({
return; return;
} }
resource.addFilterGroup(ctx.model.uid, params.filter); resource.addFilterGroup(ctx.model.uid, params.filter);
resource.refresh(); // resource.refresh();
}, },
}); });

View File

@ -12,4 +12,5 @@ export * from './dataScope';
export * from './openView'; export * from './openView';
export * from './titleField'; export * from './titleField';
export * from './dateTimeFormat'; export * from './dateTimeFormat';
export * from './sortingRules';
// //

View File

@ -0,0 +1,112 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { defineAction, MultiRecordResource, useStepSettingContext } from '@nocobase/flow-engine';
import React from 'react';
import { Select } from 'antd';
import { tval } from '@nocobase/utils/client';
import { useCompile } from '../../schema-component';
import { useSortFields } from '../../';
const SelectOptions = (props) => {
const {
model: { resource },
app,
} = useStepSettingContext();
const compile = useCompile();
const sortFields = useSortFields(resource?.resourceName);
console.log(sortFields);
return <Select {...props} options={sortFields} />;
};
const isArrayEqualIgnoreOrder = (a: string[], b: string[]) =>
a.length === b.length && [...a].sort().every((v, i) => v === [...b].sort()[i]);
export const sortingRule = defineAction({
name: 'sortingRule',
title: tval('Set default sorting rules'),
uiSchema: {
sort: {
type: 'array',
'x-component': 'ArrayItems',
'x-decorator': 'FormItem',
items: {
type: 'object',
properties: {
space: {
type: 'void',
'x-component': 'Space',
properties: {
sort: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.SortHandle',
},
field: {
type: 'string',
required: true,
'x-decorator': 'FormItem',
'x-component': SelectOptions,
'x-component-props': {
style: {
width: 260,
},
},
},
direction: {
type: 'string',
'x-decorator': 'FormItem',
'x-component': 'Radio.Group',
'x-component-props': {
optionType: 'button',
},
enum: [
{
label: tval('ASC'),
value: 'asc',
},
{
label: tval('DESC'),
value: 'desc',
},
],
},
remove: {
type: 'void',
'x-decorator': 'FormItem',
'x-component': 'ArrayItems.Remove',
},
},
},
},
},
properties: {
add: {
type: 'void',
title: tval('Add sort field'),
'x-component': 'ArrayItems.Addition',
},
},
},
},
defaultParams(ctx) {
return {
sort: [],
};
},
async handler(ctx, params) {
const sortArr = params.sort.map((item) => {
return item.direction === 'desc' ? `-${item.field}` : item.field;
});
// @ts-ignore
const resource = ctx.model?.resource as MultiRecordResource;
if (!resource) {
return;
}
resource.setSort(sortArr);
},
});

View File

@ -502,6 +502,10 @@ TableModel.registerFlow({
use: 'dataScope', use: 'dataScope',
title: tval('Set data scope'), title: tval('Set data scope'),
}, },
sortingRule: {
use: 'sortingRule',
title: tval('Set default sorting rules'),
},
enabledIndexColumn: { enabledIndexColumn: {
title: tval('Enable index column'), title: tval('Enable index column'),
uiSchema: { uiSchema: {