Skip to content

Commit

Permalink
refactor(module:pagination): refactor pagination to support OnPush (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vthinkxie authored Jan 17, 2019
1 parent 852ad73 commit 99bfeb1
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 303 deletions.
14 changes: 10 additions & 4 deletions components/core/util/convert.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { coerceBooleanProperty, coerceCssPixelValue, coerceNumberProperty } from '@angular/cdk/coercion';
import { coerceBooleanProperty, coerceCssPixelValue, _isNumberValue } from '@angular/cdk/coercion';
import { FunctionProp } from '../types/common-wrap';

export function toBoolean(value: boolean | string): boolean {
return coerceBooleanProperty(value);
}

export function toNumber<D>(value: number | string, fallback: D): number | D {
return coerceNumberProperty(value, fallback);
export function toNumber(value: number | string): number;
export function toNumber<D>(value: number | string, fallback: D): number | D;
export function toNumber(value: number | string, fallbackValue: number = 0): number {
return _isNumberValue(value) ? Number(value) : fallbackValue;
}

export function toCssPixel(value: number | string): string {
Expand Down Expand Up @@ -70,4 +72,8 @@ export function InputBoolean(): any { // tslint:disable-line: no-any

export function InputCssPixel(): any { // tslint:disable-line: no-any
return propDecoratorFactory('InputCssPixel', toCssPixel);
}
}

export function InputNumber(): any { // tslint:disable-line: no-any
return propDecoratorFactory('InputNumber', toNumber);
}
2 changes: 1 addition & 1 deletion components/pagination/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A long list can be divided into several pages by `Pagination`, and only one page
| `[nzShowQuickJumper]` | determine whether you can jump to pages directly | `boolean` | `false` |
| `[nzShowSizeChanger]` | determine whether `nzPageSize` can be changed | `boolean` | `false` |
| `[nzSimple]` | whether to use simple mode | `boolean` | - |
| `[nzSize]` | specify the size of `nz-pagination`, can be set to `small` | `'small'` | `""` |
| `[nzSize]` | specify the size of `nz-pagination`, can be set to `small` | `'small'` | `'default'` |
| `[nzPageSizeOptions]` | specify the sizeChanger options | `number[]` | `[10, 20, 30, 40]` |
| `[nzItemRender]` | to customize item | `TemplateRef<{ $implicit: 'page'|'prev'|'next', page: number }>` | - |
| `[nzShowTotal]` | to display the total number and range | `TemplateRef<{ $implicit: number, range: [ number, number ] }>` | - |
Expand Down
2 changes: 1 addition & 1 deletion components/pagination/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cols: 1
| `[nzShowQuickJumper]` | 是否可以快速跳转至某页 | `boolean` | `false` |
| `[nzShowSizeChanger]` | 是否可以改变 `nzPageSize` | `boolean` | `false` |
| `[nzSimple]` | 当添加该属性时,显示为简单分页 | `boolean` | - |
| `[nzSize]` | 当为「small」时,是小尺寸分页 | `'small'` | `""` |
| `[nzSize]` | 当为「small」时,是小尺寸分页 | `'small'` | `'default'` |
| `[nzPageSizeOptions]` | 指定每页可以显示多少条 | `number[]` | `[10, 20, 30, 40]` |
| `[nzItemRender]` | 用于自定义页码的结构 | `TemplateRef<{ $implicit: 'page'|'prev'|'next', page: number }>` | - |
| `[nzShowTotal]` | 用于显示数据总量和当前数据范围,具体使用方式见代码演示部分 | `TemplateRef<{ $implicit: number, range: [ number, number ] }>` | - |
Expand Down
226 changes: 102 additions & 124 deletions components/pagination/nz-pagination.component.html
Original file line number Diff line number Diff line change
@@ -1,133 +1,111 @@
<ng-template #renderItemTemplate let-type let-page="page">
<a class="ant-pagination-item-link" *ngIf="type==='pre'"><i nz-icon type="left"></i></a>
<a class="ant-pagination-item-link" *ngIf="type==='next'"><i nz-icon type="right"></i></a>
<a *ngIf="type=='page'">{{page}}</a>
<a *ngIf="type=='page'">{{ page }}</a>
</ng-template>
<ng-container *ngIf="(nzHideOnSinglePage&&(nzTotal>nzPageSize))||!nzHideOnSinglePage">
<ul
*ngIf="nzSimple"
<ng-container *ngIf="nzHideOnSinglePage && (nzTotal > nzPageSize) || !nzHideOnSinglePage">
<ul class="ant-pagination"
[class.ant-table-pagination]="nzInTable"
class="ant-pagination ant-pagination-simple">
<li
title="{{ locale.prev_page }}"
class="ant-pagination-prev"
(click)="jumpPreOne()"
[class.ant-pagination-disabled]="isFirstIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'pre'}"></ng-template>
</li>
<li [attr.title]="nzPageIndex+'/'+lastIndex" class="ant-pagination-simple-pager">
<input
#simplePagerInput
[ngModel]="nzPageIndex"
(keydown.enter)="handleKeyDown($event,simplePagerInput,false)"
size="3">
<span class="ant-pagination-slash"></span>
{{ lastIndex }}
</li>
<li
title="{{ locale.next_page }}"
class="ant-pagination-next"
(click)="jumpNextOne()"
[class.ant-pagination-disabled]="isLastIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'next'}"></ng-template>
</li>
</ul>
<ul
*ngIf="!nzSimple"
[class.mini]="nzSize=='small'"
[class.ant-table-pagination]="nzInTable"
class="ant-pagination">
<li class="ant-pagination-total-text" *ngIf="nzShowTotal">
<ng-template
[ngTemplateOutlet]="nzShowTotal"
[ngTemplateOutletContext]="{ $implicit: nzTotal,range:[(nzPageIndex-1)*nzPageSize+1, min(nzPageIndex*nzPageSize, nzTotal)] }">
</ng-template>
</li>
<li
title="{{ locale.prev_page }}"
class="ant-pagination-prev"
(click)="jumpPreOne()"
[class.ant-pagination-disabled]="isFirstIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'pre'}"></ng-template>
</li>
<li
[attr.title]="firstIndex"
class="ant-pagination-item"
(click)="jumpPage(firstIndex)"
[class.ant-pagination-item-active]="isFirstIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: firstIndex }"></ng-template>
</li>
<li
[attr.title]="locale.prev_5"
(click)="jumpPreFive()"
class="ant-pagination-jump-prev"
*ngIf="(lastIndex >9)&&(nzPageIndex-3>firstIndex)">
<a class="ant-pagination-item-link">
<div class="ant-pagination-item-container">
<i nz-icon type="double-left" class="ant-pagination-item-link-icon"></i>
<span class="ant-pagination-item-ellipsis">•••</span>
</div>
</a>
</li>
<li
*ngFor="let page of pages"
[attr.title]="page.index"
class="ant-pagination-item"
(click)="jumpPage(page.index)"
[class.ant-pagination-item-active]="nzPageIndex==page.index">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: page.index }"></ng-template>
</li>
<li
[attr.title]="locale.next_5"
(click)="jumpNextFive()"
class="ant-pagination-jump-next ant-pagination-item-link-icon"
*ngIf="(lastIndex >9)&&(nzPageIndex+3<lastIndex)">
<a class="ant-pagination-item-link">
<div class="ant-pagination-item-container">
<i nz-icon type="double-right" class="ant-pagination-item-link-icon"></i>
<span class="ant-pagination-item-ellipsis">•••</span>
[class.ant-pagination-simple]="nzSimple"
[class.mini]="(nzSize === 'small') && !nzSimple">
<ng-container *ngIf="nzSimple; else normalTemplate">
<li class="ant-pagination-prev"
[attr.title]="locale.prev_page"
[class.ant-pagination-disabled]="isFirstIndex"
(click)="jumpDiff(-1)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'pre'}"></ng-template>
</li>
<li [attr.title]="nzPageIndex+'/'+lastIndex" class="ant-pagination-simple-pager">
<input #simplePagerInput [value]="nzPageIndex" (keydown.enter)="handleKeyDown($event,simplePagerInput,false)" size="3">
<span class="ant-pagination-slash"></span>
{{ lastIndex }}
</li>
<li class="ant-pagination-next"
[attr.title]="locale.next_page"
[class.ant-pagination-disabled]="isLastIndex"
(click)="jumpDiff(1)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'next'}"></ng-template>
</li>
</ng-container>
<ng-template #normalTemplate>
<li class="ant-pagination-total-text" *ngIf="nzShowTotal">
<ng-template [ngTemplateOutlet]="nzShowTotal" [ngTemplateOutletContext]="{ $implicit: nzTotal,range:ranges }"></ng-template>
</li>
<li class="ant-pagination-prev"
[attr.title]="locale.prev_page"
[class.ant-pagination-disabled]="isFirstIndex"
(click)="jumpDiff(-1)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'pre'}"></ng-template>
</li>
<li class="ant-pagination-item"
[attr.title]="firstIndex"
[class.ant-pagination-item-active]="isFirstIndex"
(click)="jumpPage(firstIndex)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: firstIndex }"></ng-template>
</li>
<li class="ant-pagination-jump-prev"
*ngIf="(lastIndex > 9) && (nzPageIndex - 3 > firstIndex)"
[attr.title]="locale.prev_5"
(click)="jumpDiff(-5)">
<a class="ant-pagination-item-link">
<div class="ant-pagination-item-container">
<i nz-icon type="double-left" class="ant-pagination-item-link-icon"></i>
<span class="ant-pagination-item-ellipsis">•••</span>
</div>
</a>
</li>
<li class="ant-pagination-item"
*ngFor="let page of pages"
[attr.title]="page"
[class.ant-pagination-item-active]="nzPageIndex === page"
(click)="jumpPage(page)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: page }"></ng-template>
</li>
<li class="ant-pagination-jump-next ant-pagination-item-link-icon"
[attr.title]="locale.next_5"
(click)="jumpDiff(5)"
*ngIf="(lastIndex > 9) && (nzPageIndex + 3 < lastIndex)">
<a class="ant-pagination-item-link">
<div class="ant-pagination-item-container">
<i nz-icon type="double-right" class="ant-pagination-item-link-icon"></i>
<span class="ant-pagination-item-ellipsis">•••</span>
</div>
</a>
</li>
<li class="ant-pagination-item"
[attr.title]="lastIndex"
(click)="jumpPage(lastIndex)"
*ngIf="(lastIndex > 0) && (lastIndex !== firstIndex)"
[class.ant-pagination-item-active]="isLastIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: lastIndex }"></ng-template>
</li>
<li class="ant-pagination-next"
[title]="locale.next_page"
[class.ant-pagination-disabled]="isLastIndex"
(click)="jumpDiff(1)">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'next'}"></ng-template>
</li>
<div class="ant-pagination-options" *ngIf="nzShowQuickJumper || nzShowSizeChanger">
<nz-select class="ant-pagination-options-size-changer"
*ngIf="nzShowSizeChanger"
[nzSize]="nzSize"
[ngModel]="nzPageSize"
(ngModelChange)="onPageSizeChange($event)">
<nz-option *ngFor="let option of nzPageSizeOptions"
[nzLabel]="option + locale.items_per_page"
[nzValue]="option">
</nz-option>
<nz-option *ngIf="showAddOption"
[nzLabel]="nzPageSize + locale.items_per_page"
[nzValue]="nzPageSize">
</nz-option>
</nz-select>
<div class="ant-pagination-options-quick-jumper" *ngIf="nzShowQuickJumper">
{{ locale.jump_to }}
<input #quickJumperInput (keydown.enter)="handleKeyDown($event,quickJumperInput,true)">
{{ locale.page }}
</div>
</a>
</li>
<li
[attr.title]="lastIndex"
class="ant-pagination-item"
(click)="jumpPage(lastIndex)"
*ngIf="(lastIndex>0)&&(lastIndex!==firstIndex)"
[class.ant-pagination-item-active]="isLastIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'page',page: lastIndex }"></ng-template>
</li>
<li
title="{{ locale.next_page }}"
class="ant-pagination-next"
(click)="jumpNextOne()"
[class.ant-pagination-disabled]="isLastIndex">
<ng-template [ngTemplateOutlet]="nzItemRender" [ngTemplateOutletContext]="{ $implicit: 'next'}"></ng-template>
</li>
<div class="ant-pagination-options" *ngIf="nzShowQuickJumper||nzShowSizeChanger">
<nz-select
*ngIf="nzShowSizeChanger"
[nzSize]="nzSize=='small'?'small':''"
class="ant-pagination-options-size-changer"
[ngModel]="nzPageSize"
(ngModelChange)="onPageSizeChange($event)">
<nz-option
*ngFor="let option of nzPageSizeOptions"
[nzLabel]="option + locale.items_per_page"
[nzValue]="option">
</nz-option>
<nz-option
*ngIf="nzPageSizeOptions.indexOf(nzPageSize)==-1"
[nzLabel]="nzPageSize + locale.items_per_page"
[nzValue]="nzPageSize">
</nz-option>
</nz-select>
<div class="ant-pagination-options-quick-jumper"
*ngIf="nzShowQuickJumper">
{{ locale.jump_to }}
<input #quickJumperInput (keydown.enter)="handleKeyDown($event,quickJumperInput,true)">
{{ locale.page }}
</div>
</div>
</ng-template>
</ul>
</ng-container>
Loading

0 comments on commit 99bfeb1

Please sign in to comment.