Insert array in a table from ajax [closed]

0

I have the following Ajax

$(function() {
    //function ajax
    $('#example-table #btn-analisisInterno').click(function(e) {
        e.preventDefault();
        var elemento = $(this);
        var idArt = elemento.parent().parent().find('#id_articulo').text();
        console.log(idArt);

        $.ajax({
            url: 'http://localhost:3000/visualizarArt',
            method: 'post',
            data: { idArticulo: idArt },
            success: function(res) {
                console.log(res);
                //document.getElementById('artic').innerHTML = id;
                 $('#example tbody').append("<tr><td>" + res.SI_Articulo + "</td><td>" + res.SI_Existencia + "</td><td>" + res.SI_Ubicacion + "</td></tr>");
            }
        });
    });
});

which makes me a query to sql and brings me the following data in an array

Query

getVisualizarArticulo: function(req, res, next) {
        console.log('Entra');
        var id = req.body.idArticulo;
        var config = require('.././database/config');
        console.log(id);
        var art = null;
        var respuesta = { res: false };
        sql.connect(config)
            .then(function() {
                var request2 = new sql.Request();
                request2.query("SELECT SI_Articulo, SI_Ubicacion, SI_Existencia FROM SI_Inventario_Teorico_QAD WHERE SI_Articulo = '" + req.body.idArticulo + "'")
                    .then(function(recordset) {
                        art = recordset['recordset'];
                        console.log('Recordset: ' + recordset);
                        console.log('Affected: ' + request2.rowsAffected);
                        const articulos = art;
                        console.log(articulos);
                        sql.close();
                        respuesta.res = true;
                        res.json(articulos);

                    })
                    .catch(function(err) {
                        console.log('Request error: ' + err);
                    });
            })
            .catch(function(err) {
                if (err) {
                    console.log('SQL Connection Error: ' + err);
                }
            });
    }

(5) [{…}, {…}, {…}, {…}, {…}]
0
:
{SI_Articulo: "200002", SI_Ubicacion: "FF1031", SI_Existencia: 28}
1
:
{SI_Articulo: "200002", SI_Ubicacion: "o293534a", SI_Existencia: 11}
2
:
{SI_Articulo: "200002", SI_Ubicacion: "o293894a", SI_Existencia: 16}
3
:
{SI_Articulo: "200002", SI_Ubicacion: "o293894d", SI_Existencia: 17}
4
:
{SI_Articulo: "200002", SI_Ubicacion: "TRANSCLI", SI_Existencia: 1}

This is the information that brings me in console.log(res);

What I'm trying to do is enter that information into an html table and since that table is in a modal every time I consult it, it should be cleaned and the data re-entered.

    
asked by Eduard 30.06.2017 в 15:47
source

1 answer

1

If I understand your code well, in the line where you use the append you are adding the results to the table, but first you must go through the object to obtain its value from each property.

Example in javascript :

// Variables para almacenar los valores de las propiedades de tu objeto "res".
var si_articulo = "";
var si_existencia = "";
var si_ubicacion = "";

// Recorre el objeto para obtener los resultados:
for (var i = 0; i < res.length; i++) {

    // Obtienes los valores de las propiedades según recorres el objeto.
    si_articulo = res[i].SI_Articulo;
    si_existencia = res[i].SI_Existencia;
    si_ubicacion = res[i].SI_Ubicacion;

    // Ahora si agregas la información obtenida a tu tabla HTML:
    $('#example tbody').append("<tr><td>" + si_articulo + "</td><td>" + si_existencia +"</td><td>" + si_ubicacion + "</td></tr>");
}
    
answered by 30.06.2017 / 16:41
source