Add debounce to search
This commit is contained in:
parent
25d8b6ec02
commit
481d9b2394
2 changed files with 18 additions and 3 deletions
|
@ -49,6 +49,7 @@
|
|||
<input class="form-control" type="text" placeholder="search"
|
||||
ng-model="$ctrl.enteredQuery"
|
||||
typeahead="$ctrl.onTypeahead($event)"
|
||||
ta-debounce="250"
|
||||
ta-display-key="name"
|
||||
ta-suggestion-tmpl="search-result-template"
|
||||
ta-clear-on-select="$ctrl.clearOnSearch"
|
||||
|
|
|
@ -13,16 +13,19 @@ export class TypeaheadDirective implements AfterContentInit {
|
|||
@Input('taDisplayKey') displayKey: string = '';
|
||||
@Input('taSuggestionTmpl') suggestionTemplate: string = '';
|
||||
@Input('taClearOnSelect') clearOnSelect: boolean = false;
|
||||
@Input('taDebounce') debounce: number = 250;
|
||||
|
||||
@Output('taSelected') selected = new EventEmitter<any>();
|
||||
@Output('taEntered') entered = new EventEmitter<any>();
|
||||
|
||||
private itemSelected: boolean = false;
|
||||
private existingTimer: Promise = null;
|
||||
|
||||
constructor(@Inject('$element') private $element: ng.IAugmentedJQuery,
|
||||
@Inject('$compile') private $compile: ng.ICompileService,
|
||||
@Inject('$scope') private $scope: ng.IScope,
|
||||
@Inject('$templateRequest') private $templateRequest: ng.ITemplateRequestService) {
|
||||
@Inject('$templateRequest') private $templateRequest: ng.ITemplateRequestService,
|
||||
@Inject('$timeout') private $timeout: ng.ITimeoutService) {
|
||||
}
|
||||
|
||||
public ngAfterContentInit(): void {
|
||||
|
@ -52,12 +55,23 @@ export class TypeaheadDirective implements AfterContentInit {
|
|||
templates: templates,
|
||||
display: this.displayKey,
|
||||
source: (query, results, asyncResults) => {
|
||||
this.typeahead.emit({'query': query, 'callback': asyncResults});
|
||||
this.itemSelected = false;
|
||||
this.debounceQuery(query, asyncResults);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private debounceQuery(query: string, asyncResults: Function): void {
|
||||
if (this.existingTimer) {
|
||||
this.$timeout.cancel(this.existingTimer);
|
||||
this.existingTimer = null;
|
||||
}
|
||||
|
||||
this.existingTimer = this.$timeout(() => {
|
||||
this.typeahead.emit({'query': query, 'callback': asyncResults});
|
||||
this.itemSelected = false;
|
||||
}, this.debounce);
|
||||
}
|
||||
|
||||
@HostListener('keyup', ['$event'])
|
||||
public onKeyup(event: JQueryKeyEventObject): void {
|
||||
if (!this.itemSelected && event.keyCode == 13) {
|
||||
|
|
Reference in a new issue