Commit f9118e58 authored by ismael.rodriguez's avatar ismael.rodriguez

Closed last issues and added import as a text insead as a file

parent 568de976
......@@ -12,7 +12,7 @@
<div id="modals"></div>
<div class="row">
<div class="col-sm-12 col-md-12 main">
<h1 class="page-header">File Content</h1>
<h1 class="page-header">Imported Data</h1>
<div id="danger" class="alert alert-danger" style="display:none"></div>
<div id="warning" class="alert alert-warning" style="display:none"></div>
<div class="table-responsive">
......
......@@ -29,7 +29,7 @@
<br><div align="center">
<a href="#modal_fichero" id="boton_subida" class="btn btn-primary btn-xl" data-toggle="modal">Upload file</a>
<a href="#modal_fichero" id="boton_subida" class="btn btn-primary btn-xl" data-toggle="modal">Import data</a>
</div>
<br/>
<br/>
......
......@@ -2,60 +2,71 @@ $(document).ready(function() {
$(document).on('click', '#upload_file', function() {
var formData = new FormData($('#formfile')[0]);
var fichero = $("#file").val();
var csvfile = $("#file")[0].files[0];
Papa.parse(csvfile, {
header: true,
dynamicTyping: true,
complete: function(results) {
var data = {};
data["dataset"] = [];
var key = results.meta.fields[0];
if (results.data.map(function(row) { return isNaN(row[key]); }).reduce(function(prev, curr, index, array) { return prev && curr; })) {
results.meta.fields = results.meta.fields.splice(1);
results.data.forEach(function(row) {
data["dataset"].push(row[key]);
delete row[key];
});
}
data["names"] = results.meta.fields;
var text = $("#import_text").val();
var values = {};
results.meta.fields.forEach(function(field) {
values[field] = [];
});
if (fichero == "" && text == "") {
$("#danger_file").html("<strong>You must enter either a text data or an input file.</strong>");
$("#danger_file").show();
} else {
var csvfile = ""
if (fichero != "") {
csvfile = $("#file")[0].files[0];
} else {
csvfile = text;
}
try {
results.data.forEach(function(row) {
if ($.map(row, function(v) {return v;}).length == results.meta.fields.length) {
Papa.parse(csvfile, {
header: true,
dynamicTyping: true,
complete: function(results) {
var data = {};
data["dataset"] = [];
var key = results.meta.fields[0];
if (results.data.map(function(row) { return isNaN(row[key]); }).reduce(function(prev, curr, index, array) { return prev && curr; })) {
results.meta.fields = results.meta.fields.splice(1);
results.data.forEach(function(row) {
data["dataset"].push(row[key]);
delete row[key];
});
}
data["names"] = results.meta.fields;
var values = {};
results.meta.fields.forEach(function(field) {
if (row[field] == "-" ||
row[field] == "?")
{
values[field].push(NaN);
} else if (!isNaN(row[field])) {
values[field].push(row[field]);
} else {
throw "Unexpected character";
}
values[field] = [];
});
try {
results.data.forEach(function(row) {
if ($.map(row, function(v) {return v;}).length == results.meta.fields.length) {
results.meta.fields.forEach(function(field) {
if (row[field] == "-" ||
row[field] == "?")
{
values[field].push(NaN);
} else if (!isNaN(row[field])) {
values[field].push(row[field]);
} else {
throw "Unexpected character";
}
});
}
});
data["values"] = values;
sessionStorage["data"] = JSON.stringify(data);
$("#show_file").show();
$('#info_file').html("Done!");
window.location = APP_CONFIG.app_url + "/data.html";
} catch (err) {
$("#danger_file").html("<strong>"+ err +"</strong>");
$("#danger_file").show();
$('#formfile').trigger('reset');
}
}
});
data["values"] = values;
sessionStorage["data"] = JSON.stringify(data);
$("#show_file").show();
$('#info_file').html("Done!");
window.location = APP_CONFIG.app_url + "/file.html";
} catch (err) {
var resultado = "<p style=\"color:red\";><strong>" + err + "</strong></p>";
$('#info_file').html("<br>"+resultado);
$('#formfile').trigger('reset');
}
}
});
});
if ($(document).find("#file_table").length > 0) {
......
......@@ -17,11 +17,17 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Upload file</h4>
<h4 class="modal-title">Import data</h4>
</div>
<div class="modal-body">
<form class="form" id="formfile" enctype="multipart/form-data">
<p class="bg-warning">The file must be in csv format <label><a href="#helpModal" data-toggle="modal" more-info="file">[?]</a></label></p>
<div id="danger_file" class="alert alert-danger" style="display:none"></div>
<p class="bg-warning">The data must be in csv format <label><a href="#helpModal" data-toggle="modal" more-info="file">[?]</a></label></p>
<h5>Insert text:</h5>
<div class="form-group">
<textarea class="form-control" id="import_text" rows="10"></textarea>
</div>
<h5>Or upload a file:</h5>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
......@@ -32,11 +38,10 @@
</div>
<div id="info_file" align="center"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="upload_file">Upload file</button>
<button type="button" class="btn btn-primary" id="upload_file">Import data</button>
</div>
</div>
</div>
......
......@@ -35,8 +35,8 @@
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li id="show_file" style="display:none"><a href="file.html"><span class="glyphicon glyphicon-list-alt"></span> Show file</a></li>
<li><a id="a_subida" href="#modal_fichero" data-toggle="modal"><span class="glyphicon glyphicon-upload"></span> Upload file</a></li>
<li id="show_file" style="display:none"><a href="data.html"><span class="glyphicon glyphicon-list-alt"></span> Show data</a></li>
<li><a id="a_subida" href="#modal_fichero" data-toggle="modal"><span class="glyphicon glyphicon-upload"></span> Import data</a></li>
<li><a href="#helpModal" data-toggle="modal"><span class="glyphicon glyphicon-question-sign"></span> Help</a></li>
</ul>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment