TypeError: table.renderRows is not a function
Asked Answered
G

2

1

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.

I have my ts file

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();
  }
}

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
Gravitation answered 5/3, 2018 at 22:59 Comment(1)
This might offer some clues on usage: github.com/angular/material2/blob/…Menfolk
N
3

Seems like you need to update your @angular-cdk package to 5.2.

After checking the source code, renderRows was only added since that version:

5.1 version of CdkTable source code -> does not contain a renderRows method.

5.2 version of CdkTable source code -> contains a renderRows method.

(Note that MatTable extends CdkTable.)

Nuts answered 5/3, 2018 at 23:15 Comment(0)
P
4

I used ViewChild to make it work.

First you have to import it:

import { ViewChild } from '@angular/core';

Then just after

export class DatatableComponent implements OnInit {

you declare:

@ViewChild(MatTable) table: MatTable<any>;

Now you can access it and it should have the renderRows method.

Pinsky answered 30/7, 2018 at 21:7 Comment(0)
N
3

Seems like you need to update your @angular-cdk package to 5.2.

After checking the source code, renderRows was only added since that version:

5.1 version of CdkTable source code -> does not contain a renderRows method.

5.2 version of CdkTable source code -> contains a renderRows method.

(Note that MatTable extends CdkTable.)

Nuts answered 5/3, 2018 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.