ng2-filter-pipe

<h1 align="center">Angular2+ Filter Pipe</h1>

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
ng2-filter-pipe
0.1.107 years ago8 years agoMinified + gzip package size for ng2-filter-pipe in KB

Readme

Angular2+ Filter Pipe

<img src="https://img.shields.io/npm/dm/ng2-filter-pipe.svg?style=flat" alt="downloads">

<img src="https://travis-ci.org/VadimDez/ng2-filter-pipe.svg?branch=master" height="18">

<img src="https://badge.fury.io/js/ng2-filter-pipe.svg" alt="npm version" height="18">

<img src="https://david-dm.org/vadimdez/ng2-filter-pipe.svg" height="18">


Filter arrays
Angular 2+ pipeline for filtering arrays.

Demo Page

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

Install

npm install ng2-filter-pipe --save

Usage

In case you're using ``systemjs`` - see configuration here.
Import Ng2FilterPipeModule to your module
import { NgModule } from '@angular/core';
import { BrowserModule  } from '@angular/platform-browser';
import { AppComponent } from './app';
 
import { Ng2FilterPipeModule } from 'ng2-filter-pipe';
 
@NgModule({
  imports: [BrowserModule, Ng2FilterPipeModule],
  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: stringFilter).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:
<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>

Test

Run tests
npm test

License

MIT © Vadym Yatsyuk