I can’t get him to print everything together as a vector, it’s all separate

Asked

Viewed 40 times

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 him to print like this:

ID: 1 Plate: Hsv-2536 Brand: Fiat Model No.: Uno

  • You can give an example of what you have this.veiculos?

  • o this.veiculos seria o vetor, e nele deveria estar dentro [id,placa,marca,modelo]

1 answer

1


At times

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

you need a <td> for each element of that 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 array [id,placa,marca,modelo] will be placed inside your td, and the reduce returns a string with all.

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

dados += Object.keys(this.veiculos[i]).reduce(function(str, chave){
    return str + '<td>' + this.veiculos[i][chave] + '</td>';
}, '');
  • i tested and gave this error here: Uncaught Typeerror: this.veiculos[i]. reduce is not a Function

  • @Antonygabriel ok, in that case this.veiculos[i] is not an array as you have indicated, maybe a json. What gives console.log(typeof this.veiculos[i], this.veiculos[i]);?

  • Uncaught Typeerror: this.veiculos[i]. reduce is not a Function at Object.listTodos (P1-rad1-RE_UP.html:38) at Object.add (P1-rad1-RE_UP.html:74) at Htmlformelement.onsubmit (P1-rad1-RE_UP.html:7). This here

  • Put that console.log I asked on the line after for(i=0;i<this.veiculos.length;i++){

  • the error continues

  • @Antonygabriel this line with the console.log doesn’t take the error away, it’s so I know what kind of content you have. You know what makes console.log? puts your result here pf.

  • @Antonygabriel you made it?

  • yes, I managed to print everything, the problem is the search by the board, I asked a new question, look there

  • @Antonygabriel saw the other question now and saw that what you had was not an array, but an object, take a look at this answer here now.

Show 4 more comments

Browser other questions tagged

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