I can successfully upload a (non-empty) CSV file, and the table responds to fit the size of the dataset. However the table represents the CSV file as empty. Is there something I am possibly missing here?
I am using Angular 12, Npm 9, and I am using ngx-csv-parser 0.0.7
Could this be an issue with the particular CSV file, or with the code? Please give grace as I am somewhat new to this.
I am happy to clarify any unclear details and provide code snippets as needed.
app.component.ts:
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
import { Component, ViewChild } from '@angular/core';
import { NgxCsvParser, NgxCSVParserError } from 'ngx-csv-parser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
public feedbackArray: FormArray;
public feedbackForm: FormGroup;
constructor(private fb: FormBuilder) {
this.feedbackForm = this.fb.group({
feedbackArray: this.fb.array([ this.createFeedback() ]),
});
}
get feedbackControls() {
return this.feedbackForm.get('feedbackArray')['controls'];
}
createFeedback(): FormGroup {
return this.fb.group({
feedback: '',
});
}
removeFeedback(i: number) {
this.feedbackArray.removeAt(i);
}
addFeedback(): void {
this.feedbackArray = this.feedbackForm.get('feedbackArray') as FormArray;
this.feedbackArray.push(this.createFeedback());
}
}
export class CsvParser {
csvRecords: any[] = [];
constructor(private ngxCsvParser: NgxCsvParser) {}
@ViewChild('fileImportInput') fileImportInput: any;
fileChangeListener($event: any): void {
const files = $event.srcElement.files;
this.ngxCsvParser.parse(files[0], { header: true, delimiter: ',' })
.pipe().subscribe((result: Array<any>) => {
console.log('Result', result);
this.csvRecords = result;
}, (error: NgxCSVParserError) => {
console.log('Error', error);
});
}
}
Table in my app.component.html:
<div class="panel panel-default" id="main-right-panel">
<div class="csv-file-chooser-section file-select">
<label>Upload CSV File: </label>
<input
type="file"
#fileImportInput
name="File Upload"
id="csvFileUpload"
(change)="fileChangeListener($event)"
accept=".csv"
data-testid="importCSV"/>
</div>
<table>
<thead>
<tr>
<th>#</th>
<th>Identifier</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Status</th>
<th>Grade</th>
<th>Maximum Grade</th>
<th>Grade can be changed</th>
<th>Last modified submission</th>
<th>Online text</th>
<th>Last modified grade</th>
<th>Feedback comments</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let csvData of csvRecords;let i = index;">
<td>{{i+1}}</td>
<td>
<span>{{csvData.identifier}}</span>
</td>
<td>
<span>{{csvData.fullName}}</span>
</td>
<td>
<span>{{csvData.emailAddress}}</span>
</td>
<td>
<span>{{csvData.status}}</span>
</td>
<td>
<span>{{csvData.grade}}</span>
</td>
<td>
<span>{{csvData.maximumGrade}}</span>
</td>
<td>
<span>{{csvData.gradeCanBeChanged}}</span>
</td>
<td>
<span>{{csvData.lastModifiedSubmission}}</span>
</td>
<td>
<span>{{csvData.onlineText}}</span>
</td>
<td>
<span>{{csvData.lastModifiedGrade}}</span>
</td>
<td>
<span>{{csvData.feedbackComments}}</span>
</td>
</tr>
</tbody>
</table>
</div>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgxCsvParserModule } from 'ngx-csv-parser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ReactiveFormsModule,
AppRoutingModule,
BrowserAnimationsModule,
FormsModule,
NgxCsvParserModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Snip of CSV File, 53 lines long: https://i.sstatic.net/yPrIj.png
console.log('Result', result);infileChangeListenershow the values?Property 'feedbackArray' has no initializer and is not definitely assigned in the constructor.in my component.ts file