DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Angular Component Tree With Tables in the Leaves and a Flexible JPA Criteria Backend
  • Using Bing Maps to add Shapes With Angular in a Spring Boot Application
  • Bing Maps With Angular in a Spring Boot Application
  • Connecting Angular to an SQL database

Trending

  • Continuous Integration vs. Continuous Deployment
  • Using Open Source for Data Integration and Automated Synchronizations
  • Software Verification and Validation With Simple Examples
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  1. DZone
  2. Data Engineering
  3. Databases
  4. Implementing an Angular Table Filter

Implementing an Angular Table Filter

It's no secret that tables are popular, so it would help if you could create a filter for them. This guide walks you through the creation of a table filter in Angular 2.

Fabian Gosebrink user avatar by
Fabian Gosebrink
·
Jan. 02, 17 · Tutorial
Like (1)
Save
Tweet
Share
33.57K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, I want to show you how to implement a table filter in Angular.

The code can be found on GitHub.

A table in your application is maybe one of the most used controls. So, logic dictates that a filter for the table is pretty handy.

Image title


 You can achieve this using a pipe:

import { Pipe, PipeTransform, Injectable } from '@angular/core';

@Pipe({
    name: 'filter'
})

@Injectable()
export class FilterPipe implements PipeTransform {
    transform(items: any[], field: string, value: string): any[] {
        if (!items) {
            return [];
        }
        if (!field || !value) {
            return items;
        }

        return items.filter(singleItem => singleItem[field].toLowerCase().includes(value.toLowerCase()));
    }
}


This pipe takes an array if items and checks if the field which is also a parameter on a single items contains the value the user types. It returns the array of matching items.

The Pipe is available through the name “filter”.

After implementing this, the pipe has to be registered on a module to make it available in our application. Could be your application module or if you have one, a shared module. In case of the shared one: Do not forget to export it. 

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

// ...

import { FilterPipe } from '../pipes/filter.pipe';

@NgModule({
    imports: [
        // Modules
        BrowserModule
    ],

    declarations: [

        // Components & directives
        FilterPipe
    ],

    providers: [
        // Services
    ],

    exports: [
        // ...
        FilterPipe
    ]
})

export class SharedModule { }


AppModule:

import { NgModule } from '@angular/core';
// ...
import { SharedModule } from './modules/shared.module';

// ...


@NgModule({
    imports: [
        // ...
        SharedModule
    ],

    declarations: [
        // ...
    ],

    providers: [
        // ...
    ],

    bootstrap: [AppComponent]
})

export class AppModule { }


In the template, you have to add an input to a form to display a field to the user where the searchstring can be typed. After this, the pipe has to be applied and the searchstring has to be databound in the template. 

import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
// ...

@Component({
    selector: 'foodList',
    templateUrl: 'app/components/foodList/foodList.component.html'
})

export class FoodListComponent {
    public foodItem: FoodItem;
    public searchString: string;

    // ...
}


<form>
    <div class="form-group">
        <div class="input-group">
            <div class="input-group-addon"><i class="glyphicon glyphicon-search"></i></div>
            <input type="text" class="form-control" name="searchString" placeholder="Type to search..." [(ngModel)]="searchString">
        </div>
    </div>
</form>

<table class="table">
    <tr>
        <th>Name</th>
        <th>Calories</th>
        <th class="text-right">Actions</th>
    </tr>
    <tr *ngFor="let food of foods | filter : 'name' : searchString; let i = index">
        <td class="text-left">
            {{food.name}}
        </td>
        // ...
    </tr>
</table>

 

Now the table is filtered after the field “name” by the string which is typed into the searchString-input. 

Image title


Database Filter (software) AngularJS

Published at DZone with permission of Fabian Gosebrink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Angular Component Tree With Tables in the Leaves and a Flexible JPA Criteria Backend
  • Using Bing Maps to add Shapes With Angular in a Spring Boot Application
  • Bing Maps With Angular in a Spring Boot Application
  • Connecting Angular to an SQL database

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: