Leverage Angular's filtering capabilities with a variety of input options

I am in need of help with a filter for my simple list of people, where each person has a name and a first name. I want to be able to search for a specific field or both fields at the same time.

https://i.sstatic.net/pPRjO.png

The HTML code is as follows:

<table class="table table-striped table-hover">
      <thead>
          <tr>
              <th>
                <input [(ngModel)]="searchText.nom" type="text" class="form-control" placeholder="Nom">
              </th>
              <th>
                <input [(ngModel)]="searchText.prenom" type="text" class="form-control" placeholder="Prénom">
              </th>
          </tr>
      </thead>
      <tbody>
          <tr *ngFor="let defunt of defunts | filterPersonne : searchText">
              <td>{{defunt.nom}}</td>
              <td>{{defunt.prenom}}</td>
          </tr>
      </tbody>
  </table>

I have created this custom filter:

import { Pipe, PipeTransform } from '@angular/core';
import { DefuntSearch } from '../models/DefuntSearch';
import { Defunt } from '../models/Defunt';
@Pipe({
  name: 'filterPersonne'
})
export class FilterDefuntPipe implements PipeTransform {
  transform(items: Array<Personne>, searchText: Search):  Array<Personne> {
      items = searchText.nom ? items.filter(t => t.nom.includes(searchText.nom)) : items;
      items = searchText.prenom ? items.filter(t => t.nom.includes(searchText.prenom)) : items;
      return items;
   }
}

This is my Search class:

export class Search {
  constructor(
    public nom?: string,
    public prenom?: string,
  ) {

  }
}

When I provide a string input for searchText, the filter works fine. However, when searchText is an object, the filter does not respond to changes in searchText.nom or searchText.prenom. Can anyone assist me with this issue?

Thank you for your help!

Answer №1

Another option is to utilize it like this

<tr *ngFor="let defunt of defunts | filterPersonne : 
                             { nom:searchText.nom, prenom:searchText.prenom }>

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

Develop unique web components and distribute them across various frameworks

Currently, I am working on two projects that utilize Angular and React. I have noticed that certain components are duplicated in both projects. To streamline this process, I am considering creating a company library where I can develop custom components on ...

The data type '{ [key: string]: number; }' cannot be assigned to type 'T'

I’m working with a complex TypeScript type and trying to manage it within a function. Here’s what I have: type T = { a: number } | { b: number } function func(k: 'a' | 'b', v: number) { // error message below const t: T = { ...

Spin a picture in Angular 9

One of my tasks involves rotating an image in HTML using a component I'm working on. The code snippet I used is: (document.querySelector('#image') as HTMLElement).style.transform = `rotate(${degree}deg)`; Alternatively, I also tried: docu ...

When running jest unit tests, an error is thrown stating that includes() and toLowerCase are not functions

MyComponent.js contains both toLowerCase and includes methods on the props. However, when attempting to perform unit testing on MyComponent, I encounter an issue where the functions toLowerCase() and includes() are not recognized as valid. Within MyCompon ...

Learn how to apply CSS styles from one component to another in Angular

I am attempting to modify the background color of a wrapper from a different component. I tried using ::ng-deep but it's not functioning correctly. For the mauto component, the background should be a solid block color. For the vgud component, the back ...

What is the best way to export Firebase admin in Typescript?

Struggling to export Firebase admin for access in my modules while using Typescript. Upon importing it into my modules, the properties of admin are not recognized. admin.ts import * as admin from "firebase-admin"; admin.initializeApp(); module. ...

Transitioning a codebase from @angular-builders/custom-webpack to NX for project optimization

I need help migrating my Angular project from using "@angular-builders/custom-webpack" build targets to transitioning to an integrated NX monorepo. When I run the command npx nx@latest init --integrated, I receive the following warning: Unsupported build ...

Utilizing Angular for Webcam Integration

After trying out this code snippet: <video autoplay playsinline style="width: 100vw; height: 100vh;"></video> <script> navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } }) .then(stream =&g ...

The responsive table fails to completely fill the column it is contained within

In my Angular application, I am integrating Bootstrap 4. Within one of the component's HTML files, I am attempting to embed a responsive table within a grid row. I have followed the instructions by nesting the "table" div inside another div with the ...

Dynamic Data Binding in Ionic 2

One challenge I am facing is with creating my own info window for a Google Maps marker. Whenever I click on the marker, a div is displayed but the information inside the div does not get updated. It seems like the event.title remains unchanged from its old ...

Ways to collect particular tokens for delivering targeted push notifications to designated devices

When filtering the user's contacts, I ensure that only contacts with created accounts are displayed on the screen. This process helps in visually organizing the contact list. List<PhonesContacts> phoneContacts = snapshot.data; Lis ...

How come my ts-mockito spy isn't delegating method calls properly?

In my code, I have a class named MyPresenter which has a method called doOperation(). This method calls another method on a View class that implements an interface and is passed in as a parameter. Below you can find the implementation of the class, interfa ...

Issue with Typescript - Node.js + Ionic mobile app's Angular AoT build has encountered an error

Currently, I am in the process of developing an Android application using Node.js and Ionic framework. The app is designed to display random text and images stored in separate arrays. While testing the app on Chrome, everything works perfectly fine. Upon ...

Can you explain the distinction between embeddedViewRef and templateRef?

check this page for more information The statement above explains how an embedded view can be accessed from a different component than the one where it is defined, or it can be created separately using a TemplateRef. Can you clarify what this means? I&ap ...

Creating a library that relies on Cypress without the need to actually install Cypress

We have adopted the page object pattern in our testing and recently made the decision to move them into a separate npm-published library for reusability. Considering the heavy nature of Cypress and potential version conflicts, we believe it's best no ...

TSX: Interface Definition for Nested Recursive Array of Objects

I'm having trouble making my typescript interface compatible with a react tsx component. I have an array of objects with possible sub items that I need to work with. Despite trying various interfaces, I always run into some kind of error. At the mome ...

Steps for integrating .web extension files in React Native Web using Typescript

When using react native web, there is a potential to utilize files with extensions such as .web and .android. For example: myFile.web.js myFile.android.js These files can then be included like so: import myFile from './myFile'; React Native w ...

Typescript enhances React Native's Pressable component with a pressed property

I'm currently diving into the world of typescript with React, and I've encountered an issue where I can't utilize the pressed prop from Pressable in a React Native app while using typescript. To work around this, I am leveraging styled comp ...

Using the useContext hook across multiple files without needing to export it

I am working on a React app that has multiple states being managed function App(){ const [activeChoice, setActiveChoice] = useState("flights"); const [overlay, setOverlay] = useState(false); const [airports, setAirports] = useState([]); const [loading, ...

Converting a string to a Date in Angular2 using Typescript

I need to initialize a new Date object using a specific date. I have considered converting it from a particular string, like so: let dateString = '1968-11-16T00:00:00' How can I achieve this in typescript? Update: I am specifically looking fo ...