I can not get it to print everything together as a vector, it gets all separated

0

                      P1-2017-RAD I                                                                                    

                                  

    <table>
      <tr>
          <th>Veculos</th>
      </tr>
      <tbody id="veiculos">
      </tbody>
    </table>

</body>
<script>
    var cadastro = new function(){
        this.veiculos = [];
        this.el = document.getElementById("veiculos");
        this.itemEditar = -1;

        this.listarTodos = function(){
            var dados = '';

            if(this.veiculos.length > 0){
                for(i=0;i<this.veiculos.length;i++){
                    dados += '<tr>';
                    dados += '<td>'+this.veiculos[i]+'</td>';
                    dados += '<td><button onclick="cadastro.alterar('+i+')"> Alterar </button></td>';
                    dados += '<td><button onclick="cadastro.excluir('+i+')"> Excluir </button></td>';
                    dados += '</tr>';
                }
            }
            this.el.innerHTML = dados;
        };

        this.add = function(){
            var id;
            id++;
            var placa = document.getElementById("add-placa").value;
            var marca = document.getElementById("add-marca").value;
            var modelo = document.getElementById("add-modelo").value;

            if(placa && marca && modelo){
                if(this.itemEditar == -1){
                    this.veiculos.push(id,placa,marca,modelo);
                    /*this.veiculos.push(id);
                    this.veiculos.push(placa);
                    this.veiculos.push(marca);
                    this.veiculos.push(modelo);
                    */
                }else{
                    this.veiculos.splice(this.itemEditar,1,id,placa.trim(),marca.trim(),modelo.trim());
                    /*this.veiculos.splice(this.itemEditar,1,id);
                    this.veiculos.splice(this.itemEditar,2,placa.trim());
                    this.veiculos.splice(this.itemEditar,3,marca.trim());
                    this.veiculos.splice(this.itemEditar,4,modelo.trim());*/
                    this.itemEditar = -1;
                }
                this.listarTodos();
            }   
            this.novo();
        };

        this.editar = function(item){
            this.itemEditar = item;
            var placa = this.veiculos[item];
            var marca = this.veiculos[item];
            var modelo = this.veiculos[item];
            if(placa && marca && modelo){
                var elPlaca = document.getElementById("add-placa");
                var elMarca = document.getElementById("add-marca");
                var elModelo = document.getElementById("add-modelo");
                elPlaca.value = placa;
                elMarca.value = marca;
                elModelo.value = modelo;
            }
        };

        this.novo = function(){
            this.itemEditar = -1;
            var elId;
            var elMarca = document.getElementById("add-marca");
            var elPlaca = document.getElementById("add-placa");
            var elModelo = document.getElementById("add-modelo");

            elId.value = this.elId + 1;
            elMarca.value = '';
            elPlaca.value = '';
            elModelo.value = '';
        };

        this.excluir = function(item){
            this.veiculos.splice(item,1);
            this.listarTodos();
        };

    }
    function fechar(){
        document.getElementById("mostrar").style.display = 'none';
    };
    cadastro.listarTodos();
</script>

For example, I wanted it to print like this:

ID: 1 License Plate: Hsv-2536 Make: Fiat Model: Uno

    
asked by anonymous 20.04.2017 / 21:45

1 answer

1

Instead of

dados += '<td>'+this.veiculos[i]+'</td>';

You need a <td> for each element of this array this.veiculos[i] , and you can do it like this:

dados += this.veiculos[i].reduce(function(str, el){
    return str + '<td>' + el + '</td>';
}, '');

So each element of the [id,placa,marca,modelo] array will be placed within its td , and the reduce returns a string with everything.

If this.veiculos[i] is an object, you can do this:

dados += Object.keys(this.veiculos[i]).reduce(function(str, chave){
    return str + '<td>' + this.veiculos[i][chave] + '</td>';
}, '');
    
20.04.2017 / 21:52