0

I am using Angular 6 Pipe. I want to filter or search from a table field. I have loaded list data in to table. I have created a custom pipe file. I am able to filter or search a object like (applicant.vivaBoardId) from the list but can not able to filter or search from the object (applicant.studentInfo.formSerialNo or applicant.studentInfo.firstName) field inside the object.

// pipe filter code

@Pipe({
name: 'searchFilters'
})
export class IdAndNameFilterPipe implements PipeTransform {

  transform(items: any, filter: any, defaultFilter: boolean): any {
    if (!filter){
        return items;
    }

    if (!Array.isArray(items)){
        return items;
    }

    if (filter && Array.isArray(items)) {
        let filterKeys = Object.keys(filter);

        if (defaultFilter) {
            return items.filter(item =>
                filterKeys.reduce((x, keyName) =>
                    (x && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] == "", true));
        }
        else {
            return items.filter(item => {
                return filterKeys.some((keyName) => {
                    return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] == "";
                });
            });
        }
    }
}

}

// html code

    <input placeholder="Search by Form Sl" name="searchFilter [(ngModel)]="searchFilter">
    <table>
       <thead>
           <tr>
               <th>Form SL</th><th>Name</th><th>Viva Board Id</th><th>Viva Marks</th>
           </tr>
       </thead>
       <tbody>
           <tr *ngFor="let applicant of applicantListForVivaMarksEntry | searchFilters: {formSerialNo: searchFilter}">
               <td>{{applicant.studentInfo.formSerialNo}}</td>
               <td>{{applicant.studentInfo.firstName}}</td>
               <td>{{applicant.vivaBoardId}}</td>
            </tr>
        </tbody>
     </table>
monir
  • 47
  • 2
  • 2
  • 10
  • have you tried this? `` – Jon Fernandez Apr 03 '19 at 06:28
  • 1
    [You're not supposed to create pipes to filter, search or sort](https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe) –  Apr 03 '19 at 06:40
  • @JonFernandez i have tried this before but it went wrong. – monir Apr 03 '19 at 06:44
  • @monir your filter works by searching as soon as you type in the input right? – Jon Fernandez Apr 03 '19 at 08:17
  • @trichetriche and how would you filter the data then? – Jon Fernandez Apr 03 '19 at 08:43
  • @JonFernandez with a function/getter/bservable in the component, which then would not be generic, and would avoid the post-compilation issues created by minification and uglyfication. –  Apr 03 '19 at 08:52
  • @trichetriche do you have an example around there? Is allways good to learn new stuff :) – Jon Fernandez Apr 03 '19 at 09:31
  • 1
    That's literally the same code, but put into the component. You just don't use the `Object.keys` to iterate over the keys, but rather iterate over a defined object like `if (ob.prop1.includes(query) || ob.prop2.includes(query) || ... )`. Because once bundled, `prop1` will be transformed to `a`, meaning that your pipe `| filter:prop1` will not be able to find any value. –  Apr 03 '19 at 09:34
  • @trichetriche i did't get that. please give an example or a link to better understand. – monir Apr 03 '19 at 09:59
  • @JonFernandez yes – monir Apr 03 '19 at 10:00
  • here is my filter pipe, it works. https://stackoverflow.com/questions/55060545/angular-search-bar-in-typescript-table/55061017#55061017 – Jon Fernandez Apr 03 '19 at 10:28
  • @JonFernandez thanks for cooperation . but i did't get the solution from your example. – monir Apr 03 '19 at 14:39

0 Answers0