[英]How to populate data on UI after reading from CSV file in Angular?
I am implementing angular template and I am reading CSV file data to show it in an organized table.我正在实现 angular 模板,我正在读取 CSV 文件数据以在有组织的表格中显示它。 I am not strong at core scripting part based on retrieved csv data.
基于检索到的 csv 数据,我不擅长核心脚本部分。
I am sharing my code:我正在分享我的代码:
export class AppComponent implements OnInit{
title = 'temp-app';
public headers = [];
public data = {};
public strData = ''
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());
}
this.headers=[...new Set(this.headers)];
} else {
this.headers = [];
}
}
);
Service.ts服务.ts
@Injectable({
providedIn: 'root'
})
export class FileService {
constructor(private httpClient: HttpClient) {
}
public getHeaders() {
return this.httpClient.get('assets/data.csv', { responseType: 'text' });
}
}
Above code is not correct: I am sharing UI, What I am accepting.上面的代码不正确:我正在共享 UI,我正在接受什么。
Expected预期的
1. Code first should read the column 1 ie "AppName" and after reading all the rows of column 1 it should only keep the unique values and create a button using those unique values.Means - if "LDAP" is multiple times in column 1, it should only consider "LDAP" only once and using that it should create a button. 1.代码首先应该读取第1列,即“AppName”,在读取第1列的所有行之后,它应该只保留唯一值并使用这些唯一值创建一个按钮。意味着 - 如果“LDAP”在第1列中多次出现,它应该只考虑一次“LDAP”并使用它应该创建一个按钮。 2. Similarily, create button for all remaining values of column 1.
2. 同样,为第 1 列的所有剩余值创建按钮。
For reference purpose one related link here:为参考目的,这里有一个相关链接:
https://stackblitz.com/edit/angular-ivy-ubcknl
https://stackblitz.com/edit/angular-ivy-ubcknl
Thanks in advance In future I want to show respective value only in column.提前致谢 将来我只想在列中显示相应的值。 If i click on OAM then OAM column will show, If I will click on LDAP the LDAP column value will show.
如果我单击OAM ,则会显示 OAM 列,如果我单击 LDAP,则会显示 LDAP 列值。
lets say you are getting this data from assets folder.假设您从资产文件夹中获取此数据。 For eg:-
例如:-
return this.httpClient.get('assets/tableContent.csv', { responseType: 'text' }).pipe((map(res)=> {
let columns= new Set(response.split('\n').map((item) => item.replace('\r', '').split(",")[0]));
let rows= response.split('\n');
let result obj = {
uniqueColumns: Array.from(columns),
data: {}
}
columns.forEach((columnName) => {
obj.data['columnName'] = [];
});
rows.forEach((row) => {
var rowItems = row.replace('\r', '').split(",");
obj.data[rowItems[0]].push(rowItems.slice(1));
});
return obj;
}));
When you will subscribe to it you will get a object like below:-当您订阅它时,您将获得 object,如下所示:-
{
uniqueColumns: ['OAM','LDAP', 'myAccess', 'OAM', 'myLogin'],
data: {
'OAM': [
['DEV', 'OHS', 'Success'],
['Stage', 'OHS', 'Success']
],
'LDAP': [
['DEV','MDS','FAIL'],
['DEV','DDNET','FAIL']
//and other data
]
//same for other columns
}
}
with the above data structure you can easily create your template using ngfor directive.使用上述数据结构,您可以使用 ngfor 指令轻松创建模板。
Eg:- Template Code:-例如:- 模板代码:-
<table id="mainTable">
<thead>
<tr>
<th *ngFor="let head of items.uniqueColumns">{{head}}</th>
<tr>
</thead>
<tbody>
<tr>
<td *ngFor="let head of items.uniqueColumns">
<table id="columns">
<tr>
<td></td>
<td *ngFor="let col of items.data[head]">
<ng-container *ngFor="let items of col">
<td >{{items}}</td>
</ng-container>
</td>
<tr>
<tr>
<td>Dev</td>
</tr>
<tr>
<td>Stage</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
CSS:- CSS:-
table#mainTable,table#columns, th {
border: 1px solid black;
}
#columns td {
padding: 1em;
}
#columns>tr>td:not(:first-child) {
border: 1px solid black;
}
#columns>tr>td:nth-child(2) {
border-left: none;
}
#columns>tr:nth-child(1) {
border-bottom: 1px solid black;
}
tr {
padding: 1em;
}
table, td, th {
border-collapse: collapse;
}
Typescript:- Typescript:-
import { Component } from '@angular/core';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
public items = {
uniqueColumns: ['OAM','LDAP', 'myAccess', 'OAM', 'myLogin'],
data: {
'OAM': [
['DEV', 'OHS', 'Success'],
['Stage', 'OHS', 'Success']
],
'LDAP': [
['DEV','MDS','FAIL'],
['DEV','DDNET','FAIL']
//and other data
]
//same for other columns
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.