-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (75 loc) · 2.26 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
$(()=>{ // ready
// ler arquivo usando jQuery
let arquivo = "catalogo.csv";
/*
$.ajax({
type: "GET",
url: arquivo,
dataType: "text",
success: (dados)=>{
// separa as linhas
let linhas = dados.split("\n")
// para cada linha
for(let linha of linhas){
console.log(linha);
}
},
statusCode: {
404: function() {
console.log( "page not found" );
},
500: function() {
console.log( "page fail" );
}
},
error:(jqXHR, textStatus, errorThrown)=>{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
console.log("Fim");
}
});
*/
$.ajax({
type: "GET",
url: "catalogo.csv",
dataType: "text",
success: function(data) {
const parsedCSV = parseCSV(data)
$( function() {
var availableTags = parsedCSV;
$( "#tags" ).autocomplete({
source: availableTags
});
} );
},
error:(jqXHR, textStatus, errorThrown)=>{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
console.log("Fim");
}
})
function lerArquivoCSV(arquivo, callback) {
const leitor = new FileReader();
leitor.onload = function (evento) {
const conteudo = evento.target.result;
callback(conteudo);
};
leitor.readAsText(arquivo);
}
const inputArquivo = document.getElementById('arquivoCSV');
inputArquivo.addEventListener('change', function (evento) {
const arquivo = evento.target.files[0];
lerArquivoCSV(arquivo, function (conteudo) {
// Aqui você pode prosseguir para o próximo passo
console.log(conteudo);
});
});
});
function parseCSV(csv) {
/* split the data into array of lines of type */
const csvLines = csv.split(/\r\n|\n/);
/* loop throw all the lines a remove first part (from the start, to comma) */
return csvLines.map(line => line.split(',')[1])
}