I have an HTML page in which I want to create a table. The columns in this table are dynamic which means that they are fetched from the server into a variable in the compone
It can be used in Angular 2, 5, 6, 7. We should create a component, so that it can be used anytime, with any kind of data. I have tested it and is working.
File : dynamic-table.component.ts
import { Component, OnInit, Input, Output, OnChanges, SimpleChanges } from '@angular/core';
@Component({
selector: 'dynamic-table',
templateUrl: './dynamic-table.component.html',
styleUrls: ['./dynamic-table.component.scss']
})
export class DynamicTableComponent implements OnInit, OnChanges {
@Input() tableHeads: Array = new Array();
@Input() tableDatas: Array = new Array();
@Input() tableColName: Array = new Array();
private tableColNameGenerated: Array = new Array();
private isTableColNameSet: Boolean = false;
constructor() { }
ngOnInit() {
}
ngOnChanges(changes: SimpleChanges) {
if (changes['tableHeads']) {
if (this.tableHeads.length > 0) {
// console.log('tableHeads');
}
}
if (changes['tableDatas']) {
if (!this.isTableColNameSet) {
if (this.tableDatas.length > 0) {
this.tableColNameGenerated = this.getKeys(this.tableDatas[0]);
if (!this.isHeadAndColLengthSame(this.tableHeads, this.tableColNameGenerated)) {
console.error('Table column row is not same as with property name in self generated');
}
}
}
}
if (changes['tableColName']) {
if (this.tableColName.length > 0) {
this.tableColNameGenerated = this.tableColName;
this.isTableColNameSet = true;
if (!this.isHeadAndColLengthSame(this.tableHeads, this.tableColName)) {
console.error('Table column row is not same as with property name provided');
}
}
}
}
/**
* This method will fetch all the property name and convert it into a list of String.
* @param {Array} head Pass in the list of String, which contains table header values
* @param {Array} col Pass in the list of String, which contains column property
* name, which was received from Input or generated using this.getKeys()
*/
private isHeadAndColLengthSame(head: Array, col: Array): Boolean {
return (head.length === col.length);
}
/**
* This method will fetch all the property name and convert it into a list of String.
* @param {any} value Pass Instance of Object eg. new User()
*/
private getKeys(value: any): Array {
return Object.keys(value);
}
}
File : dynamic-table.component.html
{{tableHead}}
{{tableData[colName]}}
To implement this component call in some view file
File : view-table.component.html
File : view-table.component.ts
export class ViewTable implements OnInit{
// fetch or create an Object of UserDetails type and pass it to dynamic-table
private userDetails: Array;
// required to provide the table header, you can call an api or hard code the column name.
private tableHead: Array;
// optional, you can hard code the property name or just send the data of an object and dynamic-table component will figure out.
private tableColName: Array;
constructor(){
this.tableHead = new Array('Name', 'Age', 'Gender');
// this.tableColName = new Array('name', 'age', 'gender');
this.userDetails = new Array();
}
ngOnInit() {
this.userDetails.push(new UserDetails('Apple', 18, 'Male'));
this.userDetails.push(new UserDetails('Banana', 24, 'Female'));
this.userDetails.push(new UserDetails('Mango', 34, 'Male'));
this.userDetails.push(new UserDetails('Orange', 13, 'Female'));
this.userDetails.push(new UserDetails('Guava', 56, 'Male'));
}
}
export class UserDetails{
constructor(public name: String, public age: Number, public gender: String) { }
}