Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module:st): add single sort config #283

Merged
merged 1 commit into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/abc/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Property | Description | Type | Default
`[loading]` | Loading status of table | `boolean` | `false`
`[loadingDelay]` | Specifies a delay in milliseconds for loading state (prevent flush) | `number` | `0`
`[scroll]` | Whether table can be scrolled in x/y direction, x or y can be a string that indicates the width and height of table body | `{ y?: string; x?: string }` | -
`[singleSort]` | Single sort config<br>If not specified, return: `columnName=ascend|descend`<br>If specified, return: `sort=columnName.(ascend|descend)` | `STSingleSort` | `null`
`[multiSort]` | Whether to mulit-sort, recommended use in URL data source | `boolean, STMultiSort` | `false`
`[rowClickTime]` | Click twice in the time range for double click, unit is millisecond | `number` | `200`
`[header]` | Table header renderer | `string,TemplateRef<void>` | -
Expand Down Expand Up @@ -175,6 +176,13 @@ Property | Description | Type | Default
`[filename]` | Save file name | `string` | `export.xslx`
`[callback]` | Callback before saving | `(wb: WorkBook) => void` | -

### STSingleSort

Property | Description | Type | Default
-------- | ----------- | ---- | -------
`[key]` | Request parameter name | `string` | `sort`
`[nameSeparator]` | Column name and state separator | `string` | `.`

### STMultiSort

Property | Description | Type | Default
Expand Down
8 changes: 8 additions & 0 deletions packages/abc/table/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ config: STConfig
`[loading]` | 页面是否加载中 | `boolean` | `false`
`[loadingDelay]` | 延迟显示加载效果的时间(防止闪烁) | `number` | `0`
`[scroll]` | 横向或纵向支持滚动,也可用于指定滚动区域的宽高度:`{ x: "300px", y: "300px" }` | `{ y?: string; x?: string }` | -
`[singleSort]` | 单排序规则<br>若不指定,则返回:`columnName=ascend|descend`<br>若指定,则返回:`sort=columnName.(ascend|descend)` | `STSingleSort` | `null`
`[multiSort]` | 是否多排序,当 `sort` 多个相同值时自动合并,建议后端支持时使用 | `boolean, STMultiSort` | `false`
`[rowClickTime]` | 行单击多少时长之类为双击(单位:毫秒) | `number` | `200`
`[header]` | 表格标题 | `string,TemplateRef<void>` | -
Expand Down Expand Up @@ -177,6 +178,13 @@ class TestComponent {

### STMultiSort

参数 | 说明 | 类型 | 默认值
----|------|-----|------
`[key]` | 请求参数名 | `string` | `sort`
`[nameSeparator]` | 列名与状态间分隔符 | `string` | `.`

### STMultiSort

参数 | 说明 | 类型 | 默认值
----|------|-----|------
`[key]` | 请求参数名 | `string` | `sort`
Expand Down
16 changes: 12 additions & 4 deletions packages/abc/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
STColumn,
STMultiSort,
STRowClassName,
STSingleSort,
} from './table.interfaces';
import { STSortMap } from './table-column-source';

Expand All @@ -27,6 +28,7 @@ export interface STDataSourceOptions {
res?: STRes;
page?: STPage;
columns?: STColumn[];
singleSort?: STSingleSort;
multiSort?: STMultiSort;
rowClassName?: STRowClassName;
}
Expand Down Expand Up @@ -202,15 +204,15 @@ export class STDataSource {
url: string,
options: STDataSourceOptions,
): Observable<any> {
const { req, page, pi, ps, multiSort, columns } = options;
const { req, page, pi, ps, singleSort, multiSort, columns } = options;
const method = (req.method || 'GET').toUpperCase();
const params: any = Object.assign(
{
[req.reName.pi]: page.zeroIndexed ? pi - 1 : pi,
[req.reName.ps]: ps,
},
req.params,
this.getReqSortMap(multiSort, columns),
this.getReqSortMap(singleSort, multiSort, columns),
this.getReqFilterMap(columns),
);
let reqOptions: any = {
Expand Down Expand Up @@ -255,6 +257,7 @@ export class STDataSource {
}

getReqSortMap(
singleSort: STSingleSort,
multiSort: STMultiSort,
columns: STColumn[],
): { [key: string]: string } {
Expand All @@ -274,8 +277,13 @@ export class STDataSource {
};
} else {
const mapData = sortList[0];
ret[mapData.key] =
(sortList[0].reName || {})[mapData.default] || mapData.default;
let sortFiled = mapData.key;
let sortValue = (sortList[0].reName || {})[mapData.default] || mapData.default;
if (singleSort) {
sortValue = sortFiled + (singleSort.nameSeparator || '.') + sortValue;
sortFiled = singleSort.key || 'sort';
}
ret[sortFiled] = sortValue;
}
return ret;
}
Expand Down
17 changes: 12 additions & 5 deletions packages/abc/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
ALAIN_I18N_TOKEN,
AlainI18NService,
DrawerHelper,
DrawerHelperOptions,
DelonLocaleService,
} from '@delon/theme';
import {
Expand All @@ -52,11 +51,11 @@ import {
STReq,
STError,
STChangeType,
STChangeRowClick,
STRes,
STPage,
STLoadOptions,
STRowClassName,
STSingleSort,
} from './table.interfaces';
import { STConfig } from './table.config';
import { STExport } from './table-export';
Expand Down Expand Up @@ -180,6 +179,14 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
/** 纵向支持滚动,也可用于指定滚动区域的高度:`{ y: '300px', x: '300px' }` */
@Input()
scroll: { y?: string; x?: string };
/**
* 单排序规则
* - 若不指定,则返回:`columnName=ascend|descend`
* - 若指定,则返回:`sort=columnName.(ascend|descend)`
*/
@Input()
singleSort: STSingleSort = null;
private _multiSort: STMultiSort;
/** 是否多排序,当 `sort` 多个相同值时自动合并,建议后端支持时使用 */
@Input()
get multiSort() {
Expand All @@ -199,7 +206,6 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
typeof value === 'object' ? value : {},
);
}
private _multiSort: STMultiSort;
@Input()
rowClassName: STRowClassName;
/** `header` 标题 */
Expand Down Expand Up @@ -298,7 +304,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
//#region data

