Empty dynamic array table

Asked

Viewed 346 times

0

It’s the following guys I have a dynamic table in Java where it includes a row with some data filled by the user, in the method of adding the row I put an array also passing the fields I am sending to the table but when I see the values of the array nothing appears as if it were empty follows the code.

<html>
<head>
	<title> pedido </title>
        
    <script LANGUAGE="JavaScript">
         
           
    totals =0;
   //Função que adiciona linha na tabela
    function adiciona(){
      
        //Array para pegar o valores mandados para a tabela
        var itens = [];
        var prod = document.getElementById('cprod').value; 
        var qtd = document.getElementById('cquant').value; 
        itens.push(prod, qtd); 
        
       
        
        
        
    totals++
        tbl = document.getElementById("tabelaBanco");
 
        var novaLinha = tbl.insertRow(-1);
        var novaCelula;
 
        if(totals%2==0) cl = "#FFFFFF";
        else cl = "##FFFFFF";

        novaCelula = novaLinha.insertCell(0);
        novaCelula.align = "left";
        novaCelula.style.backgroundColor = cl;
        novaCelula.innerHTML = document.getElementById('cprod').value;
        totals;
 
        novaCelula = novaLinha.insertCell(1);
        novaCelula.align = "left";
        novaCelula.style.backgroundColor = cl;
        novaCelula.innerHTML =  document.getElementById('cquant').value;
      

         novaCelula2 = novaLinha.insertCell(2);
         novaCelula.align = "left";
        novaCelula.style.backgroundColor = cl;
        var btnEl = document.createElement('input');  
        btnEl.setAttribute('type', 'button'); 
        btnEl.setAttribute('class', 'btn'); 
        btnEl.onclick = function ()      {deleteRow(this.parentNode.parentNode.rowIndex)};  
        btnEl.setAttribute('value', 'Delete');  
       novaCelula2.appendChild(btnEl);   
       
       
     
    }
    
    function deleteRow(i){
    document.getElementById('tabelaBanco').deleteRow(i)
}
//Função de teste para ver se esta pegando o array
function pedido(){
    alert(itens);
}

</script>

</head>
<body>

    <table>
       
    <td>  Produto: <input type="text" name="produto" id="cprod" /> </td>  
        
      <td> Quantidade: <input type="number" name="quantidade" id="cquant" /> </td>
      <td> <input type='button' id='incluir' value='Incluir Produto' class="btn btn-primary" onclick='adiciona()'/> </td>
    </table>
    
     <table id='tabelaBanco' class="table table-hover" border='0' width='100%'>
    <thead>
        <tr style='background-color:#FBF6F7'>
            <td class="produto"><strong>Produto</strong></td>
            <td class="quantidade"><strong>Quantidade</strong></td>
            <td><strong>Excluir</strong></td>
        </tr>
    </thead>
    <tbody>
        <!-- Linhas e colunas correspondente aos seus registros -->
    </tbody>
</table>
    
    <p> <input type='button' id='confirmar' value='Confirmar Pedido' class="btn btn-primary" onclick='pedido()'/> 
    
</body>
</html>

The only thing I want is to put the data in the array

1 answer

1

You are declaring the variable itens within the scope of the function adiciona(). Try to declare it in the global scope along with the variable totals.

  • Man you know how you helped me now that bullshit

  • Your orders, friend! If the answer solved your problem, don’t forget to mark it as the best answer to remove it from the Unanswered Questions session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.