I have a table that is filled with an information textbox that has the excel format (the line breaks with the charcode 10 and the tab with the charcode 9), this information is transformed into a table so that later it registers in a database with the help of AJAX and Servlet.
The codes are the following:
HTML
<div id="data" class="col-sm-9" style="text-align: center;">
<div class="table-responsive">
<table class="table table-condensed table-striped w3-white" >
<thead>
<tr>
<th>Campo1</th>
<th>Campo2</th>
<th>Campo3</th>
<th>Campo N</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
</div>
JavaScript
Creation of the table.
function tnrtable() {
var Datos = document.getElementById("textbox").value
var myTableDiv = document.getElementById("data");
var myTable = document.getElementById("table-body");
Row = Datos.split(String.fromCharCode(10));
for (i = 0; i < excelRow.length; i++) {
Row[i] = Row[i].split(String.fromCharCode(9));
}
for (i = 0; i < Row.length - 1; i++) {
var k = i;
var row = myTable.insertRow(k);
//COLUMNAS
for (j = 0; j < Row[i].length; j++) {
if (Row[i][j].length !== 0) {
var cell = row.insertCell(j);
cell.innerHTML = excelRow[i][j];
}
row.appendChild(cell);
}
}
myTableDiv.appendChild(myTbody);
document.body.appendChild(myTable);
}
Data logging with Ajax
function Registro() {
var sw = 0;
alert("1");
$('#table-body').each(function (i, row) {
var dataRow = new Array();
alert("2");
var $row = $(row);
$check = $row.find("td");
alert("Valor i: " + i);
if (i !== 0) {
$check.each(function (j, cell) {
if (j < 9) {
dataRow.push(cell.innerHTML);
sw = 1;
}
});
alert("Contador: " + sw);
$.ajax({
url: "Registro", //Servlet donde se registra.
type: "POST",
data: {
json: dataRow
},
success: function (dataRow) {
alert(":)");
},
error: function () {
alert(":(");
}
});
}
});
if (sw === 0) {
alert("No hay información a enviar");
}
else {
alert("Información guardada");
document.location.href = "/Registro";
}
}
I really do not know if the problem lies in the generation of the table because the registration function always detects that there is no information in the body even if there is data generated. Thank you very much.