Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

How can I ignore a Property with Filter? #40

Open
Jonathan002 opened this issue Aug 1, 2018 · 4 comments
Open

How can I ignore a Property with Filter? #40

Jonathan002 opened this issue Aug 1, 2018 · 4 comments

Comments

@Jonathan002
Copy link

Jonathan002 commented Aug 1, 2018

How can I get the search filter to be selective about the object properties?

Example:

Data being iterated over:

[{
      name: "Jake",
      favoriteFood: "Banana"
},
{
      name: "Max",
      favoriteFood: "Pizza"
}]

Goal is to only have the searchFilter respond to name searches

Input -> "Ja"
Result ->[{
      name: "Jake",
      favoriteFood: "Banana"
}]

Input -> "Banana"
Result ->[]
@prashantpimpale93
Copy link
Contributor

prashantpimpale93 commented May 18, 2019

@Jonathan002 Currently this feature is not implemented but you can create your own custom pipe to achieve the same.

Custom Pipe (Create Angular Pipe):

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

@Pipe({
  name: 'deepFilter',
  pure: false
})
@Injectable()
export class Ng2DeepSearchPipe implements PipeTransform {

  /**
   * @param items object from array
   * @param term term's search
   */
  transform(items: any, term: string, props?: Array<string>): any {
    if (!term || !items) return items;

    return Ng2DeepSearchPipe.filter(items, term, props);
  }

  /**
   *
   * @param items List of items to filter
   * @param term  a string term to compare with every property of the list
   *
   */
  static filter(items: Array<{ [key: string]: any }>, term: string, columnsToExclude?: Array<string>): Array<{ [key: string]: any }> {

    const toCompare = term.toLowerCase();

    function checkInside(item: any, term: string) {
      for (let property in item) {
         if (columnsToExclude.some(x=> x === property)) {
          continue;
        }
        else {
          if (item[property] === null || item[property] == undefined) {
            continue;
          }
          if (typeof item[property] === 'object') {
            if (checkInside(item[property], term)) {
              return true;
            }
          }
          if (item[property].toString().toLowerCase().includes(toCompare)) {
            return true;
          }
        }
      }
      return false;
    }

    return items.filter(function (item) {
      return checkInside(item, term);
    });
  }
}

HTML Code:

deepFilter: searchtext: ['columns to exclude_1','columns to exclude_2']"

Here is a working demo:

https://stackblitz.com/edit/angular-t1dzgn-qtynmr

@prashantpimpale93
Copy link
Contributor

@aVolpe Do we need this feature? I mean is this required?

@aVolpe
Copy link
Collaborator

aVolpe commented May 22, 2019

@prashantpimpale93 maybe with the recent change to allow nested json this is needed.

Can you make a PR for this? I only suggest changing the some with includes and make it by default to [] (also add the parameter to the transform method).

@prashantpimpale93
Copy link
Contributor

@aVolpe yes will make a PR!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants