4

enter image description here

Here I have folderObjs array and Console of this array is in below code, I put search input field and I want to make search for this array by folderName and folderSize in angular how it is possible?

HTML

<mat-form-field>
 <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search">
</mat-form-field>

<div *ngFor="let folder of folderObjs">
  <span>{{folder.folderName}}</span>
  <span>{{folder.folderSize}}</span>
</div>

TS

folderObjs : Folder[] = [];

applyFilter(filterValue) {
  console.log(this.folderObjs);
// 0: {folderid: 781, folderName: "pelu folder",folderActivity: "true", …}
   1: {folderid: 782, folderName: "biju folder", folderActivity: "true", …}
   filter: "d"
   length: 2

  this.folderObjs.filter = filterValue.trim().toLowerCase();
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Dharmesh
  • 5,383
  • 17
  • 46
  • 62

4 Answers4

11

Create custom pipe

filter.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';  
@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {
  transform(value: any, searchValue): any {
    if (!searchValue) return value;
    return value.filter((v) => 
    v.name.toLowerCase().indexOf(searchValue.toLowerCase()) > -1 || 
    v.size.toLowerCase().indexOf(searchValue.toLowerCase()) > -1)

  }

}

component.ts

<input type="search"  [(ngModel)]="search">

<div *ngFor="let f of (folderObjs | filter:search)as folder">
  {{f.name}}
  {{f.size}}    
</div>

<div *ngIf="(folderObjs | filter:search).length <1">
  Not Found
  </div>

Example:https://stackblitz.com/edit/angular-mrgzw3

Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
1
const folderObjs = [
    {name: "Jane", lastName: "Doe"},
    {name: "john", lastName: "Snow"},
    {name: "Tyrion", lastName: "Lannister"},
    {name: "Lorem", lastName: "Ipsum"},
]

applyFilter(filterValue) {
 folderObjs.filter((val) => {
    return val.name.toLowerCase().indexOf(filterValue) > -1;
 });
}

above code will filter the data based name property.

applyFilter(filterValue, filterBy) {
 folderObjs.filter((val) => {
    return val[filterBy].toLowerCase().indexOf(filterValue) > -1;
 });
}

Can pass filterBy Option as well, Hope this helps

Akshay Rajput
  • 1,978
  • 1
  • 12
  • 22
  • I got filtered array but how to hide other arrays – Dharmesh Dec 14 '18 at 06:33
  • 1
    @Ankit if you want to do it in a clean way, create a filter pipe from my code, apply the pipe in front of
    the pipe will return the filtered element and the dom will update the list accordingly, if you need help creating a filter pipe, i will help you
    – Akshay Rajput Dec 14 '18 at 06:45
1

Ideally you can use either pipes or filter() to achieve this.

sortedArray : Folder[] = [];  

applyFilter(val: string) {

  this.sortedArray = this.folderObjs.filter(x =>
    x.folderName.toLowerCase().indexOf(val.trim().toLowerCase()) !== -1 || 
    x.folderSize.toLowerCase().indexOf(val.trim().toLowerCase()) !== -1)
}
Ethan
  • 29
  • 1
1

This will work, first take a copy of the folderObjs to folderObjCopy since when search value is empty we must repopulate all the values.

applyFilter(filterValue) {
 let val = filterValue.toLowerCase();
 let searchColumns = ['folderName','folderSize'];
 if (val.length > 0) {
 this.folderObjs = this.folderObjCopy.filter(function (d) {
          let matchFound = false;
          for (let data of search_columns) {
            let value = "" + d[data];
            if (value.toLowerCase().indexOf(val) !== -1 || !val) {
              matchFound = true;
              break;
            }
          }
          return matchFound;
        });
 } else {
        this.folderObjs = this.folderObjCopy;
   }
 }

Note : There will be much more optimised solutions, this is just my implementations. Please let me know if this doesn't work

Pavan Skipo
  • 1,757
  • 12
  • 29