I am facing issues with my filtering functionality on the Angular Typescript mat-table component

I am facing issues while trying to filter my table, the reaction is not correct and I can't seem to find where I went wrong.

Here is the HTML part :

<mat-form-field appearance="outline">
  <mat-label>Search</mat-label>
  <mat-icon style="color: #4D59EF;" matPrefix>search</mat-icon>
  <input matInput [formControl]="searchBar"  placeholder="Search" #input>
</mat-form-field>

And the TypeScript part :

displayedColumns: string[] = ['natureDuService', 'domaineApplication', 'statusService', 'descriptionFonctionnelle'];
dataSource: MatTableDataSource<IdsGeneralModel> = new MatTableDataSource();

searchBar = new FormControl('');

filteredValues = { natureDuService: '', typeService: '', couvGeo: '', status: '', domain: '' };

ngOnInit(): void {

  this.searchBar.valueChanges.subscribe((searchBarFilterValue) => {
    this.filteredValues['natureDuService'] = searchBarFilterValue;
    this.dataSource.filter = JSON.stringify(this.filteredValues);
  });

  this.dataSource.filterPredicate = this.customFilterPredicate();
}

customFilterPredicate() {
  const myFilterPredicate = function (data: IdsGeneralModel, filter: string): boolean {
    let searchString = JSON.parse(filter);
    let searchFound = data.natureDuService.toString().trim().toLowerCase().indexOf(searchString.natureDuService.toLowerCase()) !== -1
    if (searchString.topFilter) {
      return searchFound
    } else {
      return searchFound
    }
  }
  return myFilterPredicate;
}

Your assistance will be greatly appreciated. Thank you!

Answer №1

No information has been linked to the data origin

You have the option to connect it during the initialization of the dataSource variable, and the rest of your programming should function correctly.

dataSource: MatTableDataSource<IdsGeneralModel> = new MatTableDataSource(ELEMENT_DATA);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to specify the type of a property that has already been assigned

I am currently utilizing a third-party library that includes a type defined as the following: export interface ThirdPartyNodeType { id: string; name: string; data: any; } Upon further exploration, I have identified the content that I intend to include ...

Utilizing JavaScript variables imported from an external library in Next.js: A Guide

I am currently working on a Next.js with Typescript website and I am in the process of adding advertisements. The ad provider has given me instructions to embed this JavaScript code on my site: <script src="//m.servedby-buysellads.com/monetization. ...

Is it advisable to send an http request within the ngOnInit of an Angular Universal component?

During my Angular app development, I did not encounter any errors while running it via ng serve. However, when I switched to SSR (Server-Side Rendering), I started receiving an error in my server log whenever a component that makes an http request as part ...

Having trouble incorporating a canvas created with html2canvas into the addHTML() method of jspdf

I am working on developing an application using angular5 and I am attempting to incorporate HTML by utilizing the jspdf.addHTML() function in conjunction with html2canvas. const content = this.vc_print_section.nativeElement; html2canvas(content).then(can ...

What is the best way to incorporate audio playback while browsing files on an HTML5 webpage with TypeScript?

<input type="file" id="soundUrl" (change)="onAudioPlay()" /> <audio id="sound" controls></audio> This is the HTML code I'm working with, and I'm looking to trigger audio playback after a user selects an MP3 file using TypeScrip ...

Tips for integrating an angular module that lacks Universal compatibility into an angular/cli application with server-side rendering (SSR) support

Greetings everyone! I've been working on developing an Angular app that utilizes Universal with SSR for quite some time now. At times, while including a module like ngx-editor, I noticed that the server would fail silently without any indication of wh ...

Exploring the intricacies of pattern matching with JavaScript visualization

I am currently working on improving my pattern matching function by creating a visualizer for it. To achieve this, I need to slow down the execution of each comparison. My approach involves declaring the i and j variables outside of the function so that I ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

Efficiently Updating Property Values in Objects Using TypeScript and Loops

I have been looking into how to iterate or loop through a code snippet, and while I managed to do that, I am facing an issue where I cannot change the value of a property. Here is the snippet of code: export interface BaseOnTotalPaidFields { groupName ...

Data binding in Angular 2: Connecting components

Is it possible to establish a connection between two components that are working with related objects? One of the components is dedicated to filtering, while the other displays the data results. By applying filters such as checkboxes, the displayed data ...

Error occurs when Protractor end-to-end tests encounter a problem with browser.executeAsyncScript

I encountered an error while attempting to implement e2e testing for our application. My initial test case failed with the following error message: Failed: Error while running testForAngular: javascript error: Unexpected identifier JavaScript stack: ...

Importing components in real-time to generate static sites

My website has a dynamic page structure with each page having its unique content using various components. During the build process, I am statically pre-rendering the pages using Next.js' static site generation. To manage component population, I have ...

The mat-datepicker appears in a unique location each time it is opened

I am facing an issue with my mat-datepicker appearing in the opposite place when clicked on one component. How can I resolve this? I have two different components with mat-datepicker, but they behave differently. I suspect that imitating a click in one com ...

There has been an unhandled runtime error: [object ProgressEvent] occurring with Next.js/Typescript

Exploring the world of nextJS and typescript is new to me. I am currently working on creating a simple blog using nextJS/typescript with a sanity CMS backend. Everything seems to be running smoothly during development, but then I encounter this Unhandled R ...

How can the value of a number in Angular be changed without altering its original value?

Imagine having the initial number 100. If I enter 50 in another input, it should add 50 to 100. However, if I then change the value from 50 to 80, the total should be 180 and not 230. The goal is always to add numbers to the original sum, not the new valu ...

What is the implementation of booleans within the Promise.all() function?

I am looking to implement a functionality like the following: statusReady: boolean = false; jobsReady: boolean = false; ready() { return Promise.all([statusReady, jobsReady]); } ...with the goal of being able to do this later on: this.ready().then(() ...

Issue with iPad Pro displaying Bootstrap 4 Navbar toggle icon

Having trouble with my Bootstrap 4.1.1 navbar on iPadPro. It's not displaying the correct data from the div and it's not collapsing properly. However, it works fine on small devices. Any suggestions on how to fix this issue? Here's the code ...

"Encountered a problem with Next JS API while trying to fetch data from the app directory

import { NextResponse } from "next/server"; export async function POST(request: Request) { const data = await request.json(); console.log(data); return NextResponse.json({ foo: "boo" }); } next version = "next": &quo ...

The letter 'X' is not suitable for use as a JSX component because its return type 'Element[]' does not qualify as a valid JSX element

Currently, I am working on rendering two simple joke cards in TypeScript. The cards are displaying correctly in my browser, but I've encountered an error message that says: 'Jokes' cannot be used as a JSX component. Its return type 'Ele ...

What do you do when schema.parseAsync cannot be found?

Currently facing an issue with zod validation in my Node.js environment, specifically encountering the error: TypeError: schema.parseAsync is not a function Despite attempting various solutions like re-importing and troubleshooting, I am unable to resol ...