0
I have a problem in a table that I did in html and javascript. The page consists of 3 input fields that with the inserted value is forwarded to the table below.
However I would like to be able to edit table items without having to delete and insert again.
Is there any way to send the values back to the input so I can edit it?
PS:
I did the removal with a checkbox field. that picks up the line and deletes, maybe to use this checkbox.
var itens = 0;
function adicionarItem() {
itens += 1;
var refNome = document.querySelector("#nome").value;
var refCpf = document.querySelector("#cpf").value;
var refRg = document.querySelector("#rg").value;
var usuarioTr = document.createElement("tr");
usuarioTr.className = itens %2 == 0 ? "linha1": "linha2";
var selec = document.createElement('td');
selec.className = 'usr';
var input = document.createElement('input');
input.type = 'checkbox';
input.className = 'check';
selec.appendChild(input);
var nomeTd = document.createElement("td");
var cpfTd = document.createElement("td");
var rgTd = document.createElement("td");
nomeTd.textContent = refNome;
cpfTd.textContent = refCpf;
rgTd.textContent = refRg;
usuarioTr.appendChild(selec);
usuarioTr.appendChild(nomeTd);
usuarioTr.appendChild(cpfTd);
usuarioTr.appendChild(rgTd);
var tabela = document.querySelector("#tabela");
tabela.appendChild(usuarioTr);
}
function removerItem() {
ckList = document.querySelectorAll("input[type=checkbox]");
ckList.forEach(function(el, index) {
if (ckList[index].checked) el.parentElement.parentElement.remove();
});
if (itens > 0)
itens -= 1;
}
<h1>Cadastro de usuário</h1>
<link rel="stylesheet" type="text/css" href="main.css" />
<section>
<form>
<div class="inputs">
<div class="inside">
<label for="nome">*Nome:</label>
<input class="inp" id="nome" name="nome" type="text" placeholder="Digite seu nome aqui" autofocus/>
</div>
<div class="inside">
<label for="cpf">*CPF:</label>
<input class="inp" id="cpf" name="cpf" type="number" placeholder="Digite seu CPF" />
</div>
<div class="inside">
<label for="rg">*RG:</label>
<input class="inp" id="rg" name="rg" type="number" placeholder="Digite seu RG" />
</div>
</div>
<div class="botoes">
<button clas="bot" id="adicionarBotao" onclick="adicionarItem()" type="button">Adicionar</button>
<button clas="bot" id="excluirBotao" onclick="removerItem()" type="button">Excluir</button>
</div>
</form>
</section>
<div class="tabelas">
<table>
<thead>
<tr>
<th>Selecionar</th>
<th>Nome</th>
<th>CPF</th>
<th>RG</th>
</tr>
</thead>
<tbody id="tabela">
</tbody>
</table>
</div>
Friend, it may be a little more complicated than that. The purpose of this table and this form is to work with a database?
– Inácio Régis
In fact I think after that it will be this, because simply my boss came and talked make this screen. kkkkk
– NegoWell
Have you considered the possibility of using plugins like this: http://www.jqueryscript.net/demo/Simple-In-line-Editing-Plugin-For-jQuery-Quick-Edit/
– Inácio Régis
I still do not know how to use kkk, it is to be a simple screen because I am at the beginning of college and internship, and as it is software engineering takes a little longer to get programming. So this screen has to be as pure as possible (html, Javascript and Css). All done by hand.
– NegoWell
Or you could use this one already https://vitalets.github.io/angular-xeditable/#Editable-Row because you already have the ajax request for your back-end (whatever) to manipulate the database.
– Inácio Régis
I understand, but if it’s that simple, you know the data won’t be saved, right? You could even store the data in the browser, but would risk losing it easily. When you have a little time, analyze this tutorial.https://codepen.io/manolof/post/angularjs-todo-save-data-to-localstorage-or-to-txt-file-using-php#local It might help you. unfortunately I can’t make the solution for you now... it would take a little time.
– Inácio Régis
ok, thank you very much, and that would be just that, without saving the data (a hitherto useless application).
– NegoWell