Kal*_*kki 5 rxjs angular-material2 angular angular-cdk angular-material-6
这是我的业余爱好项目,由于这个问题它已经被卡住了一段时间。这可能是一个简单的问题,但我对 Angular 和 JS 的了解相当有限。不过我的代码在下面(我已经缩短了一点)并且它在某种程度上起作用了。它从服务器获取数据,然后在客户端显示。那里没有问题,但是现在当我尝试进行客户端过滤时,什么也没有发生。字面上地。我正在输入过滤器输入框,什么也没有。不过滤表行。
我想知道这里有两件事:
MyData.ts
export interface MyData {
id: number;
description: string;
}
Run Code Online (Sandbox Code Playgroud)
MyData.service.ts
export class MyService {
constructor(private http: HttpClient) { }
getData(): Observable<MyData[]> {
return this.http.get...
}
}
Run Code Online (Sandbox Code Playgroud)
MyData.datasource.ts
export class MyDataSource extends MatTableDataSource<MyData> {
private mySubject = new BehaviorSubject<MyData[]>([]);
constructor(private myService: MyService) { super(); }
loadData() {
this.myService.getData()
.pipe(catchError(() => of([])))
.subscribe(data => this.mySubject.next(data));
}
connect(): BehaviorSubject<myData[]> {
return this.mySubject;
}
disconnect(): void {
this.mySubject.complete();
}
}
Run Code Online (Sandbox Code Playgroud)
MyData.component.ts
export class MyDataComponent implements OnInit {
displayedColumns= ["id", "description"];
dataSource: MyDataSource;
constructor(private myService: MyService) { }
ngOnInit() {
this.dataSource = new MyDataSource(this.myService);
this.dataSource.loadData();
}
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
Run Code Online (Sandbox Code Playgroud)
MyData.component.html
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef>ID</mat-header-cell>
<mat-cell *matCellDef="let data">{{data.id}}</mat-cell>
</ng-container>
<ng-container matColumnDef="description">
<mat-header-cell *matHeaderCellDef>Description</mat-header-cell>
<mat-cell *matCellDef="let data">{{data.description}}</mat-cell>
</ng-container>
</mat-table>
Run Code Online (Sandbox Code Playgroud)
是的,如果您想更好地控制数据,例如自定义排序、过滤、分页和实时数据流/操作,您可以扩展数据源。如果没有,您可以使用材料网站中提供的默认数据源类
https://material.angular.io/components/table/overview
如上面的材料站点所述,如果您想做更复杂的事情,可以扩展数据源类
Advanced data sources
The simplest way to provide data to your table is by passing a data array. More complex use-cases may benefit from a more flexible approach involving an Observable stream or by encapsulating your data source logic into a DataSource class.
Run Code Online (Sandbox Code Playgroud)
这是一个关于如何使用它的更复杂的示例。
下面的代码是数据源的构造函数。其中包含分页器、数据服务和 Mat 排序。
constructor(public _dataService: DataService,
public _paginator: MatPaginator,
public _sort: MatSort) {
super();
// Reset to the first page when the user changes the filter.
this._filterChange.subscribe(() => this._paginator.pageIndex = 0);
}
Run Code Online (Sandbox Code Playgroud)
然后您将实现连接类。这个连接类是一个 observable,你的 mat-table 将订阅这个 observable 并根据 observable 返回的内容显示数据
connect(): Observable<Array<Data>> {
// Listen for any changes in the base data, sorting, filtering, or
//pagination the below object types are all observable/behaviour
//subjects
const displayDataChanges = [
this._dataService.entryDataChange,
this._sort.sortChange,
this._filterChange,
this._paginator.page
];
data: YourCurrentData;
// Merge all the observable into one stream
return Observable.merge(...displayDataChanges).map((n) => {
// If is filter data observer do action
// If is a sort observer emitting data do action
// If is new incoming data do action
// If is a paginator observable emmiting data do action
// Return filtered, sorted, and paged data that you want to display
// on your current page.
});
}
Run Code Online (Sandbox Code Playgroud)
上面的示例使用旧的 rxjs 版本。但希望你能理解背后的逻辑!
| 归档时间: |
|
| 查看次数: |
5338 次 |
| 最近记录: |