Advertisement
campos20

script.js

Nov 2nd, 2020
2,650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adicionarTarefa(evt) {
  2.   // Evita que a pagina seja recarregada ao enviar o formulario
  3.   evt.preventDefault();
  4.  
  5.   // Obtem os valores dos inputs
  6.   let descricao = document.getElementById("descricao-input");
  7.  
  8.   let data = document.getElementById("data-input");
  9.  
  10.   // Cria a nova tarefa e adiciona a lista
  11.   let novaTarefa = {
  12.     descricao: descricao.value,
  13.     data: data.value,
  14.     feito: false,
  15.   };
  16.   tarefas.push(novaTarefa);
  17.  
  18.   popularTabela();
  19. }
  20.  
  21. function popularTabela() {
  22.   // Referencia ao corpo da tabela
  23.   let conteudo = document.getElementById("conteudo");
  24.  
  25.   // Popula a tabela
  26.   for (let i = 0; i < tarefas.length; i++) {
  27.     let tarefa = tarefas[i];
  28.  
  29.     let linha = document.createElement("tr");
  30.     conteudo.appendChild(linha);
  31.  
  32.     let posicao = document.createElement("th");
  33.     linha.appendChild(posicao);
  34.     posicao.innerText = i + 1;
  35.  
  36.     let descricao = document.createElement("td");
  37.     linha.appendChild(descricao);
  38.     descricao.innerText = tarefa.descricao;
  39.  
  40.     let data = document.createElement("td");
  41.     linha.appendChild(data);
  42.     data.innerText = tarefa.data;
  43.  
  44.     let feito = document.createElement("td");
  45.     linha.appendChild(feito);
  46.  
  47.     let input = document.createElement("input");
  48.     input.type = "checkbox";
  49.     input.checked = tarefa.feito;
  50.     feito.appendChild(input);
  51.   }
  52. }
  53.  
  54. // Adiciona a funcao criada ao formulario
  55. let formulario = document.getElementById("formulario");
  56. formulario.onsubmit = adicionarTarefa;
  57.  
  58. // Lista de tarefas a fazer
  59. let tarefas = [
  60.   { descricao: "Recolher o lixo", data: "02/11/2020", feito: false },
  61.   { descricao: "Enviar email", data: "03/11/2020", feito: false },
  62.   { descricao: "Criar página pessoal", data: "31/10/2020", feito: true },
  63.   { descricao: "Ligar para família", data: "02/11/2020", feito: false },
  64.   { descricao: "Tirar identidade nova", data: "09/11/2020", feito: false },
  65. ];
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement