2

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:&nbsp;</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

3
  • It could be the code or the csv. Share some code and data or better create a Stackblitz Commented Jun 20, 2021 at 23:12
  • Does console.log('Result', result); in fileChangeListener show the values? Commented Jun 21, 2021 at 5:18
  • When I'm trying to replicate this code I'm getting this error Property 'feedbackArray' has no initializer and is not definitely assigned in the constructor. in my component.ts file Commented Aug 13, 2021 at 3:14

1 Answer 1

1

Try indexing with the exact value in the column header, e.g.

<td>
<span>{{csvData["Identifier"]}}</span>
</td>
<td>
<span>{{csvData["Full name"]}}</span>
</td>
<td>
<span>{{csvData["Email address"]}}</span>
</td>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.