JavaScript arrays object

1

I'm doing a sales system in php and mysql. The database operations I do with jquery ajax. I save header and detail of the sale separately, the detail is shown in a grid of jqwidgets from which I extract its datasource in as an object of arrays. Where each array of the object is a column of the grid. My question is how to get the values of those arrays to save them, because until now I only insert null fields in the bd. This is the code:

 var i = 0;
                 while(dataGrilla[i]){
                     for(var arreglo in dataGrilla) {

                         for(var elemento in dataGrilla[arreglo]){
                             //console.log(elemento);
                             cod_det = dataGrilla['colcodigos'];
                             p_unit = dataGrilla['colprecio'];
                             cantidad = dataGrilla['colcantidad'];
                         }
                         grabaDetServ();
                     }
                     i++;
                 }

function grabaDetServ() {

            var action = 'grabaDet';
            var recargo = 0;
            var folio3 = folio;

                $.ajax({
                    type: 'GET',
                    data: {action: action,cod_sec:cod_sec,cod_det:cod_det,p_unit:p_unit,cantidad:cantidad,folio:folio3},
                    url: '../app/venta.php',
                    success: function () {
                        //toastr.success('Detalle guardado ok');
                    },
                    error: function () {
                        toastr.error('Error al guardar detalle');
                    }
                });
            }

the data I want to get out of the array are: cod_det, p_unit and quantity, the others are fixed.

    
asked by daniel2017- 20.10.2016 в 17:43
source

1 answer

1

The problem is WHERE to call function grabaDetServ , it was outside of your main FOR, so they always arrived as undefined values since in your code I do not see that you define the variables either. You should have something like this:

var i = 0;
var cod_det = 0;
var p_unit = 0;
var cantidad = 0;
while (dataGrilla[i]) {
    for (var arreglo in dataGrilla) {

        for (var elemento in dataGrilla[arreglo]) {
            //console.log(elemento);
            cod_det = dataGrilla['colcodigos'];
            p_unit = dataGrilla['colprecio'];
            cantidad = dataGrilla['colcantidad'];
            grabaDetServ();
        }

    }
    i++;
}

You call the function grabaDetServ those values do not exist, since the fields that are giving value are inside your cycle for inside. But you call the function out of that for.

function grabaDetServ() {

    var action = 'grabaDet';
    var recargo = 0;
    var folio3 = folio;

    $.ajax({
        type: 'GET',
        data: { action: action, cod_sec: cod_sec, cod_det: cod_det, p_unit: p_unit, cantidad: cantidad, folio: folio3 },
        url: '../app/venta.php',
        success: function() {
            //toastr.success('Detalle guardado ok');
        },
        error: function() {
            toastr.error('Error al guardar detalle');
        }
    });
}

Or also if you do not want to declare variables with a default value

var i = 0;
while (dataGrilla[i]) {
    for (var arreglo in dataGrilla) {

        for (var elemento in dataGrilla[arreglo]) {
            //console.log(elemento);
            cod_det = dataGrilla['colcodigos'];
            p_unit = dataGrilla['colprecio'];
            cantidad = dataGrilla['colcantidad'];
            grabaDetServ(cod_det, p_unit, cantidad);
        }

    }
    i++;
}

And your save function

function grabaDetServ(cod_det, p_unit, cantidad) {
    .....
}
    
answered by 20.10.2016 в 17:56