Angular - How to filter ngFor to specific object property data

Angular - How to filter ngFor to specific object property data

To filter an ngFor loop by a specific object property in Angular, you can use a custom pipe or filter the data directly in the component. Here are both approaches:

Approach 1: Using a Custom Pipe

  1. Create a Custom Pipe:

    Generate a custom pipe using Angular CLI:

    ng generate pipe filter 

    filter.pipe.ts:

    import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filter' }) export class FilterPipe implements PipeTransform { transform(items: any[], searchText: string, property: string): any[] { if (!items || !searchText) { return items; } return items.filter(item => item[property].toLowerCase().includes(searchText.toLowerCase()) ); } } 
  2. Use the Pipe in Your Component:

    In your component, define the items and the search text.

    app.component.ts:

    import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', }) export class AppComponent { items = [ { name: 'Apple', category: 'Fruit' }, { name: 'Carrot', category: 'Vegetable' }, { name: 'Banana', category: 'Fruit' }, { name: 'Broccoli', category: 'Vegetable' } ]; searchText: string = ''; } 
  3. Template with ngFor:

    Use the pipe in your template.

    app.component.html:

    <input type="text" [(ngModel)]="searchText" placeholder="Search by name"> <ul> <li *ngFor="let item of items | filter:searchText:'name'"> {{ item.name }} - {{ item.category }} </li> </ul> 

Approach 2: Filtering Directly in the Component

If you prefer not to use a custom pipe, you can filter the array directly in your component's method.

  1. Define a Filter Method:

    app.component.ts:

    import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', }) export class AppComponent { items = [ { name: 'Apple', category: 'Fruit' }, { name: 'Carrot', category: 'Vegetable' }, { name: 'Banana', category: 'Fruit' }, { name: 'Broccoli', category: 'Vegetable' } ]; searchText: string = ''; get filteredItems() { return this.items.filter(item => item.name.toLowerCase().includes(this.searchText.toLowerCase()) ); } } 
  2. Template with ngFor:

    Use the method in your template.

    app.component.html:

    <input type="text" [(ngModel)]="searchText" placeholder="Search by name"> <ul> <li *ngFor="let item of filteredItems"> {{ item.name }} - {{ item.category }} </li> </ul> 

Summary

  • Custom Pipe: Use a custom pipe for reusable filtering logic, making it easy to filter data in multiple components.
  • Direct Filtering: Alternatively, filter the data directly in the component using a getter method for simpler use cases.

Both approaches provide a flexible way to filter ngFor data based on specific object properties in Angular.

Examples

  1. How to filter ngFor based on a specific object property in Angular? Description: Use Angular's ngFor directive with a filter to display items based on a specific object property.

    <div *ngFor="let item of items"> <div *ngIf="item.property === 'value'"> {{ item.property }} </div> </div> 
  2. Filtering ngFor based on a nested object property in Angular? Description: Filter ngFor based on a nested property of objects in an array using Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.nestedObject.property === 'value'"> {{ item.property }} </div> </div> 
  3. How to display only specific items using ngFor in Angular? Description: Use ngFor with ngIf to display only items matching a certain condition in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.condition === true"> {{ item.property }} </div> </div> 
  4. Using ngFor with filter to display items with specific property values in Angular? Description: Implement ngFor with a custom pipe or method to filter and display items based on specific property values in Angular.

    <div *ngFor="let item of items | filterByProperty: 'property', 'value'"> {{ item.property }} </div> 
  5. Filtering ngFor to show items where property is not null in Angular? Description: Filter ngFor to display items where a specific property is not null or undefined in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.property"> {{ item.property }} </div> </div> 
  6. How to use ngFor to filter and display items with a certain property value range in Angular? Description: Use ngFor with ngIf to filter and display items within a specified property value range in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.property >= minValue && item.property <= maxValue"> {{ item.property }} </div> </div> 
  7. Filtering ngFor based on a boolean property value in Angular? Description: Display items using ngFor based on a boolean property value being true or false in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.booleanProperty === true"> {{ item.property }} </div> </div> 
  8. How to filter ngFor to show items with a specific property value using Angular pipes? Description: Filter ngFor to display items based on a specific property value using Angular pipes for data transformation.

    <div *ngFor="let item of items | filterByProperty: 'property', 'value'"> {{ item.property }} </div> 
  9. Using ngFor to display items where property matches a regex pattern in Angular? Description: Implement ngFor to filter and display items where a property matches a regular expression pattern in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.property.match('regex_pattern')"> {{ item.property }} </div> </div> 
  10. How to filter ngFor to display items with specific property values dynamically in Angular? Description: Use ngFor with a dynamic filter condition to display items with specific property values based on user input or conditions in Angular.

    <div *ngFor="let item of items"> <div *ngIf="item.property === filterValue"> {{ item.property }} </div> </div> 

More Tags

exoplayer2.x spring-rest gatt exif splunk jxl primeng-datatable sonarqube-scan supervisord privileges

More Programming Questions

More Livestock Calculators

More Bio laboratory Calculators

More Cat Calculators

More Dog Calculators