What is the use of a custom table?
Let’s consider we are developing an application which is having multiple pages with grids. You can design it as a component and use it as Child View. There are a few benefits to this approach as mentioned below.
- Custom table is a reusable component.
- Easy to use and maintain.
- Rapid development.
- Reliable and extendable.
Example:
- <custom-table [records]="datas"
- [edit]="true" (childEvent)="openModal($event)">
- </custom-table>
Here, we can call a custom table component by passing the record set in our Views as per the view requirement. Let’s start from scratch.
First, let us create a component for the custom table. We will create “table-layout.component.ts”.
- import { Component, Input, OnChanges, EventEmitter, Output } from '@angular/core';
- @Component({
- selector: 'custom-table',
- templateUrl: './table-layout.component.html'
- })
- export class TableLayoutComponent implements OnChanges {
- @Input() records: any[];
- keys: string[];
- ngOnChanges() {
- if (this.records)
- this.keys = Object.keys(this.records[0]);
- }
- }
Now, we will create the “table-layout.component.html” file.
- <table class="table">
- <thead>
- <tr>
- <th (click)="sort(key)" *ngFor="let key of keys">{{ key }}</th>
- </tr>
- </thead>
- <tbody>
- <tr *ngFor="let record of records">
- <td *ngFor="let key of keys">{{ record[key] }}</td>
- </tr>
- </tbody>
- </table>
Next, we have to add the reference to app module or the module where we want to use this custom table. Since I am using custom table in the app component, I am adding these references to the app module.
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { FormsModule, ReactiveFormsModule } from '@angular/forms';
- import { AppComponent } from './app.component';
- //import { CustomTableModule } from './components/custom.components/custom.table/table-layout.module';
- import { TableLayoutComponent } from './components/custom.components/custom.table/table-layout.component';
- import { FilterPipe } from './components/custom.components/custom.table/filter.pipe';
- import {DataService} from './service';
- @NgModule({
- declarations: [
- AppComponent,
- TableLayoutComponent,
- FilterPipe
- ],
- imports: [
- BrowserModule,
- FormsModule,
- ReactiveFormsModule
- ],
- providers: [DataService],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
For searching, we will use pipes. We will write custom pipes for searching.
Reference Link
https://angular.io/guide/pipes
Pipes are used to transform the data when we only need that data transformed in a template.
Custom Filter pipe
- import { Pipe, PipeTransform, Injectable } from '@angular/core';
- @Pipe({
- name: 'filter'
- })
- @Injectable()
- export class FilterPipe implements PipeTransform {
- transform(items: any[], field: string, text: string): any[] {
- if (!items) {
- return [];
- }
- else if (!text) {
- return items;
- }
- else {
- let keys = Object.keys(items[0]);
- //let type =
- let filteredItems = new Array<typeof items[0]>();
- items.forEach(item => {
- for (var i = 0; i < keys.length; i++) {
- if (item[keys[i]].toString().toLowerCase().includes(text.toLowerCase())) {
- filteredItems.push(item);
- break;
- }
- }
- });
- return filteredItems;
- }
- }
- }

Sarathlal SaseendranPosted Aug 28, 2018, 2:33 AM
Nice article.