From a6f994c8bcd6949c186fe4209ad5a5f5f9f58eb5 Mon Sep 17 00:00:00 2001 From: Sonja Galovic Date: Wed, 9 Mar 2022 22:27:02 +0100 Subject: Komponenta za ucitavanje i prikaz csv fajla v2 --- .../dataset-load/dataset-load.component.html | 40 ++++++++-- .../dataset-load/dataset-load.component.ts | 93 +++++++--------------- 2 files changed, 65 insertions(+), 68 deletions(-) (limited to 'frontend/src/app/_elements') diff --git a/frontend/src/app/_elements/dataset-load/dataset-load.component.html b/frontend/src/app/_elements/dataset-load/dataset-load.component.html index 934aa5eb..48883ba0 100644 --- a/frontend/src/app/_elements/dataset-load/dataset-load.component.html +++ b/frontend/src/app/_elements/dataset-load/dataset-load.component.html @@ -1,16 +1,46 @@
- - + + + + + +     + + + + + +

+ + + - + - - + + + + +
{{item}}{{item}}
{{item[j]}}
{{col}}
+
+ . . .
+ {{rowsNumber}} x {{colsNumber}} +
+
\ No newline at end of file diff --git a/frontend/src/app/_elements/dataset-load/dataset-load.component.ts b/frontend/src/app/_elements/dataset-load/dataset-load.component.ts index d97e7cbe..7cdfe384 100644 --- a/frontend/src/app/_elements/dataset-load/dataset-load.component.ts +++ b/frontend/src/app/_elements/dataset-load/dataset-load.component.ts @@ -1,4 +1,5 @@ -import { Component, OnInit, ViewChild } from '@angular/core'; +import { Component, ViewChild } from '@angular/core'; +import { NgxCsvParser, NgxCSVParserError } from 'ngx-csv-parser'; @Component({ selector: 'app-dataset-load', @@ -7,76 +8,42 @@ import { Component, OnInit, ViewChild } from '@angular/core'; }) export class DatasetLoadComponent { - //array varibales to store csv data - lines : any[] = []; //for headings - linesR : any[] = []; // for rows + delimiter: string = ""; + delimiterOptions: Array = [",", ";", "\t", "razmak", "|"]; //podrazumevano "," -/* - const csv = require('csv-parser') - const fs = require('fs') - const res : string[] = []; + header: string = ""; + headerOptions: Array = ["Da", "Ne"]; //podrazumevano je "Da" ======> false - fs.createReadStream('https://raw.githubusercontent.com/sharmaroshan/Churn-Modelling-Dataset/master/Churn_Modelling.csv') - .pipe(csv()) - .on('data', (data : string) => res.push(data)) - .on('end', () => { - console.log(res); + slice: string = ""; -*/ + csvRecords: any[] = []; + rowsNumber: number = 0; + colsNumber: number = 0; - changeListener(files: FileList) { + constructor(private ngxCsvParser: NgxCsvParser) { + } + + @ViewChild('fileImportInput', { static: false }) fileImportInput: any; + + changeListener($event: any): void { - console.log(files); + const files = $event.srcElement.files; - if(files && files.length > 0) { - - let file: File | null = files.item(0); - if (file == null) - return; + 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) => { - if (file) { - console.log(file.name); - console.log(file.size); - console.log(file.type); - //File reader method - let reader: FileReader = new FileReader(); - reader.readAsText(file); - reader.onload = (e) => { - let csv: any = reader.result; - let allTextLines = []; - allTextLines = csv.split(/\r|\n|\r/); - - //Table Headings - let headers = allTextLines[0].split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/); - let data = headers; - let tarr = []; - for (let j = 0; j < headers.length; j++) { - tarr.push(data[j]); - } - //Pusd headings to array variable - this.lines.push(tarr); - //console.log(this.lines); - - - // Table Rows - let tarrR : string[] = []; - - let arrl = allTextLines.length; - let rows = []; - for(let i = 1; i < arrl; i++){ - rows.push(allTextLines[i].split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/)); - - } - - for (let j = 0; j < arrl; j++) { - tarrR.push(rows[j]); - } - //Push rows to array variable - this.linesR.push(tarrR); - console.log(this.linesR); + 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); + }); + } + } -- cgit v1.2.3