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 Sort a table with data from a Array of complex JSON Object

Sort-Filter.pipe.ts

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

export class SortFilterPipe
 implements PipeTransform {
    key: string;
  
    transform(items: Array, 
   key: string, asc: boolean)
 {
  this.key = key;
 if (items)
  {
  if (asc)
   {
   return items.sort((item1: any, 
                    item2: any) => 
    {               
    if (!item2[this.key])
     return -1
    else if (!item1[this.key])
     return 1
    else if (item1[this.key] > 
            item2[this.key])
     return 1
    else if (item1[this.key] ===
           item2[this.key])
     return 0
    else
     return -1
   })
  }
  else
 {
  return items.sort((item1: any,
    item2: any) => 
 {                 
 if (!item2[this.key])
   return 1
 else if (!item1[this.key])
   return -1
 else if (item1[this.key] < 
          item2[this.key])
   return 1
 else if (item1[this.key] ===
         item2[this.key])
  return 0
 else
   return -1
  })
 }
}}}


Componentt.ts

import { sortFilterPipe } from '../../../shared/sort-filter.pipe'; export class component implements OnInit{ sorkKey: string; isAsc: boolean; sortedList; 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 sortFilter: sortFilterPipe) { } ngOnInit(){ //default sort key this.sortedList = this.data; this.sorkKey = "name"; this.sort(this.sortKey) } sort(key: string) { this.isAsc = !this.isAsc this.sorkKey = key; this.sortedList= this.sortFilter.transform(this.sortedList, this.sorkKey, this.isAsc); } }

component.html

<table> <th> <td (click)="sort('name')">Name</td> <td (click)="sort('age')" >Age</td> <td (click)="sort('sex')">Sex</td> <td (click)="sort('married')">Marrital Status& lt;/td> </th> <tr *ngFor="let item of sortedList"> <td> {{item.name}} </td> <td>{{item.age}} </td> <td>{{item.sex}}; </td> <td>{{item.married}} </td> </tr> </table>

Comments

  1. Learn the right skills to break into a Web development career and advance yourself as a full-stack Web Developer. This course will serve as a comprehensive introduction to various topics in Software Development. This course is a proper blend of theory and the practical hands-on session for each and every concept. Throughout the course, participants will work on a complete end-to-end tech stack to implement the concepts learned during the course.

    full stack development course in hyderabad
    full stack development Training in hyderabad

    ReplyDelete
  2. Angular 2/3/4/5/6/7 Sort a table with data from a Array of complex JSON Object

    Very informative post to learn...!

    Learners can try out this program to perform sorting...

    Check out our Software Testing Services Company

    best Software Testing Services Company

    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)

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