Implementing an Angular template to read .CSV files and generate a table involves creating two separate files: one for the header and another for the table content.
For the header CSV file: header.csv
https://i.stack.imgur.com/ojMo6.png
For the table data CSV file: tableContent.csv
https://i.stack.imgur.com/kl6GF.png
I have successfully converted all the data into arrays. However, it is currently stored in a single array. Below is my code for better understanding. The .csv files are located in the assets folder.
app.component.ts
export class AppComponent {
title = 'project';
myData: any;
myContent: any;
constructor(private httpClient: HttpClient) { }
ngOnInit() {
this.httpClient.get('assets/header.csv', { responseType: 'text' }).subscribe(
data => {
this.myData = data.split("\n");
}
);
}
clicked(event) {
console.log(event.srcElement.name);
this.httpClient.get('assets/tableContent.csv', { responseType: 'text' }).subscribe(
data => {
this.myContent = data.split(",");
let ab=this.myContent;
console.log("------------>",ab);
}
);
}
}
app.component.html
<table id="customers">
<tr>
<th *ngFor="let dataheader of myData"><button (click)="clicked($event)" id={{dataheader}} name={{dataheader}}>{{dataheader}}</button></th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
<td>Germany</td>
</tr>
</table>
Browser screen https://i.stack.imgur.com/elUo8.png
The goal is to create multiple object arrays as shown below:
[{
"myAccess":["myAccess","Prod","URL","[email protected]","Enable"]
},
{
"System":["System","Environment","URL","[email protected]","Enable"]
},
{
"OAM":["OAM","DEV","URL","[email protected]","Enable"]
},
{
"Mylogin":["Mylogin","prod","URL","[email protected]","Enable"]
}]
The table heading will come from the specific header.csv file, while the data will be sourced from the tableContent.csv file. When clicking on a particular header, it should search within the JSON object read from tablecontent.csv, displaying the corresponding result. For example, clicking on "myAccess" should display the related myaccess data in the table.
Thank you in advance, would appreciate your insights.