3

I have a material Data-table called in my component where the data is being called from an API which is defined in a service and called in the component which is working fine but when I apply the Data-table with Filtering which is provided at the material.io is not working as expected for whatever reason I don't know the filter part doesn't work?

Can anyone please help?

There are no errors or anything unusual I have tried various ways of shuffling the code and searching for this problem still nothing found.

Here is my code of component 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

2
  • Can you add html as well? Commented Mar 28, 2018 at 10:41
  • did you find a solution!? Commented May 3, 2019 at 17:48

3 Answers 3

4

I guess the problem is with your datasource, except this it seems fine.

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

And in HTML you should use datasource like this:

<mat-table [dataSource]="dataSource">
Sign up to request clarification or add additional context in comments.

Comments

3

To add to Anshuman's answer, you might also need to override the filterPredicate of your dataSource. This allows you to customise your filtering system. Let's say you want the ability to filter by Premise ID:

ngOnInit() {
    this.Info.getCustomers().subscribe(data => {
      this.dataSource = new MatTableDataSource(data);
    });
    // Define filter function to look for 'premiseId'matches
    this.dataSource.filterPredicate = function(data, filter): boolean {
      return data.premiseId.toLowerCase().includes(filter);
    };
  }

Comments

0

try this:

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 {

 dayaSource : MatTableDataSource<Element>;
 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.dataSource.filter = filterValue.trim().toLowerCase();
 }

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

1 Comment

this.dataSource.filter is not coming for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.