Skip to content

Commit dacb3a0

Browse files
authored
[新增] 分页组件增加searchDebounce,用于给搜索增加防抖和敲击回车键立刻搜索功能
1 parent 552c80d commit dacb3a0

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

src/app/demo/table/pageable/demo.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
[searchable]="true"
1414
[showQuickJumper]="true"
1515
(search)="onSearch($event)"
16+
[searchDebounce]="searchDebounce[0]"
1617
></jigsaw-pagination>
1718
<span>共{{pageable.pagingInfo.totalRecord}}条记录</span>
19+
<div style="padding: 10px 0; line-height: 1.5">
20+
设置搜索防抖时间: <j-button-bar [data]="['none', 1000, 3000]" [(selectedItems)]="searchDebounce" ></j-button-bar>
21+
<p>分页的默认搜索是没有防抖功能的。searchDebounce属性会给搜索增加一个防抖功能,并增加enter回车立刻搜索。</p>
22+
</div>
23+

src/app/demo/table/pageable/demo.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export class TablePageableDemoComponent {
4444
this.pageable.filter(filter, context);
4545
}
4646

47+
searchDebounce = ['none'];
48+
4749
// ====================================================================
4850
// ignore the following lines, they are not important to this demo
4951
// ====================================================================

src/app/demo/table/pageable/demo.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {JigsawTableModule} from "jigsaw/component/table/table";
33
import {JigsawPaginationModule} from "jigsaw/component/pagination/pagination";
44
import {TablePageableDemoComponent} from './demo.component';
55
import {JigsawDemoDescriptionModule} from "app/demo-description/demo-description";
6+
import {JigsawButtonBarModule} from "jigsaw/component/list-and-tile/button-bar";
67

78
@NgModule({
8-
imports: [JigsawTableModule, JigsawPaginationModule, JigsawDemoDescriptionModule],
9+
imports: [JigsawTableModule, JigsawPaginationModule, JigsawDemoDescriptionModule, JigsawButtonBarModule],
910
declarations: [TablePageableDemoComponent],
1011
exports: [TablePageableDemoComponent]
1112
})

src/jigsaw/component/pagination/pagination.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<ng-container [ngSwitch]="mode">
22
<ng-container *ngSwitchDefault>
3-
<jigsaw-input class="jigsaw-paging-search" *ngIf="searchable" width="160"
4-
[placeholder]="placeholder" (valueChange)="search.emit($event)">
3+
<jigsaw-input class="jigsaw-paging-search" *ngIf="searchable" width="160" [(value)]="_$searchKey"
4+
[placeholder]="placeholder" (valueChange)="_$searchKeyChange($event)" (keydown.enter)="_$enterSearch($event)">
55
<span jigsaw-prefix-icon class="fa fa-search"></span>
66
</jigsaw-input>
77

src/jigsaw/component/pagination/pagination.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,66 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
143143
@Input() public mode: 'complex' | 'simple' = 'complex'; // 当为「small」时,是小尺寸分页
144144
@Input() public placeholder: string = '';
145145

146+
/**
147+
* 设置了此属性会给搜索增加一个防抖功能,并增加enter回车立刻搜索
148+
* 设为'none'、NaN、小于0,或者不设置则表示不设置防抖
149+
*/
150+
@Input()
151+
public searchDebounce: number | 'none' = NaN;
152+
146153
@ViewChildren(forwardRef(() => JigsawPagingItem))
147154
private _pages: QueryList<JigsawPagingItem> = null;
148155

149156
@ViewChildren(JigsawInput) inputs: QueryList<JigsawInput>;
150157

158+
/**
159+
* @internal
160+
*/
161+
public _$searchKeyChange($event) {
162+
if (this._isValidSearchDebounce()) {
163+
this._debounceSearch($event);
164+
} else {
165+
this.search.emit($event)
166+
}
167+
}
168+
169+
private _isValidSearchDebounce() {
170+
return this.searchDebounce && this.searchDebounce != 'none' && !isNaN(this.searchDebounce) && Number(this.searchDebounce) > 0
171+
}
172+
173+
/**
174+
* @internal
175+
*/
176+
public _$searchKey: string;
177+
178+
/**
179+
* @internal
180+
*/
181+
public _$enterSearch($event) {
182+
$event.preventDefault();
183+
$event.stopPropagation();
184+
if (this._isValidSearchDebounce()) {
185+
this._clearSearchTimer();
186+
this.search.emit(this._$searchKey);
187+
}
188+
}
189+
190+
private _searchTimer: number;
191+
192+
private _debounceSearch(key: string) {
193+
this._clearSearchTimer();
194+
this._searchTimer = this.callLater(() => {
195+
this.search.emit(key);
196+
}, this.searchDebounce)
197+
}
198+
199+
private _clearSearchTimer() {
200+
if (this._searchTimer) {
201+
clearTimeout(this._searchTimer);
202+
this._searchTimer = null;
203+
}
204+
}
205+
151206
/*
152207
* 根据current设置page按钮的显示,上一页,下一页,上五页,下五页的显示
153208
* */

0 commit comments

Comments
 (0)