My custom pipe (filter) does not work in Angular 2

Michal Bialek

I have my own pipe created for filtering over any field in my table. When I put a string into search box it prints 'found' correctly in my console but no rows are displayed in the table. When I remove a pipe completely, everything works fine, table contains all records.

import { Component, Input, OnInit } from '@angular/core';
import { MyTableData } from '../interfaces/my-table-data.interface'

@Component({
    selector: 'my-table',
    template: `
    <div style="width: 100%">
        <div style="float: left; height: 50px; width: 100%">
            Search: <input type="text" [(ngModel)]="filterValue" style="height: 30px; border: 1px solid silver"/>
        </div>
        <div style="float: left; width: 100%">
            <table>
                <tr *ngFor="let row of data.rows | myTableFilter:filterValue">
                    <td *ngFor="let value of row">{{value}}</td>
                </tr>
            </table>
        </div>
    </div>
    `
})
export class MyTableComponent implements OnInit { 
    @Input() data: MyTableData;
    filterValue: string;

    ngOnInit() {

    }
}  

and pipe:

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

@Pipe({
    name: 'myTableFilter',
    pure: false
})
@Injectable()
export class MyTableFilterPipe implements PipeTransform {
    transform(items: any[], arg: string): any {
        if(arg == '') return true;

        items.filter(item => {
            for(let value of item) {
                if(String(value).indexOf(arg) !== -1) {   
                    console.log('found match') //it prints correctly
                    return true;
                }
            }
            return false;
        });
    };
}

Regards

Poul Kruijt

A pipe is a pipe, and not really a filter. It's used to alter the input. Your input in this case is the array data.rows and you want to only show rows matching a certain input argument filterValue. In that case you want to return the filtered array, and not a true or false value. On the other hand, you should really keep your pipe pure, because.. this is a pure pipe. It only changes if the input changes (filterValue):

@Pipe({
    name: 'myTableFilter'
})
@Injectable()
export class MyTableFilterPipe implements PipeTransform {
    transform(items: any[], filterValue: string): any {
        if(!items) {
          return [];
        }
        if(!filterValue) {
          return items;
        }
        return items.filter(item => {
            for(let value of item) {
                if(String(value).indexOf(filterValue) !== -1) {   
                    return true;
                }
            }              
        });
    };
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

The pipe ' ' could not be found angular2 custom pipe

From Dev

RouteConfig does not seem to work in my angular 2 project

From Dev

Why does my template not work in angular2 component?

From Dev

Angular 2 pipe/filter with component variable

From Dev

Angular2 use basic pipe in custom pipe

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular 2 Input Custom Pipe

From Dev

Angular2 Pipe filter by name best approach

From Dev

Filter object array with pipe Angular 2

From Dev

my angular app does not work

From Dev

Angular 2 pipe/filter with component variable

From Dev

angular2 case insensitive filter using pipe?

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular 2 pipe to filter grouped arrays

From Dev

Angular2 filter pipe on input

From Dev

Find and filter pipe for all content of Array as JSON format Angular 2

From Dev

Why does my filter function in Bash not work?

From Dev

Angular 2 Search Pipe filter objects with array property

From Dev

Angular2 Filter Array of Objects with pipe using RegExp?

From Dev

Angular 2 custom pipe not found

From Dev

Angular2 filter table based on two inputs using custom pipe

From Dev

Custom Pipe product filter?

From Dev

Why date pipe with input type text does not work with angular2?

From Dev

Angular2 pipe does filter but doesn't show at all

From Dev

Angular select does not filter my ngRepeat

From Dev

Angular2 filter/pipe using radio buttons to filter

From Dev

Angular filter pipe doesn´t work

From Dev

titlecase pipe does not work with ternary operator angular2

From Dev

My function does work when I click the button (Angular 2)

Related Related

HotTag

Archive