Skip to content

Commit f4aeb6c

Browse files
authored
feat(abc:st): add sort directions (#1894)
1 parent 12e30aa commit f4aeb6c

File tree

8 files changed

+42
-8
lines changed

8 files changed

+42
-8
lines changed

packages/abc/st/demo/sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class DemoComponent {
7070
},
7171
sort: true
7272
},
73-
{ title: '注册时间', type: 'date', index: 'registered' }
73+
{ title: '注册时间', type: 'date', index: 'registered', sort: 'descend' }
7474
];
7575
}
7676
```

packages/abc/st/index.en-US.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class TestComponent {
271271
| `[className]` | Class name of this column, e.g: `text-center`, `text-right`, `text-error`, pls refer to [Style Tools](/theme/tools) | `string` | - |
272272
| `[colSpan]` | Span of this column's title | `number` | - |
273273
| `[onCell]` | Set props on per cell | `(item: T, index: number) => STOnCellResult;` | - |
274-
| `[sort]` | Sort config of this column, Remote Data Configuration**Priority** Rule: <br>`true` allow sorting, should be auto generate compose `compare: (a, b) => a[index] - b[index]` method when data is local<br>`string` corresponding `key` value | `true,string,STColumnSort` | - |
274+
| `[sort]` | Sort config of this column, Remote Data Configuration**Priority** Rule: <br>`true` allow sorting, should be auto generate compose `compare: (a, b) => a[index] - b[index]` method when data is local<br>`ascend`<br>`descend`<br>`string` corresponding `key` value | `true,string,STColumnSort` | - |
275275
| `[filter]` | Filter config of this column | `STColumnFilter` | - |
276276
| `[selections]` | Config of type is checkbox | `STColumnSelection[]` | - |
277277
| `[numberDigits]` | Config of type is number | `string` | - |
@@ -310,6 +310,7 @@ class TestComponent {
310310
| `[compare]` | Sort function for local sort, see [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)'s compareFunction, `null` ingore local sort but keeping sort function. | `(a: STData, b: STData) => number, null` | - |
311311
| `[key]` | Unique key of this column, default is `index` property value<br>`multiSort: false` => `key: 'name' => ?name=1&pi=1`<br>`multiSort: true` allow multiple sort keys, or use `STMultiSort` to specify multi-column sort key merge rule | `string` | - |
312312
| `[reName]` | Map name<br>`{ ascend: '0', descend: '1' }` => `?name=1&pi=1`<br>`{ ascend: 'asc', descend: 'desc' }` => `?name=desc&pi=1` | `{ ascend?: string, descend?: string }` | - |
313+
| `[directions]` | Supported sort order, could be `'ascend'`, `'descend'`, `null` | `Array<'ascend' \| 'descend' \| null>` | `['ascend', 'descend', null]` ||
313314

314315
### STColumnFilter
315316

packages/abc/st/index.zh-CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class TestComponent {
271271
| `[className]` |`class` 属性值,例如:`text-center` 居中; `text-right` 居右; `text-error` 异常色,更多参考[样式工具类](/theme/tools) | `string` | - |
272272
| `[colSpan]` | 合并列 | `number` | - |
273273
| `[onCell]` | 设置单元格属性 | `(item: T, index: number) => STOnCellResult;` | - |
274-
| `[sort]` | 排序配置项,远程数据配置**优先**规则:<br>`true` 表示允许排序,且若数据源为本地数据时会自动生成 `compare: (a, b) => a[index] - b[index]` 方法<br>`string` 表示远程数据排序相对应 `key`| `true,string,STColumnSort` | - |
274+
| `[sort]` | 排序配置项,远程数据配置**优先**规则:<br>`true` 表示允许排序,且若数据源为本地数据时会自动生成 `compare: (a, b) => a[index] - b[index]` 方法<br>`ascend` 表示升序<br>`descend` 表示降序<br>`string` 表示远程数据排序相对应 `key`| `true,string,STColumnSort` | - |
275275
| `[filter]` | 过滤配置项 | `STColumnFilter` | - |
276276
| `[selections]` | 选择功能配置 | `STColumnSelection[]` | - |
277277
| `[numberDigits]` | 数字格式,`type=number` 有效 | `string` | - |
@@ -310,6 +310,7 @@ class TestComponent {
310310
| `[compare]` | 本地数据的排序函数,使用一个函数(参考 [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) 的 compareFunction),`null` 忽略本地排序,但保持排序功能 | `(a: any, b: any) => number, null` | - |
311311
| `[key]` | 远程数据的排序时后端相对应的KEY,默认使用 `index` 属性<br>若 `multiSort: false` 时:`key: 'name' => ?name=1&pi=1`<br>若 `multiSort: true` 允许多个排序 key 存在,或使用 `STMultiSort` 指定多列排序key合并规则 | `string` | - |
312312
| `[reName]` | 远程数据的排序时后端相对应的VALUE<br>`{ ascend: '0', descend: '1' }` 结果 `?name=1&pi=1`<br>`{ ascend: 'asc', descend: 'desc' }` 结果 `?name=desc&pi=1` | `{ ascend?: string, descend?: string }` | - |
313+
| `[directions]` | 支持的排序方式,取值为 `'ascend'`, `'descend'`, `null` | `Array<'ascend' \| 'descend' \| null>` | `['ascend', 'descend', null]` ||
313314

314315
### STColumnFilter
315316

packages/abc/st/st-column-source.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class STColumnSource {
9494
}
9595
item.type = 'none';
9696
} else {
97-
item.modal = { ...{ paramsName: 'record', size: 'lg' }, ...modal, ...item.modal };
97+
item.modal = { paramsName: 'record', size: 'lg', ...modal, ...item.modal };
9898
}
9999
}
100100

@@ -105,7 +105,7 @@ export class STColumnSource {
105105
}
106106
item.type = 'none';
107107
} else {
108-
item.drawer = { ...{ paramsName: 'record', size: 'lg' }, ...drawer, ...item.drawer };
108+
item.drawer = { paramsName: 'record', size: 'lg', ...drawer, ...item.drawer };
109109
}
110110
}
111111

@@ -178,7 +178,11 @@ export class STColumnSource {
178178
let res: STSortMap = {};
179179

180180
if (typeof item.sort === 'string') {
181-
res.key = item.sort;
181+
if (item.sort === 'ascend' || item.sort === 'descend') {
182+
res.directions = [item.sort, null];
183+
} else {
184+
res.key = item.sort;
185+
}
182186
} else if (typeof item.sort !== 'boolean') {
183187
res = item.sort;
184188
} else if (typeof item.sort === 'boolean') {
@@ -189,6 +193,10 @@ export class STColumnSource {
189193
res.key = item.indexKey;
190194
}
191195

196+
if (!Array.isArray(res.directions)) {
197+
res.directions = this.cog.sortDirections ?? ['ascend', 'descend', null];
198+
}
199+
192200
res.enabled = true;
193201

194202
return res;

packages/abc/st/st.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
[attr.data-col]="_c.indexKey"
7373
[attr.data-col-index]="index"
7474
[nzShowSort]="_c._sort.enabled"
75+
[nzSortDirections]="_c._sort.directions!"
7576
[nzSortOrder]="$any(_c)._sort.default"
7677
(nzSortOrderChange)="sort(_c, $event)"
7778
[nzCustomFilter]="!!_c.filter"

packages/abc/st/st.interfaces.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { DisabledTimeFn } from 'ng-zorro-antd/date-picker';
1414
import type { NzDrawerOptions } from 'ng-zorro-antd/drawer';
1515
import type { ModalOptions } from 'ng-zorro-antd/modal';
1616
import type { PaginationItemRenderContext } from 'ng-zorro-antd/pagination';
17-
import type { NzTablePaginationType } from 'ng-zorro-antd/table';
17+
import type { NzTablePaginationType, NzTableSortOrder } from 'ng-zorro-antd/table';
1818

1919
import type { STComponent } from './st.component';
2020

@@ -329,9 +329,11 @@ export interface STColumn<T extends STData = any> {
329329
/**
330330
* 排序配置项,远程数据配置**优先**规则:
331331
* - `true` 表示允许排序,且若数据源为本地时自动生成 `compare: (a, b) => a[index] - b[index]` 方法
332+
* - `ascend` 表示升序
333+
* - `descend` 表示降序
332334
* - `string` 表示远程数据排序相对应 `key` 值
333335
*/
334-
sort?: true | string | STColumnSort<T>;
336+
sort?: true | STColumnSort<T> | 'ascend' | 'descend' | string;
335337
/**
336338
* 过滤配置项
337339
*/
@@ -534,6 +536,12 @@ export interface STColumnSort<T extends STData = any> {
534536
* - `{ ascend: 'asc', descend: 'desc' }` 结果 `?name=desc&pi=1`
535537
*/
536538
reName?: { ascend?: string; descend?: string };
539+
/**
540+
* 支持的排序方式
541+
*
542+
* Supported sort order
543+
*/
544+
directions?: NzTableSortOrder[];
537545
}
538546

539547
export interface STSortMap<T extends STData = any> extends STColumnSort<T> {

packages/abc/st/test/st-column-source.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ describe('st: column-source', () => {
278278
expect(res.enabled).toBe(true);
279279
expect(res.key).toBe('aa');
280280
});
281+
it('when sort is ascend or descend', () => {
282+
let res = srv.process([{ title: '', index: 'aa', sort: 'ascend' }], options).columns[0]._sort!;
283+
expect(res.enabled).toBe(true);
284+
expect(res.directions?.[0]).toBe('ascend');
285+
expect(res.directions?.[1]).toBe(null);
286+
res = srv.process([{ title: '', index: 'aa', sort: 'descend' }], options).columns[0]._sort!;
287+
expect(res.enabled).toBe(true);
288+
expect(res.directions?.[0]).toBe('descend');
289+
expect(res.directions?.[1]).toBe(null);
290+
});
281291
});
282292
describe('[filter]', () => {
283293
it('should be disabled when invalid menus', () => {

packages/util/config/abc/st.type.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { NzSafeAny } from 'ng-zorro-antd/core/types';
66
import type { NzDrawerOptions } from 'ng-zorro-antd/drawer';
77
import type { ModalOptions } from 'ng-zorro-antd/modal';
88
import type { PaginationItemRenderContext } from 'ng-zorro-antd/pagination';
9+
import type { NzTableSortOrder } from 'ng-zorro-antd/table';
910

1011
export interface AlainSTConfig {
1112
/**
@@ -159,6 +160,10 @@ export interface AlainSTConfig {
159160
* 重命名排序值,`columns` 的重命名高于属性
160161
*/
161162
sortReName?: { ascend?: string; descend?: string };
163+
/**
164+
* 排序状态
165+
*/
166+
sortDirections?: NzTableSortOrder[];
162167
/**
163168
* 单排序规则
164169
* - 若不指定,则返回:`columnName=ascend|descend`

0 commit comments

Comments
 (0)