angular - Angular Material 数据表 - 尝试加载数据
问题描述
我正在使用下面的 TS 代码通过 Bootstrap 表加载数据,并且我想在小幅提升后试验 Angular Material Data 表。此时我只能加载表头,但找不到如何显示数据。没有抛出错误,我插入的控制台日志说:
Observable -> MapOperator -> thisArg: 未定义
import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { AngularFirestore } from '@angular/fire/firestore';
@Component({
selector: 'app-tasks',
templateUrl: './tasks.component.html',
styleUrls: ['./tasks.component.scss']
})
export class TasksComponent implements OnInit {
tasks: Observable<any[]>;
displayedColumns = ['description', 'note'];
dataSource: MatTableDataSource<Tasks>;
constructor(private db: AngularFirestore) {}
ngOnInit() {
this.tasks = this.db
.collection('tasks')
.snapshotChanges()
.pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Tasks;
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
console.log(this.tasks);
return this.tasks;
}
}
export interface Tasks {
description: string;
note: string;
}
这是我的 HTML:
<mat-table #table [dataSource]="dataSource">
<ng-container matColumnDef="description">
<mat-header-cell *matHeaderCellDef> description </mat-header-cell>
<mat-cell *matCellDef="let task">{{task.description}}</mat-cell>
</ng-container>
<ng-container matColumnDef="note">
<mat-header-cell *matHeaderCellDef> note </mat-header-cell>
<mat-cell *matCellDef="let task">{{task.note}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
我正在使用带有当前软件包版本的 Angular 7,并且数据库包含记录,那么我在这里做错了什么?
解决方案
你应该试试这个代码......
<mat-table class="highlight" #table [dataSource]="dataSource" matSort>
<!-- Dynamic -->
<ng-container *ngFor="let col of displayedColumns" matColumnDef={{col}}>
<mat-header-cell *matHeaderCellDef mat-sort-header>{{col.replace('_', ' ')| titlecase }} </mat-header-cell>
<mat-cell *matCellDef="let Exception">
<span>{{Exception[col]}}</span>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
TS
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
/* 数据源:MatTableDataSource; 将其替换为 */
dataSource = new MatTableDataSource();
this.dataSource.data = this.Task;
推荐阅读
- javascript - 捕获多个异步错误
- javascript - 取消选中所有内容时显示消息,并在每次标记每个检查时显示其他消息
- firebase - 为其他用户创建 Firebase 身份验证帐户
- javascript - 是否可以从模拟模块本身中要求模拟 Jest 模块的原始/实际实现?
- git - 带有 git pre-commit 钩子的 CalledProcessError
- pandas - 在不同行的一列中添加所有文本
- java - 如何仅在 Spring MVC 中填充内容时进行验证?
- c++ - 如何初始化一个数组?
- flutter - 在隔离中。流已被收听
- java - 为什么我必须强制转换 Constructor.newInstance 的返回值?