-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildDynamicTable.tsx
94 lines (81 loc) · 3.09 KB
/
BuildDynamicTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Build Dynamic Table - From any WebApi or Local Json File Using AXIOS Methods
import * as React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom';
import axios from 'axios';
import _ from 'lodash';
export interface IState {
apiurl: string;
datarecords: any[];
datacolumns: any[];
}
class BuildDynamicTable extends React.Component<RouteComponentProps<any>, IState> {
constructor(props: RouteComponentProps) {
super(props);
this.state = {
datarecords: [],
datacolumns: [],
}
}
public componentWillMount(): void {
const api_url = "http://dummy.restapiexample.com/api/v1/employees";
axios.get(api_url).then(response => {
this.setState({datarecords: response.data});
this.extractColumnNames();
})
}
private extractColumnNames()
{
const firstrecord = _.keys(this.state.datarecords[0]);
this.setState({datacolumns: firstrecord,});
}
private displayRecords(key: number) {
const datacolumns= this.state.datacolumns;
return datacolumns.map((each_col) =>
this.displayRecordName(each_col,key)
)
}
private displayRecordName(colname:string, key:number){
const record = this.state.datarecords[key];
return <th>{record[colname]}</th>
}
private Capitalize(str: string){
const str_t = str.toUpperCase();
const str_tt = str_t.replace("_", " ");
return str_tt;
}
public render() {
const datarecords = this.state.datarecords;
const each_datarecord_keys = this.state.datacolumns;
return (
<div>
{datarecords.length === 0 && (
<div className="text-center">
<h2>No datarecords found at the moment</h2>
</div>
)}
<div className="container">
<div className="row">
<table className="table table-bordered">
<thead className="thead-light">
<tr>
{each_datarecord_keys && each_datarecord_keys.map(each_datarecord_key =>
<th scope="col">{this.Capitalize(each_datarecord_key)}</th>
)}
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{datarecords && datarecords.map((each_datarecord, recordindex) =>
<tr key={each_datarecord.id}>
{this.displayRecords(recordindex)}
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
)
}
}
export default BuildDynamicTable;