Skip to content

Commit d0f8c9b

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

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

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

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class TablePageableDemoComponent {
4444
];
4545

4646
onSearch(reg) {
47+
console.log(reg);
4748
// 这里需要特别注意,filter函数的执行是在服务端,而非在浏览器!
4849
// 这里context变量是filter的执行上下文(即filter函数里的this所指向的对象),它将会一起传输给服务端,
4950
// 因此这里需要注意控制context的值里只包含有用的数据,以加快前后端通信速度
@@ -55,6 +56,8 @@ export class TablePageableDemoComponent {
5556
this.pageable.filter(filter, context);
5657
}
5758

59+
searchDebounce = ['none'];
60+
5861
// ====================================================================
5962
// ignore the following lines, they are not important to this demo
6063
// ====================================================================

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {NgModule} from '@angular/core';
2-
import {JigsawTableModule, JigsawPaginationModule} from "jigsaw/public_api";
2+
import {JigsawTableModule, JigsawPaginationModule, JigsawButtonBarModule} from "jigsaw/public_api";
33
import {TablePageableDemoComponent} from './demo.component';
44
import {JigsawDemoDescriptionModule} from "app/demo-description/demo-description";
55

66
@NgModule({
7-
imports: [JigsawTableModule, JigsawPaginationModule, JigsawDemoDescriptionModule],
7+
imports: [JigsawTableModule, JigsawPaginationModule, JigsawDemoDescriptionModule, JigsawButtonBarModule,],
88
declarations: [TablePageableDemoComponent],
99
exports: [TablePageableDemoComponent]
1010
})

src/jigsaw/pc-components/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/pc-components/pagination/pagination.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
130130
@Input()
131131
public placeholder: string = '';
132132

133+
/**
134+
* 设置了此属性会给搜索增加一个防抖功能,并增加enter回车立刻搜索
135+
* 设为'none'、NaN、小于0,或者不设置则表示不设置防抖
136+
*/
137+
@RequireMarkForCheck()
138+
@Input()
139+
public searchDebounce: number | 'none' = NaN;
140+
133141
@Output()
134142
public search = new EventEmitter<string>();
135143
/**
@@ -149,6 +157,55 @@ export class JigsawPagination extends AbstractJigsawComponent implements OnInit,
149157
@ViewChildren(JigsawInput)
150158
public inputs: QueryList<JigsawInput>;
151159

160+
/**
161+
* @internal
162+
*/
163+
public _$searchKeyChange($event) {
164+
if (this._isValidSearchDebounce()) {
165+
// 输入3000ms没有回车也会发一次事件
166+
this._debounceSearch($event);
167+
} else {
168+
this.search.emit($event)
169+
}
170+
}
171+
172+
private _isValidSearchDebounce() {
173+
return this.searchDebounce && this.searchDebounce != 'none' && !isNaN(this.searchDebounce) && Number(this.searchDebounce) > 0
174+
}
175+
176+
/**
177+
* @internal
178+
*/
179+
public _$searchKey: string;
180+
181+
/**
182+
* @internal
183+
*/
184+
public _$enterSearch($event) {
185+
$event.preventDefault();
186+
$event.stopPropagation();
187+
if (this._isValidSearchDebounce()) {
188+
this._clearSearchTimer();
189+
this.search.emit(this._$searchKey);
190+
}
191+
}
192+
193+
private _searchTimer: number;
194+
195+
private _debounceSearch(key: string) {
196+
this._clearSearchTimer();
197+
this._searchTimer = this.callLater(() => {
198+
this.search.emit(key);
199+
}, this.searchDebounce)
200+
}
201+
202+
private _clearSearchTimer() {
203+
if (this._searchTimer) {
204+
clearTimeout(this._searchTimer);
205+
this._searchTimer = null;
206+
}
207+
}
208+
152209
private _current: number;
153210

154211
/**

0 commit comments

Comments
 (0)