private _load() {
const { pi, ps, data, req, res, page, total, multiSort, rowClassName } = this;
const { pi, ps, data, req, res, page, total, singleSort, multiSort, rowClassName } = this;
this.loading = true;
return this.dataSource
.process({
Expand All @@ -310,6 +316,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
res,
page,
columns: this._columns,
singleSort,
multiSort,
rowClassName
})
Expand Down Expand Up @@ -463,7 +470,7 @@ export class STComponent implements AfterViewInit, OnChanges, OnDestroy {
this._load();
const res = {
value,
map: this.dataSource.getReqSortMap(this.multiSort, this._columns),
map: this.dataSource.getReqSortMap(this.singleSort, this.multiSort, this._columns),
column: col,
};
this.changeEmit('sort', res);
Expand Down
7 changes: 7 additions & 0 deletions packages/abc/table/table.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
STColumnButtonDrawerConfig,
STIcon,
STRowClassName,
STSingleSort,
} from './table.interfaces';

export class STConfig {
Expand Down Expand Up @@ -58,6 +59,12 @@ export class STConfig {
* 重命名排序值,`columns` 的重命名高于属性
*/
sortReName?: { ascend?: string; descend?: string };
/**
* 单排序规则
* - 若不指定,则返回:`columnName=ascend|descend`
* - 若指定,则返回:`sort=columnName.(ascend|descend)`
*/
singleSort?: STSingleSort = null;
/**
* 是否多排序,当 `sort` 多个相同值时自动合并,建议后端支持时使用
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/abc/table/table.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,18 @@ export interface STExportOptions {
callback?: (wb: any) => void;
}

/**
* 单排序规则
* - 若不指定,则返回:`columnName=ascend|descend`
* - 若指定,则返回:`sort=columnName.(ascend|descend)`
*/
export interface STSingleSort {
/** 请求参数名,默认:`sort` */
key?: string;
/** 列名与状态间分隔符,默认:`.` */
nameSeparator?: string;
}

/**
* 多排序相同排序 key 时合并规则
*/
Expand Down
34 changes: 30 additions & 4 deletions packages/abc/table/test/table-data-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ describe('abc: table: data-souce', () => {
});

describe('[remote data]', () => {
beforeEach(() => {
genModule();
options.data = '/mockurl';
});
describe('[request params]', () => {
beforeEach(() => {
genModule();
options.data = '/mockurl';
});
it('should be default method to GET', (done: () => void) => {
options.req.method = undefined;
let resMethod = '';
Expand Down Expand Up @@ -296,6 +296,10 @@ describe('abc: table: data-souce', () => {
});
});
describe('[response]', () => {
beforeEach(() => {
genModule();
options.data = '/mockurl';
});
it('should be re-name total & list', (done: () => void) => {
options.res.reName = { total: 'T', list: 'L' };
spyOn(http, 'request').and.callFake(
Expand Down Expand Up @@ -384,6 +388,8 @@ describe('abc: table: data-souce', () => {
describe('[sort]', () => {
let resParams: any;
beforeEach(() => {
genModule();
options.data = '/mockurl';
options.columns[0]._sort = {
enabled: true,
key: 'id',
Expand Down Expand Up @@ -466,10 +472,30 @@ describe('abc: table: data-souce', () => {
});
});
});
describe('[singleSort]', () => {
it(`should working`, (done: () => void) => {
options.columns[0]._sort.default = 'ascend';
options.singleSort = {};
srv.process(options).then(res => {
expect(resParams.sort).toBe('id.ascend');
done();
});
});
it(`should specify options`, (done: () => void) => {
options.columns[0]._sort.default = 'ascend';
options.singleSort = { key: 'SORT', nameSeparator: '-' };
srv.process(options).then(res => {
expect(resParams.SORT).toBe('id-ascend');
done();
});
});
});
});
describe('[filter]', () => {
let resParams: any;
beforeEach(() => {
genModule();
options.data = '/mockurl';
options.columns[0].filter = {
default: true,
key: 'id',
Expand Down