Rob*_*aro 4 observable rxjs ngrx angular-material2 angular
我有一个 Angular Material Data Table,我正在尝试为它添加分页。
我遇到过这个问题,如果数据是异步源,那么 Mat-Paginator 就不起作用。
我尝试使用同步源,分页按预期工作,但由于我有一个可观察的,当数据最终到达时分页不会被初始化。
但是,我已尝试搜索此问题,但到目前为止似乎没有人遇到此问题,因此可能与我的设置有关。
以下是我的组件的 ts 文件。
import { Component, Input, Output, EventEmitter, style, Inject, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Router } from '@angular/router';
import { Country} from '../../core/models/country';
import { OnChanges, AfterViewInit } from '@angular/core';
import { MatTableDataSource, MatPaginator } from '@angular/material';
@Component({
// tslint:disable-next-line:component-selector
selector: 'app-country-list',
templateUrl: './country-preview-list.html',
styleUrls : ['./country-preview-list.css']
})
export class CountryListComponent implements OnChanges, AfterViewInit {
@Input() public countries: Country[];
displayedColumns = ['Name', 'Code'];
dataSource: MatTableDataSource<Country>;
@ViewChild(MatPaginator) paginator: MatPaginator;
ngOnChanges(): void {
this.dataSource = new MatTableDataSource<Country>(this.countries);
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
}
Run Code Online (Sandbox Code Playgroud)
和父组件 html 有这一行来添加我的组件
<app-country-list [countries]="countries$ | async"></app-country-list>
Run Code Online (Sandbox Code Playgroud)
编辑
父组件 TS 文件
import { Component, ChangeDetectionStrategy, OnChanges } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { take } from 'rxjs/operators';
import * as fromCountries from '../../state/countries/reducers';
import * as fromConfiguration from '../../state/configuration/reducers';
import * as country from '../../state/countries/actions/countries';
import * as configuration from '../../state/configuration/actions/configuration';
import { Country} from '../../core/models/country';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
selector: 'app-countries-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<app-country-search (searchCountries)="search($event)"></app-country-search>
<app-country-list [countries]="countries$ | async"></app-country-list>
`,
})
export class CountriesPageComponent {
countries$: Observable<Country[]>;
constructor(private store: Store<fromCountries.State>) {
this.countries$ = store.pipe(select(fromCountries.getAllCountries));
}
search(query: any) {
this.store.dispatch(new country.Search(query));
}
}
Run Code Online (Sandbox Code Playgroud)
如果改变国家$ | 异步到同步的国家列表;分页器工作。
有人可以帮忙吗。谢谢
您的最后一条评论帮助我找到了解决方案。这确实是由 dataTable 位于 ngIf* 内这一事实引起的。
要解决此问题,请删除此代码:
@ViewChild(MatPaginator) paginator: MatPaginator;
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
Run Code Online (Sandbox Code Playgroud)
并添加这个:
private paginator: MatPaginator;
@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
this.paginator = mp;
this.dataSource.paginator = this.paginator;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8084 次 |
| 最近记录: |