[英]How to create multiple array based on row of .CSV file in Angular?
我正在實現 Angular 模板來讀取.CSV 文件和生成表。 所以我創建了兩個.CSV 文件,一個用於 header,第二個用於表格內容。
對於標題 CSV 文件:header.csv
對於表 CSV 文件的數據:tableContent.csv
現在我能夠讀取所有數據並轉換為數組,但我正在進入一個數組。 我正在分享我的代碼以獲得更多理解。我已將.csv 文件放入 assets 文件夾中。
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>
我想創建多個 object 數組。 如下所示:
[{
"myAccess":["myAccess","Prod","URL","a@gmail.com","Enable"]
},
{
"System":["System","Environment","URL","a@gmail.com","Enable"]
},
{
"OAM":["OAM","DEV","URL","test@gmail.com","Enable"]
},
{
"Mylogin":["Mylogin","prod","URL","s@gmail.com","Enable"]
}]
表格標題將來自特定的 header.csv,數據將來自 tableContent.csv。 所以最后,如果我點擊特定的 header,它將搜索 json 對象(由 tablecontent.csv 讀取)。 將顯示特定的結果。 就像如果我將點擊 myAccess 這樣相關的 myaccess 數據顯示在表數據中。
在此先感謝,請分享您的想法。
這應該可以解決您的問題。 請比我更好地處理錯誤:D(引導程序用於樣式)
零件
import { Component, OnInit } from '@angular/core';
import { FileService } from './services/file.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
title = 'temp-app';
public headers = [];
public data = {};
public selectedHeader = null;
constructor(private fileSvc: FileService) {
}
ngOnInit(): void {
this.fileSvc.getHeaders().subscribe(
data => {
if (data != null && data.length > 0) {
let headers = data.split('\n');
headers = headers.filter(x => x.trim() !== '');
for (const item of headers) {
this.headers.push(item.trim());
}
} else {
this.headers = [];
}
}
);
this.fileSvc.getData().subscribe(
data => {
if (data != null && data.length > 0) {
const tempData = data;
let rows = [];
rows = tempData.split('\n');
for (let row of rows) {
if (row.trim() === '') {
continue;
}
row = row.replace('\r', '')
const rowSplits = row.split(',');
this.data[rowSplits[0]] = rowSplits;
}
} else {
this.data = {};
}
});
}
headerSeleced(header) {
this.selectedHeader = header;
}
}
服務
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class FileService {
constructor(private httpClient: HttpClient) {
}
public getHeaders() {
return this.httpClient.get('assets/header.csv', { responseType: 'text' });
}
public getData() {
return this.httpClient.get('assets/tableContent.csv', { responseType: 'text' });
}
}
樣品 html
<div class="container">
<div class="row">
<div class="col-sm-12">
<h3>Headers</h3>
</div>
<div *ngFor="let item of headers"
class="col-sm-3 bg-secondary p-2 m-1 text-white btn"
(click)="headerSeleced(item)">
{{item}}
</div>
</div>
<hr>
<div class="row">
<ng-container *ngFor="let item of data | keyvalue">
<ng-container *ngIf="selectedHeader == item.key" >
<div class="col-auto border">
<b>{{item.key}}</b>
</div>
<div *ngFor="let prop of item.value" class="col-auto border">
{{prop}}
</div>
</ng-container>
</ng-container>
</div>
</div>
export class AppComponent {
title = 'project';
myData: any;
myContent = [];
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.push({event.srcElement.name: data.split(",")});
let ab=this.myContent;
console.log("------------>",ab);
}
);
}
}
您需要在 app.component.ts 中添加這一行
this.httpClient.get('assets/content.csv', { responseType: 'text' }).subscribe(
data => {
// add this line before the split
data = data.replace(/[\r\n]/g,",");
this.headerData = data.split(",").filter(Boolean);
}
);
首先,我們將從 CSV 文件中獲取 header 數據
this.httpClient.get('assets/header.csv', { responseType: 'text' }).subscribe(
data => {
// add this line before the split
data = data.replace(/[\r\n]/g,",");
this.headerData = data.split(",").filter(Boolean);
}
);
為了得到想要的output,可以根據需要拆分陣列。
從您的 CSV 文件圖像中猜測,列長度現在將基於 this.headerData.length 這將是動態的
for( let index = 0; index < this.myContent.length; index ++) {
const value = this.myContent.splice(0,this.headerData.length);
// now you got the value you can create structure according to your need
}
閱讀有關Array.Splice()的更多信息,因為它有助於在創建子數組時分配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.