• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

𝗩 Angular 5+ pipeline for array filtering.

Angular5+ Filter Pipe

downloads downloads npm version Greenkeeper badge PayPal donate button

Filter arrays

Angular 5+ pipeline for filtering arrays.

Demo Page

https://vadimdez.github.io/ngx-filter-pipe/

or see demo code

https://stackblitz.com/edit/ngx-filter-pipe

Usage

In HTML template
{{ collection | filterBy: searchTerm }}

Arguments

Param Type Details
collection array The collection to filter
searchTerm string or number or object or array or function Predicate used to filter items from collection

Install

npm install ngx-filter-pipe --save

For Angular lower than 5 use version 1.0.2

Setup

In case you're using SystemJS - see configuration here.

Usage

Import FilterPipeModule to your module

import { NgModule } from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent } from './app';
 
import { FilterPipeModule } from 'ngx-filter-pipe';
import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [BrowserModule, FormsModule, FilterPipeModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}

And use pipe in your component

import { Component } from '@angular/core';
 
@Component({
  selector: 'example-app',
  template: `
    <div>
        <input type="text" [(ngModel)]="userFilter.name" placeholder="name">
        <ul>
          <li *ngFor="let user of users | filterBy: userFilter">{{ user.name }}</li>
          
          <!-- in case you want to show empty message -->
          <li *ngIf="(users | filterBy: userFilter).length === 0">No matching elements</li>
        </ul>
    </div>  
  `
})
 
export class AppComponent {
  users: any[] = [{ name: 'John' }, { name: 'Jane' }, { name: 'Mario' }];
  userFilter: any = { name: '' };
}

$or matching

Use $or to filter by more then one values.

$or expects an Array.

In your component:

// your array
const languages = ['English', 'German', 'Russian', 'Italian', 'Ukrainian'];
// your $or filter
const filter = { $or: ['German', 'English'] };

In your template:

<div *ngFor="let language of languages | filterBy: filter">
  {{ language }}
</div>

Result will be:

<div>English</div>
<div>German</div>

$or example with nessted values

In your component:

// your array
const languages = [
  { language: 'English' },
  { language: 'German' },
  { language: 'Italian' }
];

// your $or filter
const filter = {
  language: {
    $or: ['Italian', 'English']
  }
};

In your template:

<div *ngFor="let object of languages | filterBy: filter">
  {{ object.language }}
</div>

Result:

<div>English</div>
<div>Italian</div>

$or example with multiple predicates

const objects = [
  { name: 'John' },
  { firstName: 'John' }
]

const filter = { $or: [{ name: 'John' }, { firstName: 'John' }] }

In your template:

<div *ngFor="let object of objects | filterBy: filter">
  {{ object | json }}
</div>

Result:

<div>{ name: 'John' }</div>
<div>{ firstName: 'John' }</div>

Use FilterPipe in a component

Inject FilterPipe into your component and use it:

class AppComponent {
  objects = [
    { name: 'John' },
    { name: 'Nick' },
    { name: 'Jane' }
  ];
  
  constructor(private filter: FilterPipe) {
    let result = this.filter.transform(this.objects, { name: 'J' });
    console.log(result); // [{ name: 'John' }, { name: 'Jane' }]
  }
}

Test

Run tests

npm test

Contribute

License

MIT Β© Vadym Yatsyuk

More Repositories

1

ng2-pdf-viewer

πŸ“„ PDF Viewer Component for Angular
TypeScript
1,301
star
2

Counter-Strike-JS

Counter Strike implementation with Javascript/Typescript
TypeScript
271
star
3

ngx-order-pipe

β–Ό Angular 5+ orderBy pipe
TypeScript
243
star
4

ngx-img-fallback

πŸ–Ό Load placeholder image on image error, Angular 5+
TypeScript
82
star
5

ngx-online-status

πŸ”› Angular 5+ Detect online/offline state
TypeScript
22
star
6

roguelike-dungeon-crawler

react.js game
JavaScript
18
star
7

spr-viewer

Online sprite (*.spr) viewer
TypeScript
12
star
8

watchstocks

Track stocks
JavaScript
11
star
9

Who-wants-to-be-millionaire

Who wants to be millionaire game made with MEAN (MongoDB, Express, Angularjs, Nodejs)
JavaScript
11
star
10

backend-starter

Backend starter template project
JavaScript
6
star
11

frontend-starter

Angular based frontend starter
TypeScript
5
star
12

pinterest

Pinterest clone
JavaScript
4
star
13

pomodoro-app

Pomodoro time management app for OS X
Swift
4
star
14

football-quiz

Mobile quiz app
JavaScript
3
star
15

Google-Translate-Bar-App

Google translate in bar app for MacOS
Swift
3
star
16

weather-action

GitHub Action to print get and print weather
JavaScript
3
star
17

ngx-g2

G2 Component for Angular >= 2
TypeScript
3
star
18

qa

hackathon project
JavaScript
2
star
19

vd-create-app

JavaScript
2
star
20

ecast

FutureLab hackathon (2017) project
Swift
2
star
21

pomodoro

Pomodoro time management app
JavaScript
2
star
22

freelance

Web platform for freelance
PHP
2
star
23

automotive

My research/learning project
2
star
24

WebAssembly

JavaScript
2
star
25

create-backend-app

cli tool for createing backend projects
EJS
2
star
26

pact-consumer-driven-testing

JavaScript
2
star
27

progressive-web-apps

JavaScript
1
star
28

ng2-pdf-viewer-electron-example

TypeScript
1
star
29

DES

Des in c++
C++
1
star
30

pomodoro-timer

Pomodoro timer
JavaScript
1
star
31

local-eye

Techfest hackathon project for MAN and FlixBus
JavaScript
1
star
32

apiprojects

JavaScript
1
star
33

github-actions-for-ci

JavaScript
1
star
34

clown_in_the_cloud

Sensor controlled robot
JavaScript
1
star
35

ilquando

PHP project based on Tonic RESTful Framework + Doctrine + Twig
PHP
1
star
36

react-game-of-life

game of life on react
JavaScript
1
star
37

hello-github-actions

Dockerfile
1
star
38

VadimDez

1
star
39

Learn

C++
1
star
40

a-shuttle

Hackathon project
TypeScript
1
star