Angular5 Material 数据表过滤不起作用

标签 angular angular-material angular5

我在我的组件中调用了一个 Material 数据表,其中数据是从 API 调用的,该 API 在服务中定义并在工作正常的组件中调用,但是当我应用带有过滤的数据表时material.io 提供的无论出于何种原因都无法按预期工作,我不知道过滤器部分不起作用?

有人可以帮忙吗?

没有错误或任何异常我尝试了各种改组代码并搜索此问题的方法仍然没有找到。

这是我的组件 TS 代码:

 import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { ViewAgentInformationDetailComponent } from '../view-agent-information-detail/view-agent-information-detail.component'
import { MatTableDataSource, MatPaginator, MatDialog } from '@angular/material';
import { protractor } from 'protractor/built/ptor';
import { Router, ActivatedRoute } from '@angular/router';
import { Element } from '../userinfo';
import { InfoService } from '../info.service';
import { MatPaginatorModule } from '@angular/material/paginator';

@Component({
  selector: 'app-view-agent-information',
  templateUrl: './view-agent-information.component.html',
  styleUrls: ['./view-agent-information.component.scss']
})
export class ViewAgentInformationComponent implements OnInit {

  dataSource;
  data: Element[];
  constructor(private router: Router, public Info: InfoService) { }

  displayedColumns = ['ViewOpt', 'CustID', 'PremiseID', 'Status', 'Market'];

  ngOnInit() {
    this.Info.getCustomers().subscribe(data => {
      this.dataSource = data
      console.log("Data Source: ");
      console.log(this.dataSource);
    })
  }

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

  onSelect(element) {
    this.Info.changeData(element);
    this.router.navigate(['/dashboard/viewinfo', element.cust_id])
  }
}

Here you can see what is happening the screenshot

最佳答案

我想问题出在你的数据源上,除了这个似乎没问题。

ngOnInit() {
    this.Info.getCustomers().subscribe(data => {
        this.dataSource = new MatTableDataSource(data);
        // rather than this.dataSource = data;
        console.log("Data Source: ");
        console.log(this.dataSource);
    })
}

HTML 中,您应该像这样使用数据源:

<mat-table [dataSource]="dataSource">

关于Angular5 Material 数据表过滤不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532119/

相关文章:

html - 如何从 Angular Material Datepicker 中提取日期

angular - 在订阅方法中构建父子对象

当 mat-expansion-panel-header 在子组件中时 CSS 不适用

typescript - Angular 5点击绑定(bind)到 anchor 标签

angular - 通过 angular-cli 生成的 Angular 组件的自定义项目级模板

javascript - 在指令中禁用不重置

node.js - DOCKERFILE NPM 命令包含冒号和连字符

node.js - TypeScript 编译中缺少 Carousel.d.ts。请通过 'files' 或“include”属性确保它位于您的 tsconfig 中

angular - 如何删除 ng2-smart-table 中的一行

css - Angular Material 在没有 CSS Flex 的情况下对齐单元格内容?无论如何,Flex 都会在行上水平对齐单元格