Angular 5 filter pipe

Issue

I have question about filter pipe angular 5.
Filter pipe works fine, but i have task to filter Date format.

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

@Pipe({
  name: "filter"
})
export class FilterPipe implements PipeTransform {
  transform(items: any[], searchText: string): any[] {
    if (!items) return [];
    if (!searchText) return items;
    searchText = searchText.toLowerCase();

    return items.filter(it => {
      return it.data.DatumRodjenja.toLowerCase().includes(searchText);
    });
  }
}

This is my date format: 01.01.1992. how to ignore dots (.) and search only typing numbers. My code works but i need to type dots when i search for something.

Solution

you can use replace and /\./g regexp, which fill find all dots and replace it with ''

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

@Pipe({
  name: "filter"
})
export class FilterPipe implements PipeTransform {
  transform(items: any[], searchText: string): any[] {
    if (!items) return [];
    if (!searchText) return items;
    searchText = searchText.toLowerCase();

    return items.filter(it => {
      return it.data.DatumRodjenja.toLowerCase().replace(/\./g,' ').includes(searchText);
    });
  }
}

Answered By – Artyom Amiryan

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.