Angular 错误类型错误:this.dataSource.filter不是函数

Angular 错误类型错误:this.dataSource.filter不是函数,angular,Angular,我使用angular 6通过使用对话框内联行操作来实现材料表中的编辑/添加/删除行,但得到错误数据源。当我按下更新按钮时,过滤器不是一个功能,我不知道这里有什么问题,我缺少什么 这是我的更新管理信息组件ts* ```import { Component, ViewChild, OnInit, ChangeDetectorRef } from '@angular/core'; import {MatTableDataSource, MatTable} from '@angular/materi

我使用angular 6通过使用对话框内联行操作来实现材料表中的编辑/添加/删除行,但得到错误数据源。当我按下更新按钮时,过滤器不是一个功能,我不知道这里有什么问题,我缺少什么

这是我的更新管理信息组件ts*

```import { Component, ViewChild, OnInit, ChangeDetectorRef   } from '@angular/core';
import {MatTableDataSource, MatTable} from '@angular/material/table';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
import { UserService } from '../services/user.service';
import { User } from '../model/User.model'
import { DialogeditComponent } from '../dialogedit/dialogedit.component';


@Component({
  selector: 'app-update-admin-info',
  templateUrl: './update-admin-info.component.html',
  styleUrls: ['./update-admin-info.component.css']
})
export class UpdateAdminInfoComponent implements OnInit {
  displayedColumns: string[] = ['firstname', 'lastname', 'email','college','department','action'];
   dataSource;
  users: User[];

  @ViewChild(MatTable,{static:true}) table: MatTable<any>;

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }


  constructor(public dialog: MatDialog,private userservice:UserService,private cdr: ChangeDetectorRef) { }

  ngOnInit() {
    this.userservice.getAdmins()
    .subscribe((users: User[]) => {
      this.users = users;
      this.dataSource = new MatTableDataSource(users);

    });
  }

  editUser(action,obj)
{
  obj.action = action;
 const dialogRef = this.dialog.open(DialogeditComponent,{
    width: '250px',
    data:obj

  });
  dialogRef.afterClosed().subscribe(result => {
    if(result.event == 'update')
    {
      this.updateRowData(result.data);
    }

  });

     this.cdr.detectChanges();
}

updateRowData(row_obj){
  this.dataSource = this.dataSource.filter((value,key)=>{
    if(value._id == row_obj.id){
      value.firstname = row_obj.firstname;
    }
    return true;
  });

 this.cdr.detectChanges();
 this.table.renderRows();
}


}```
``从'@angular/core'导入{Component,ViewChild,OnInit,ChangeDetectorRef};
从“@angular/material/table”导入{MatTableDataSource,MatTable};
从“@angular/material/DIALOG”导入{MatDialog,MatDialogRef,MAT_DIALOG_DATA};
从“../services/user.service”导入{UserService};
从“../model/User.model”导入{User}
从“../dialogedit/dialogedit.component”导入{DialogeditComponent};
@组成部分({
选择器:“应用程序更新管理信息”,
templateUrl:“./update admin info.component.html”,
样式URL:['./更新管理信息.component.css']
})
导出类UpdateAdminInfo组件实现OnInit{
显示的列:字符串[]=['firstname','lastname','email','college','department','action'];
数据源;
用户:用户[];
@ViewChild(MatTable,{static:true})table:MatTable;
applyFilter(filterValue:string){
this.dataSource.filter=filterValue.trim().toLowerCase();
}
构造函数(公共对话框:MatDialog,私有userservice:userservice,私有cdr:ChangeDetectorRef){}
恩戈尼尼特(){
this.userservice.getAdmins()
.subscribe((用户:User[])=>{
this.users=用户;
this.dataSource=新MatTableDataSource(用户);
});
}
编辑用户(操作,obj)
{
行动=行动;
const dialogRef=this.dialog.open(DialogeditComponent{
宽度:'250px',
资料来源:obj
});
dialogRef.afterClosed().subscribe(结果=>{
如果(result.event=='update')
{
this.updateRowData(result.data);
}
});
this.cdr.detectChanges();
}
updateRowData(行对象){
this.dataSource=this.dataSource.filter((值,键)=>{
if(值.\u id==行_对象id){
value.firstname=行\对象.firstname;
}
返回true;
});
this.cdr.detectChanges();
this.table.renderRows();
}
}```
这是我的Dialogedit.component.ts

