1
I am developing in Django an application for procedural research and it would be made based on excel spreadsheets. I found on the internet tutorials to select the table rows, however, I do not know how to develop a button with a function that sends the selected rows as lists in Python to perform the search.
HTML is very simple and the javascript function I copied from an internet tutorial:
var tabela = document.getElementById("minhaTabela");
var linhas = tabela.getElementsByTagName("tr");
for(var i = 0; i < linhas.length; i++){
var linha = linhas[i];
linha.addEventListener("click", function(){
selLinha(this, true);
});
}
function selLinha(linha, multiplos){
if(!multiplos){
var linhas = linha.parentElement.getElementsByTagName("tr");
for(var i = 0; i < linhas.length; i++){
var linha_ = linhas[i];
linha_.classList.remove("selecionado");
}
}
linha.classList.toggle("selecionado");
}
var btnVisualizar = document.getElementById("visualizarDados");
btnVisualizar.addEventListener("click", function(){
var selecionados = tabela.getElementsByClassName("selecionado");
//Verificar se eestá selecionado
if(selecionados.length < 1){
alert("Selecione pelo menos uma linha");
return false;
}
var dados = "";
for(var i = 0; i < selecionados.length; i++){
var selecionado = selecionados[i];
selecionado = selecionado.getElementsByTagName("td");
dados += "CNPJ: " + selecionado[0].innerHTML + " - RAZÃO SOCIAL: " + selecionado[1].innerHTML + " - NOME FANTASIA: " + selecionado[2].innerHTML + "\n";
}
alert(dados);
});
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{% static 'css/banco.css' %}">
</head>
<body>
<button id="visualizarDados">Visualizar Dados</button>
<table border="1" class="dataframe" id="minhaTabela">
<thead>
<tr style="text-align: right;">
<th></th>
<th>CNPJ</th>
<th>RazaoSocial</th>
<th>NomeFantasia</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>00000000000</td>
<td>xxxxxxxxxxxxx S/A</td>
<td>xxxxxxxxxxx</td>
</tr>
<tr>
<th>1</th>
<td>111111111111</td>
<td>yyyyyyyyyyyy S/A</td>
<td>yyyyyyyyyyyyyy</td>
</tr>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="{% static 'js/banco.js' %}" async> </script>
</body>
</html>
But instead of sending the alert on the screen, I would like to receive these as if the items selected in the table were a form. I don’t know if this is possible, but I don’t understand practically anything about Javascript and I couldn’t find this kind of information on the Internet.