Fill in form data from an object - Using Javascript

1

Javascript - I'm not sure how to fill in the data fields in the form. I have to enter a name in the " id=nombre " field of my form and press the " Ver " button to see all the data of the object of that name.

I store the data in an array. An object has the following attributes:

  • Name
  • Singer
  • Year
  • Shelves
  • Type
  • Borrowed
  • Code of the two functions that I should use:

        //Devuelve el objeto Disco que está en la posicion i-esima.
        this.getDisco = function (pos){
            return this.arrayDiscos[pos];
        }
    
        //Devuelve verdadero si existe el disco cuyo nombre coincide con el que se pasa.
        this.existeDisco = function (nombreRecibido){
            var existe = false;
            for(var i=0; i<this.arrayDiscos.length; i++){
                var nombre = this.arrayDiscos[i].nombre;
                if(nombre == nombreRecibido)
                    existe = true;
            }
            return existe;
        }
    

    Javascript code - event button View:

    document.getElementById('ver').addEventListener('click', verDisco, false);
    

    Javascript code - verDisco ():

    function verDisco(){
        var nombre = document.getElementById('nombre').value;
        //Comprobamos si existe el disco en nuestra tienda...
        if(miTienda.existeDisco(nombre)){
            //Cargamos los datos del objeto cuyo nombre = nombre en los diferentes campos del formulario.
            //...
            return true;
        }else{
            //Si NO EXISTE ningún disco con ese titulo/nombre, mensaje.
            document.getElementById("errores").innerHTML="NO EXISTE NINGUN DISCO CON EL NOMBRE '"+nombre+"'.";
            document.getElementById("nombre").focus();
            return false;
        }
    }
    

    How can I access each attribute of the object in question?

        
    asked by omaza1990 25.05.2017 в 17:50
    source

    2 answers

    1

    If you are going to show the data in the same form where the name is, simply:

    //Cargamos los datos del objeto cuyo nombre = nombre en los diferentes campos del formulario.
    
    // antes filtras el array por el nombre y devuelves el registro, supongamos en la variable registro
    
        var data = [{
            Nombre: 'Nombre',
            Cantante: 'Cantante',
            Año: 'Año',
            Estanteria: 'Estanteria',
            Tipo: 'Tipo',
            Prestado: 'Prestado'
    }];
    
     var registro = data.filter(function(record) {
        return record.nombre = document.getElementById('nombre').value;
    });
    
    document.<id formulario>.<id text>.value = registro[0].cantante
    

    This way you can fill in the form

        
    answered by 25.05.2017 в 18:10
    0

    No, taking into account that your array, as it is, is like a Json object, you must take each one of the values from it and assign them to your html objects; example if you have the arrary

    var data = [{
        Nombre: 'Nombre',
        Cantante: 'Cantante',
        Año: 'Año',
        Estanteria: 'Estanteria',
        Tipo: 'Tipo',
        Prestado: 'Prestado'
    }];
    

    And you have in your Html let's say two inputs where you are going to load the information of the attribute "Name" and "Singer" could be like this:

    <input type="text" id="inputNombre"/>
    <input type="text" id="inputCantante"/>
    

    Now to load the information by means of jquery is quite simple, first you must obfilly carry out the filter to find the name inside the array as you have it in your code and after you find it you simply assign the attributes to the inputs with this reserved word "val ()"; so do it like this:

    function verDisco(){
       //primero buscas el disco
       if(buscarDisco==true){
        //esto es sintaxis de jquery
        //dentro del val pones el array con la posicion correspondiente y 
          apuntando 
     //al atributo que se va a poner
     $('#inputNombre').val(data[x].Nombre);
     $('#inputCantante').val(data[x].Cantante);
    
       }else{
    alert("Disco no encontrado");
       }
       }
    
        
    answered by 25.05.2017 в 19:01