aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/dataset-load/dataset-load.component.ts
blob: 843a57090de89912ef3bbce0f36194acf0f60618 (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
50
51
52
53
54
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 ","

  hasHeader: boolean = true;

  slice: string = "";

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

  constructor(private ngxCsvParser: NgxCsvParser) {
  }

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

  changeListener($event: any): void {
    this.files = $event.srcElement.files;
    this.update();
  }

  update() {

    if (this.files.length < 1)
      return;

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

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