问题描述
我正在将Angular材质数据表与数组作为数据源.我会使用文档中提到的renderRows()方法来更新每一个数据更改的表.
I'm using Angular material datatable with array as datasource. I would update my table every data change using renderRows() method as mentionned in the doc.
我有我的 ts文件
import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { AjouttextService } from '../ajouttext.service';
import { DataSource } from "@angular/cdk/collections";
import { Observable } from "rxjs/Observable";
import { nomchamp } from '../model';
@Component({
selector: 'app-datatable',
templateUrl: './datatable.component.html',
styleUrls: ['./datatable.component.css']
})
export class DatatableComponent implements OnInit {
constructor(private dataService: AjouttextService) { }
data: nomchamp[] = [{ id: "33", text: "HAHA" }, { id: "55", text: "bzlblz" }];
displayedColumns = ['text'];
dataSource = new MatTableDataSource(this.data);
//new UserDataSource(this.dataService);
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
}
ngOnInit() {
}
ajoutText(newtext: string, table: any) { \\ADD TEXT
let noew: nomchamp = { id: "44", text: newtext };
this.data.push(new);
table.renderRows();
}
}
有 HTML文件,我在其中声明#table变量并将其传递给ajoutText
函数
There HTML file where I declare #table variable and pass it in ajoutText
function
<form class="form">
<mat-form-field class="textinput">
<input #newtext matInput placeholder="Ajoutez votre texte" value="">
</mat-form-field>
<button type="button" mat-raised-button color="primary" (click)="ajoutText(newtext.value,table)">Ajouter</button>
</form>
<p>{{text}}</p>
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!-- Position Column -->
<ng-container matColumnDef="text">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.text}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</div>
当我在输入中添加文本并单击按钮时,出现此错误
When I add text in input and click the button I get this error
ERROR TypeError: table.renderRows is not a function
推荐答案
似乎您需要更新 @angular-cdk
打包到5.2.
Seems like you need to update your @angular-cdk
package to 5.2.
检查源代码后,仅从该版本开始添加renderRows
:
After checking the source code, renderRows
was only added since that version:
5.1版本的CdkTable源代码->不包含renderRows
方法.
5.2版本的CdkTable源代码->包含renderRows
方法.
(请注意,MatTable
扩展了CdkTable
.)
这篇关于TypeError:table.renderRows不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!