在哪里可以找到无限滚动自动完成输入的角度扩展(例如https://select2.org/data-sources/ajax)?我有一个带分页的API,当用户向下滚动时必须捕获其他对象
答案 0 :(得分:3)
angular material 2 autocomplete的无限滚动
<form class="example-form">
<mat-form-field class="example-full-width">
<input matInput placeholder="State" aria-label="State" [matAutocomplete]="statesAutocomplete" [formControl]="stateCtrl">
<mat-autocomplete #statesAutocomplete="matAutocomplete" (opened)="autocompleteScroll()">
<mat-option *ngFor="let state of filteredStates" [value]="state.name">
<img class="example-option-img" aria-hidden [src]="state.flag" height="25">
<span>{{state.name}}</span> |
<small>Population: {{state.population}}</small>
</mat-option>
</mat-autocomplete>
</mat-form-field>
<br>
</form>
@ViewChild('statesAutocomplete') statesAutocompleteRef: MatAutocomplete;
@ViewChild(MatAutocompleteTrigger) autocompleteTrigger: MatAutocompleteTrigger;
autocompleteScroll() {
setTimeout(() => {
if (
this.statesAutocompleteRef &&
this.autocompleteTrigger &&
this.statesAutocompleteRef.panel
) {
fromEvent(this.statesAutocompleteRef.panel.nativeElement, 'scroll')
.pipe(
map(x => this.statesAutocompleteRef.panel.nativeElement.scrollTop),
takeUntil(this.autocompleteTrigger.panelClosingActions)
)
.subscribe(x => {
const scrollTop = this.statesAutocompleteRef.panel.nativeElement
.scrollTop;
const scrollHeight = this.statesAutocompleteRef.panel.nativeElement
.scrollHeight;
const elementHeight = this.statesAutocompleteRef.panel.nativeElement
.clientHeight;
const atBottom = scrollHeight === scrollTop + elementHeight;
if (atBottom) {
// fetch more data
this.filteredStates = [...this.filteredStates, ...this.states]
}
});
}
});
}