Angular ng-filter-search
This library was generated with Angular CLI version 9.0.7.
Description
A simple search filter for table, ul, li or div. we can use this filter for many place as possible.
Example:
syntax
filterArray = FilterSearchService.filterSearch(Searchvalue, ClonedArray, MappingArray);
Abbrevation
filterArray ==> Array to be filtered.
Searchvalue ==> String from input or textbox.
ClonedArray ==> Copy of filterArray for assigning.
MappingArray ==> Array which contain filterArray key to filter
Step1: Import FilterSearchModule to module.ts
; @
step2: Import FilterSearchService to component.ts
;;@ public productList: any; // productList must be a type any. public cloner = ; // searchMap is defined to which key to filter public searchMap = 'name' 'price' 'sellerName' 'status'; {// dummy productList you can load from your api thisproductList = id: 1 identity: 11012 name: 'Blue Headset' price: '$123' sellerName: 'Alibaba' count: 12 status: 'Sold Out' id: 2 identity: 31012 name: 'Red Shoe' price: '$143' sellerName: 'Amazon' count: 2 status: 'Available' id: 3 identity: 6423 name: 'Red Shoe' price: '$123' sellerName: 'FlipKart' count: 5 status: 'Available' id: 4 identity: 64123 name: 'Mi Phone' price: '$1123' sellerName: 'FlipKart' count: 11 status: 'Sold out' id: 5 identity: 86423 name: 'Redmi pro' price: '$223' sellerName: 'Alibaba' count: 9 status: 'Available' ; // this.cloner is important to copy your productList thiscloner = ...thisproductList; } : void // Use filterSearch() function to filter the data thisproductList = thisfs;
HTML
Table Filter search
<input placeholder="SEARCH FILTER" input="onSearchChange($event.target.value)" /><table><thead><tr><th>#</th><th>Product Identity</th><th>Product Name</th><th>Product Price</th><th>Seller Name</th><th>Total Count</th><th>Status</th></tr></thead><tbody>// productList is an array of an object from which we need to filter<tr *ngFor="let product of productList; let i = index"><td>i+1</td><td>productidentity</td><td>productname</td><td>productprice</td><td>productsellerName</td><td>productcount</td><td>productstatus</td></tr></tbody></table>
For list
<input placeholder="SEARCH FILTER" style="float:left" input="onSearchChange($event.target.value)" /> <ul> <li *ngFor="let product of productList">productname</li> </ul>
Issue
Don't forget to clone this.cloner = [...this.productList];
. Sometime [...array] is may not for dynamic rendering. In that case use like below example
thiscloner = ; // initialize as an array // copy the product list by this waythisproductList // passing index from an array like this.cloner[index] : void thisproductList = thisfs;