How to autoincrement a variable 1 in 1, according to dynamically created record, jquery?

0

I currently generate a list of products dynamically in this way:

var listado_ofertas_adicionales = $('.listado_ofertas_adicionales');
                       valCarrito = response.data;
                       $.each(valCarrito, function (i) {
                            $('<li/>').addClass('swipeout')
                                .append($('<div/>').addClass('swipeout-content item-content')
                                    .append($('<div/>').addClass('item-media')
                                        .append('<img src="' + valCarrito[i].Url + '" onerror="this.onerror=null;" alt="" width="40" height="60" />'))
                                    .append($('<div/>').addClass('item-inner')
                                        .append($('<div/>').addClass('item-title-row')
                                            .append($('<div style="font-size: 12px;"/>').addClass('item-title').append(valCarrito[i].Codigo_Inventario + " - " + valCarrito[i].Descripcion))
                                            .append('<div class="codInventario" style="display:none">' + valCarrito[i].Codigo_Inventario + '</div>')
                                            .append('<div class="tipo" style="display:none">' + valCarrito[i].Tipo + '</div>')
                                            .append('<div class="tipo_costo" style="display:none">' + valCarrito[i].TipoCosto + '</div>')
                                            .append('<div class="precioSocio" style="display:none">' + valCarrito[i].PrecioSocio + '</div>')
                                            .append('<div class="volumenNegocio" style="display:none">' + valCarrito[i].VolumenNegocio + '</div>')
                                            .append('<div class="item-after" data-unit-price="' + valCarrito[i].PrecioSocio + '"><span class="product-amount" style="font-size: 12px;">' + valCarrito[i].PrecioSocio + '</span></div>')
                                        )
                                        .append($('<div/>').addClass('item-text')
                                            /*.append('<input type="number" class="add_ofertas_adicionales" value="' + valCarrito[i].Cantidad + '" min="0" placeholder="Ingrese cantidad">')*/                                              
                                                .append($('<div/>').addClass('chip chip-small').append('<div class="chip-label"><span class="product-quantity">' + valCarrito[i].Cantidad + '</span></div>'))
                                                .append($('<div style="background: rgba(0, 0, 0, 0);"/>').addClass('chip chip-small').append('<div class="chip-label"><button class="sumarOferta">+</button></div>'))
                                                .append($('<div style="background: rgba(0, 0, 0, 0);"/>').addClass('chip chip-small').append('<div class="chip-label"><button class="restarOferta">-</button></div>'))
                                        )
                                    )
                                )

From this dynamic content I must increase the number of products to be selected. Which I do this way:

$('.listado_ofertas_adicionales').on('click','.sumarOferta',function(e){
        myApp.showIndicator();
        var el                  = $(this).closest('.swipeout');
            el_product_quantity = el.find('.product-quantity');
        var tipo                = el.find('.tipo').text();
        var codigoinventario    = el.find('.codInventario').text();
        var cantidad            = acrescimo;
        var tipocosto           = el.find('.tipo_costo').text();
        var preciosocio         = el.find('.precioSocio').text();
        var volumennegocio      = el.find('.volumenNegocio').text();
        addProductoOferta(idPedido,tipo,codigoinventario,cantidad,tipocosto,preciosocio,volumennegocio);
        //acrescimo = 0;
        console.log(cantidad)
        cantidad = acrescimo++;
        el_product_quantity.text(cantidad);

    });

The result is as follows:

The detail is that each time I press the + button, the class product-quantity should increase as it goes in the product. 1, the detail is that if for example in the first product and I have an amount of 4, at the time of increasing the value for example in product 3 that is 0, obviously assigned the value 5 which is what would correspond correctly . Then how could you carry your own counter for each record to add and subtract. , thanks.

    
asked by JG_GJ 12.09.2018 в 04:04
source

1 answer

0

One option is to use a vector of integers, and each of the items corresponds to a position and there you save the counter. The other thing is that I see that you access the elements by class, it is advisable to use the id's. Example: .append ('' + valCarrito [i] .Codigo_Inventario + '')

And at the end of the append you do something like vecContadores.push (0);

When you want to access the item you use the example id "# codInventario5" and you know that your counter will be in vecContadores [5]

Greetings,

    
answered by 12.09.2018 в 04:37