【发布时间】:2020-05-22 05:17:15
【问题描述】:
我的页面包含多个由 *ngFor 指令创建的 ng-select(自定义服务器端搜索)下拉列表,可以从每个下拉列表中选择多个项目。
我还想包含virtual scroll 功能,但我不知道如何发出另一个服务器请求并更新filterValues$ 值以包含新数据。
component.html
<ng-select [items]="filterValues$[filter.name] | async"
[typeahead]="filterValuesInput$[filter.name]"
[virtualScroll]="true"
[multiple]="true"
[closeOnSelect]="false"
[loading]="filterValuesLoading[filter.name]"
[(ngModel)]="filter.filter_values"
(scrollToEnd)="onScrollToEnd(filter.name)"
(open)="onFilterOpen(filter.name)"
typeToSearchText="No values found"
bindLabel="name">
</ng-select>
component.ts
onScrollToEnd(filterName) {
this.fetchMore(filterName);
}
fetchMore(filterName) {
this.filterValues$[filterName] = combineLatest(this.getFilterValues(filterName, this.afterKey), of(this.existingValues))
.pipe(
map(combined => {
return combined[1].concat(combined[0])
})
);
}
getFilterValues(filterName, after) {
return this.filterValuesInput$[filterName].pipe(
tap(() => this.filterValuesLoading[filterName] = true),
startWith(''),
distinctUntilChanged(),
switchMap(term => this.search.getFilterValues(filterName, '' + term, '' + after).pipe(
tap(res => {
this.afterKey = res.after_key;
this.filterValuesLoading[filterName] = false;
this.existingValues = this.existingValues.concat(res.filter_values);
this.totalFilterValues = res.total_hits;
//this.bufferLength += this.initialValues.length;
}),
map(res => res.filter_values),
catchError(() => of([])) // empty list on error
))
)
}
任何帮助将不胜感激!
使用更新的代码进行编辑:我设法实现了虚拟滚动功能,但是每当我进入下拉列表的底部时,它都会触发 fetchMore() 方法并重置 this.filterValues$[filterName] 值,从而将下拉列表从底部移动到顶部。我怎样才能防止这种情况发生?
【问题讨论】:
-
我注意到的唯一问题是,您没有订阅 fetchMore 结果。如果没有工作示例,很难找出问题。在stackblitz.com 上创建此类
-
请检查我更新的问题。不幸的是,我无法创建堆栈闪电战。
-
尝试设置[trackByFn]功能。这将有助于 ng-select 管理更改的项目并可能解决您的问题
标签: angular rxjs angular-ngselect virtualscroll