```import { Component, OnInit, Inject, ChangeDetectorRef } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { User } from '../model/User.model';
import { UserService } from '../services/user.service';
import { UpdateinfoService } from '../services/updateinfo.service';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { FormGroup, Validators, FormControl, AbstractControl } from '@angular/forms';



@Component({
  selector: 'app-dialogedit',
  templateUrl: './dialogedit.component.html',
  styleUrls: ['./dialogedit.component.css'],
  providers: [UserService]
})
export class DialogeditComponent implements OnInit {

  action:string;
  local_data:any;

  constructor(public dialogRef: MatDialogRef<DialogeditComponent>,
    @Inject(MAT_DIALOG_DATA) public data: User, public http: HttpClient, public updateinfo: UpdateinfoService, public router: Router, private userservice:UserService,private cdr: ChangeDetectorRef) {  console.log(data);
      this.local_data = {...data};
      this.action = this.local_data.action;}

  ngOnInit() {


  }
  onNoClick(): void {
    this.dialogRef.close({event:'Cancel'});
    this.cdr.detectChanges();
  }

  onEdit() {
    this.dialogRef.close({event:this.action,data:this.local_data});
    this.userservice.selectedUser =this.local_data;
    this.cdr.detectChanges();
  }


}```
``从'@angular/core'导入{Component,OnInit,Inject,ChangeDetectorRef};
从“@angular/material/DIALOG”导入{MatDialog,MatDialogRef,MAT_DIALOG_DATA};
从“../model/User.model”导入{User};
从“../services/user.service”导入{UserService};
从“../services/updateinfo.service”导入{updateinfo服务};
从'@angular/common/http'导入{HttpClient};
从'@angular/Router'导入{Router};
从'@angular/forms'导入{FormGroup,Validators,FormControl,AbstractControl};
@组成部分({
选择器:“应用程序对话框编辑”,
templateUrl:'./dialogedit.component.html',
样式URL:['./dialogedit.component.css'],
提供者:[用户服务]
})
导出类DialogeditComponent实现OnInit{
动作:字符串;
本地_数据:任何;
构造函数(公共dialogRef:MatDialogRef,
@注入(MAT_DIALOG_DATA)公共数据:User,public http:HttpClient,public updateinfo:updateinfo服务,public router:router,private userservice:userservice,private cdr:ChangeDetectorRef){console.log(数据);
this.local_data={…data};
this.action=this.local_data.action;}
恩戈尼尼特(){
}
onNoClick():void{
this.dialogRef.close({event:'Cancel'});
this.cdr.detectChanges();
}
onEdit(){
this.dialogRef.close({event:this.action,data:this.local_data});
this.userservice.selectedUser=this.local\u数据;
this.cdr.detectChanges();
}
}```
这是我的Dialogedit.component.html

```<h1 mat-dialog-title>Row Action :: <strong>{{action}}</strong></h1>
    <div mat-dialog-content>
        <mat-form-field *ngIf="action != 'Delete'; else elseTemplate">
          <input   matInput [(ngModel)]="local_data.firstname" placeholder="First Name">
        </mat-form-field>
        <mat-form-field>
          <input  matInput [(ngModel)]="local_data.lastname" placeholder="Last Name">
        </mat-form-field>
      </div>
      <div mat-dialog-actions>
        <button mat-button (click)="onNoClick()"  mat-flat-button color="warn">Cancel</button>
        <button  mat-button (click) = "onEdit(local_data)"  >{{action}}</button>
      </div>```
``行操作::{{{Action}
取消
{{action}}
```

我得到的错误在这一行
this.dataSource=this.dataSource.filter((值,键)=>{

是this.dataSource.filter是数组吗

您有this.datasource.filter,filter也是数组方法

我想你把它改名了

或者

此.data.source可能不是数组


console.log所有内容都是this.datasource.filter是数组吗

您有this.datasource.filter,filter也是数组方法

我想你把它改名了

或者

此.data.source可能不是数组


console.log everythis

我已经阅读了
MatTableDataSource
的代码,它有
get data()
返回对象数组。因此,您应该使用
this.dataSource.data.filter
而不是
this.dataSource.filter

根据你的第二个问题

但是,如果有人能帮助我如何使用matDialog在表上实现内联编辑,我的数据将无法更新


这是一个新的线程,请打开新的问题。

我已经检查了
MatTableDataSource
的代码,它有
get data()
返回对象数组。所以你应该使用
this.dataSource.data.filter
而不是
this.dataSource.filter

根据你的第二个问题

但是,如果有人能帮助我如何使用matDialog在表上实现内联编辑,我的数据将无法更新


这是一个新线程,请就此打开新问题。

是否为
数据源
数组?这是因为
数据源
未定义,无法修复它
数据源:any[]=[]
dataSource
不是数组,而是类型为
MatDataSource
因此错误我如何解决它brother@pareshlomates我如何解决这个bro@crueengineis
dataSource
数组?这是因为
dataSource
没有定义来修复它
dataSource:any[]
dataSource
不是数组,而是类型为
MatDataSource
因此错误我如何解决它brother@PareshLom