aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/dataset-load/dataset-load.component.ts
blob: 7cdfe384ba3527e93cf04770a03502b425b0c6fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Component, ViewChild } from '@angular/core';
import { NgxCsvParser, NgxCSVParserError } from 'ngx-csv-parser';

@Component({
  selector: 'app-dataset-load',
  templateUrl: './dataset-load.component.html',
  styleUrls: ['./dataset-load.component.css']
})
export class DatasetLoadComponent {

  delimiter: string = "";
  delimiterOptions: Array<string> = [",", ";", "\t", "razmak", "|"]; //podrazumevano ","

  header: string = "";
  headerOptions: Array<string> = ["Da", "Ne"]; //podrazumevano je "Da" ======> false

  slice: string = "";

  csvRecords: any[] = [];
  rowsNumber: number = 0;
  colsNumber: number = 0;

  constructor(private ngxCsvParser: NgxCsvParser) {
  }

  @ViewChild('fileImportInput', { static: false }) fileImportInput: any;

    changeListener($event: any): void {

    const files = $event.srcElement.files;

    this.ngxCsvParser.parse(files[0], { header: (this.header == "") ? false : (this.header == "Da") ? false : true, delimiter: (this.delimiter == "razmak") ? " " : (this.delimiter == "") ? "," : this.delimiter})
      .pipe().subscribe((result) => {

        console.log('Result', result);
        if (result.constructor === Array) {
          this.csvRecords = result;
          this.rowsNumber = this.csvRecords.length;
          this.colsNumber = this.csvRecords[0].length;
        }
        
      }, (error: NgxCSVParserError) => {
        console.log('Error', error);
      });

  }


}