Angular 2/4/5/6/7-Custom Table component with Sort,Filter,Pagination with data from a complex JSON Array-Complete Guide(code snippet)

Image
Table GUI //step 1 install ngb bootstrap npm package npm install --save @ng-bootstrap/ng-bootstrap main.module.ts import { NgbPaginationModule} from '@ng-bootstrap/ng-bootstrap/'; @NgModule({ declarations:[], imports: [ NgbPaginationModule.forRoot() ] }) sample.component.html <div class="row" style="padding-top:3px;"> <div class="col-sm-5"> <label class="control-label"> Records per page <label> <input [(ngModel)]="itemsPerPage" style="width:50px" class="form-control" type="text"> </label> </label> <label class="control-label"> Total records: <label class="info-text"> {{TotalCount}} </label> </label> </div> <div *ngIf="count > itemsPerPage" class="col-sm-7 "> <div class=" pull-right&q

Angular 2/3/4/5/6/7 Filter a table based on multiple fields from a Array of complex JSON Object

Search-Filter.pipe.ts

import { Pipe, PipeTransform } 
from '@angular/core';
@Pipe({ name: 'searchFilter' })

export class SearchFilterPipe implements 
PipeTransform {
transform(items: Array,
filter: { [key: string]: any }): Array {
if (items) {
return items.filter(item => {
let match: boolean = true;
for (var k in filter) {
if (filter[k] && filter[k] != '') {
 if (typeof (item[k]) == "boolean") {
  let val: boolean = (filter[k] == "true");
   if (item[k] != val)
    {
    match = false;
    }
   }                       
   else {
     if (item[k] && item[k] !="" && 
     item[k].toString().toLowerCase().indexOf
     (filter[k].toString().
      toLowerCase()) == -1)
      match = false;
        }                    
       }
     }
 return match;
    });
   }
 }
}

Componentt.ts

import { SearchFilterPipe } from '../../../shared/search-filter.pipe'; export class component implements OnInit{ filterKeys: any; filteredList; data = [{ name: 'Name1', age: 20, sex: 'M', married: false} { name: 'Name2', age: 30, sex: 'F', married: true}, { name: 'Name3', age: 22, sex: 'F', married: false}, { name: 'Name4', age: 25, sex: 'M', married: true} , ] constructor(private searchFilter: SearchFilterPipe) { } ngOnInit(){ this.filterKeys = { name: '', age: 0, sex: '', married: null }; } filter() { this.filteredList= this.searchFilter. transform (this.data, this.filterKeys); this.count = this.filteredList.length; } }

component.html

<table> <th><td>Name</td> <td>Age</td> <td>Sex</td> <td>Marrital Status</td> </th> <tr> <td> <input type='text' [(ngModel)]="filterKeys.name" (change)="filter()"></td> <td> <input type='text' [(ngModel)]="filterKeys.age" (change)="filter()"></td> <td> <input type='text' [(ngModel)]="filterKeys.sex" (change)="filter()"></td> <td> <input type='text' [(ngModel)]="filterKeys.married" (change)="filter()"></td> </tr> </table>

Comments

  1. Angular 2/3/4/5/6/7 Filter a table based on multiple fields from a Array of complex JSON Object

    Good to know about this...! Will definitely try this....

    Learn About the Key Benefits & Use Cases of Angular Development Services

    Angular Development Services

    ReplyDelete

Post a Comment

Popular posts from this blog

Angular 2/4/5/6/7-Custom Table component with Sort,Filter,Pagination with data from a complex JSON Array-Complete Guide(code snippet)

Angular 2/4/5/6/7-Reusable Custom Bootstrap Modal Dialog Angular Component(3 ways)