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

Does not work with Nested JSON Object #45

Closed
prashantpimpale93 opened this issue May 17, 2019 · 4 comments
Closed

Does not work with Nested JSON Object #45

prashantpimpale93 opened this issue May 17, 2019 · 4 comments

Comments

@prashantpimpale93
Copy link
Contributor

prashantpimpale93 commented May 17, 2019

I have JSON object like:

[
  {
    firstName: "S",
    lastName: "P",
    address: {
      state: "Maharashtra",
      city: "Satara",
      
    }
  },
  {
    firstName: "A",
    lastName: "C",
    address: {
      state: "Maharashtra",
      city: "Satara",
      
    }
  }
]

So I can able to search the name A but If typed Satara it does not matches the nested object (As per your pipe code from GitHub)

Expected Result:

I should be able to see both records if I typed Satara

Link to Reproduce:

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

@aVolpe
Copy link
Collaborator

aVolpe commented May 17, 2019

You are rigth, the scope of this pipe doesnt include nested objects, but you can make a small modification to make it recursive, for example:

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): any {
    if (!term || !items) return items;

    return Ng2DeepSearchPipe.filter(items, term);
  }

  /**
   *
   * @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): Array<{ [key: string]: any }> {

    const toCompare = term.toLowerCase();

    function checkInside(item: any, term: string) {
      for (let property in item) {
        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);
    });
  }
}

Here a working example: https://stackblitz.com/edit/angular-t1dzgn-2bhkja

@prashantpimpale93
Copy link
Contributor Author

@aVolpe Yes, did implement with custom pipe! But it's better if you add that in code by taking pull request!

@aVolpe
Copy link
Collaborator

aVolpe commented May 17, 2019

Yes! please make a PR!

@prashantpimpale93
Copy link
Contributor Author

@aVolpe Will do it. Thanks!

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

No branches or pull requests

2 participants