Skip to content

Commit

Permalink
feat(pagination): support pagination custom suffix unit(#INFR-12336)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinglu01 committed May 23, 2024
1 parent 8045354 commit d32fb90
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
26 changes: 23 additions & 3 deletions src/pagination/examples/total/total.component.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
<h5>Default</h5>
<thy-pagination [thySize]="'sm'" [(thyPageIndex)]="currentIndex" [thyPageSize]="10" [thyTotal]="203" [thyShowTotal]="true"></thy-pagination>
<thy-pagination
[thySize]="'sm'"
[(thyPageIndex)]="currentIndex"
[thyPageSize]="10"
[thyTotal]="203"
[thyShowTotal]="true"
[thyShowSizeChanger]="true"></thy-pagination>
<br />
<br />

<h5>Custom Template</h5>
<thy-pagination
[thySize]="'sm'"
[(thyPageIndex)]="currentIndex"
[thyPageSize]="10"
[thyTotal]="203"
thySuffixUnit=""
[thyShowTotal]="totalTemplate"
></thy-pagination>
[thyShowSizeChanger]="true"></thy-pagination>
<ng-template #totalTemplate let-range="range" let-total>
<div>
{{ range.from }}-{{ range.to }} of {{ total }} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 共<span class="text-primary"> {{ total }} </span>
{{ range.from }}-{{ range.to }} of {{ total }} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 共<span class="text-primary"> {{ total }} </span>
</div>
</ng-template>
<br />
<br />

<h5>Custom Suffix Unit</h5>
<thy-pagination
[thySize]="'sm'"
[thyPageIndex]="5"
[thyPageSize]="10"
[thyTotal]="203"
[thyShowSizeChanger]="true"
[thyShowTotal]="true"
thySuffixUnit=""></thy-pagination>
5 changes: 5 additions & 0 deletions src/pagination/pagination.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export interface ThyPaginationConfigModel {
showSizeChanger?: boolean;

pageSizeOptions?: number[];

/**
* 设置后缀单位
*/
suffixUnit?: string;
}

export interface ThyPaginationChangedEvent {
Expand Down
6 changes: 3 additions & 3 deletions src/pagination/pagination.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<ng-container *ngIf="!isTemplateRef(showTotal); else totalTemplate">
<ng-container *ngIf="total > 0">
<div class="mr-3">
<span class="number"> {{ range.from }}-{{ range.to }} </span>
<span class="number"> {{ range.from }}-{{ range.to }} </span>{{ config.suffixUnit }}
</div>
<div>
<span class="number"> {{ total }} </span>
<span class="number"> {{ total }} </span>{{ config.suffixUnit }}
</div>
</ng-container>
</ng-container>
Expand All @@ -21,7 +21,7 @@
class="page-size-option"
*ngFor="let option of config.pageSizeOptions"
[thyValue]="option"
[thyLabelText]="option + ' /页'"></thy-option>
[thyLabelText]="option + ' ' + config.suffixUnit + '/页'"></thy-option>
</thy-select>
</div>
<ul class="thy-pagination-pages">
Expand Down
11 changes: 11 additions & 0 deletions src/pagination/pagination.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ export class ThyPagination implements OnInit {
*/
@Input({ alias: 'thyHideOnSinglePage', transform: booleanAttribute }) hideOnSinglePage: boolean;

/**
* 分页器单位
* @default 条
*/
@Input('thySuffixUnit')
set suffixUnit(value: string) {
if (value) {
this.config.suffixUnit = value;
}
}

/**
* 页码改变的回调
*/
Expand Down
3 changes: 2 additions & 1 deletion src/pagination/pagination.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const PaginationDefaultConfig: ThyPaginationConfigModel = {
nextIcon: 'angle-right',
totalPagesFormat: '共{total}页',
showSizeChanger: false,
pageSizeOptions: [10, 20, 50, 100]
pageSizeOptions: [10, 20, 50, 100],
suffixUnit: '条'
};
export interface ThyPaginationConfig {
main?: ThyPaginationConfigModel;
Expand Down
22 changes: 22 additions & 0 deletions src/pagination/test/pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PaginationBasicComponent {
[thyShowQuickJumper]="canQuickJump"
(thyPageIndexChange)="pageIndexChange($event)"
[thyShowSizeChanger]="showSizeChanger"
[thySuffixUnit]="suffixUnit"
[thySize]="size"
[thyPageSizeOptions]="[10, 20, 50, 100]"
(thyPageSizeChanged)="pageSizeChanged($event)"></thy-pagination>
Expand All @@ -68,6 +69,8 @@ class PaginationTestComponent {

size = '';

suffixUnit = '';

pageIndexChange = jasmine.createSpy('pageIndexChange callback');

pageSizeChanged = jasmine.createSpy('pageSizeChanged callback');
Expand Down Expand Up @@ -181,6 +184,25 @@ describe('ThyPagination', () => {
expect(pagination.pageIndex).toEqual(3);
expect(inputElement.value).toEqual('');
}));

it('should suffixUnit can works', fakeAsync(() => {
componentInstance.showSizeChanger = true;
componentInstance.pagination.pageSize = 50;
fixture.detectChanges();
expect(paginationElement.querySelectorAll('.thy-pagination-size').length).toEqual(1);

(paginationElement.querySelectorAll('.form-control-custom')[0] as HTMLElement).click();
fixture.detectChanges();
const el = document.querySelector('.thy-select-dropdown-options') as HTMLElement;
expect(el.querySelectorAll('.thy-option-item')[0]?.querySelectorAll('.text-truncate')[0]?.innerHTML).toEqual('10 条/页');

componentInstance.suffixUnit = '组';
fixture.detectChanges();
(paginationElement.querySelectorAll('.form-control-custom')[0] as HTMLElement).click();
fixture.detectChanges();
const option = document.querySelector('.thy-select-dropdown-options') as HTMLElement;
expect(option.querySelectorAll('.thy-option-item')[0]?.querySelectorAll('.text-truncate')[0]?.innerHTML).toEqual('10 组/页');
}));
});

describe('total', () => {
Expand Down

0 comments on commit d32fb90

Please sign in to comment.