How not to allow selecting a product from another list if it was already selected in a list, jquery?

0

Good day. In this way I select product from 2 list

List No. 1

$$('.lista_productos_elegir').on('click','.sumarProducto1', function(e){
        var list = $(this).closest('.swipeout_productos1');
            var el_product_quantity_producto1 = list.find('.product-quantity');
            var tipo_material                 = list.find('.tipo_material').text();
            var tipo_costo_material           = list.find('.tipo_costo_material').text();
            var codinv_material               = list.find('.codInventario_material').text();
            var preciosocio_material          = list.find('.preciosocio_material').text();
            var volumenNegocio_material       = list.find('.volumenNegocio_material').text();
            var tipo_seleccion                = list.find('.tipo_seleccion').text();
                product_sum                   = parseInt(el_product_quantity_producto1.text());
                cantidad_suma                 = product_sum+1;
                //el_product_quantity_producto1.text(cantidad_suma);

            var cantidad_valor1 =0;
            var lista;
            $('.lista_productos_elegir li').each(function(e){
                lista = $(this).find('.product-quantity');
                cantidad_valor1 += parseInt(lista.text());
            });

            if (cantidad_valor1 >=1){
               myApp.alert("Puede llevar un máximo de 1 productos");
               el_product_quantity_producto1.text(el_product_quantity_producto1.text());
            }
            else{
                $(".lista_productos_seleccionado > tr").remove();
                productos_seleccionados = deleteItem(productos_seleccionados, codinv_material);

                 productos_seleccionados.push({
                    "codigo": codinv_material,
                    "precio": preciosocio_material,
                    "cantidad": cantidad_suma
                 })
               el_product_quantity_producto1.text(cantidad_suma);
               agregarCupon(idPedido, codinv_material, tipo_costo_material,preciosocio_material,volumenNegocio_material,cantidad_suma,cod_cliente,tipo_cliente,cod_cupon,parseInt(tipo_seleccion));
               productosSeleccionados();
               $('.cupones_bienvenida_todos .cupon_check').attr('disabled', true);
            }

        });

List No.2 It is basically the same way of increasing the amount of product.

$$('.lista_productos_elegir3').on('click','.sumarProducto3', function(e){
        var list3                         = $(this).closest('.swipeout_productos3');
        var el_product_quantity_producto3 = list3.find('.product-quantity');
        var tipo_material                 = list3.find('.tipo_material').text();
        var tipo_costo_material           = list3.find('.tipo_costo_material').text();
        var codinv_material               = list3.find('.codInventario_material').text();
        var preciosocio_material          = list3.find('.preciosocio_material').text();
        var volumenNegocio_material       = list3.find('.volumenNegocio_material').text();
        var tipo_seleccion                = list3.find('.tipo_seleccion').text();
            product_sum                   = parseInt(el_product_quantity_producto3.text());
            cantidad_suma                 = product_sum+1;


        var cantidad_valor3 =0;
        var lista3;
        $('.lista_productos_elegir3 li').each(function(e){
            lista3 = $(this).find('.product-quantity');
            cantidad_valor3 += parseInt(lista3.text());
        });

        if (cantidad_valor3 >=3){
           myApp.alert("Puede llevar un máximo de 3 productos");
           el_product_quantity_producto3.text(el_product_quantity_producto3.text());
        }
        else{
            $(".lista_productos_seleccionado > tr").remove();
            productos_seleccionados = deleteItem(productos_seleccionados, codinv_material);

            productos_seleccionados.push({
                "codigo": codinv_material,
                "precio": preciosocio_material,
                "cantidad": cantidad_suma
             })
           el_product_quantity_producto3.text(cantidad_suma);
           agregarCupon(idPedido, codinv_material, tipo_costo_material, preciosocio_material, volumenNegocio_material, cantidad_suma, cod_cliente, tipo_cliente,cod_cupon, parseInt(tipo_seleccion));
           productosSeleccionados();
           $('.cupones_bienvenida_todos .cupon_check').attr('disabled', true);
        }
    });

How can I do if I already have a selected product in the List No. 1 and no longer allow me to select any value from the List No. 2 and vice versa

>     
asked by JG_GJ 27.09.2018 в 21:16
source

2 answers

1

the variables 'quantity_value1', 'quantity_value2', 'quantity_value3', put them as global variables, remove them from the click event and declare them at the top of your code and initialize them to 0. Now within each event you make a condition for validate if a value already exists in any of the variables to be validated:

$('.lista_productos_elegir').on('click','.sumarProducto1', function(e){

    if(cantidad_valor2 <= 0 && cantidad_valor3 <=0){

        var list = $(this).closest('.swipeout_productos1');
        var el_product_quantity_producto1 = list.find('.product-quantity');
        var tipo_material                 = list.find('.tipo_material').text();
        var tipo_costo_material           = list.find('.tipo_costo_material').text();
        var codinv_material               = list.find('.codInventario_material').text();
        var preciosocio_material          = list.find('.preciosocio_material').text();
        var volumenNegocio_material       = list.find('.volumenNegocio_material').text();
        var tipo_seleccion                = list.find('.tipo_seleccion').text();
            product_sum                   = parseInt(el_product_quantity_producto1.text());
            cantidad_suma                 = product_sum+1;
            //el_product_quantity_producto1.text(cantidad_suma);
        var lista;
        $('.lista_productos_elegir li').each(function(e){
            lista = $(this).find('.product-quantity');
            cantidad_valor1 += parseInt(lista.text());
        });

        if (cantidad_valor1 >=1){
           myApp.alert("Puede llevar un máximo de 1 productos");
           el_product_quantity_producto1.text(el_product_quantity_producto1.text());
        }
        else{
            $(".lista_productos_seleccionado > tr").remove();
            productos_seleccionados = deleteItem(productos_seleccionados, codinv_material);

             productos_seleccionados.push({
                "codigo": codinv_material,
                "precio": preciosocio_material,
                "cantidad": cantidad_suma
             })
           el_product_quantity_producto1.text(cantidad_suma);
           agregarCupon(idPedido, codinv_material, tipo_costo_material,preciosocio_material,volumenNegocio_material,cantidad_suma,cod_cliente,tipo_cliente,cod_cupon,parseInt(tipo_seleccion));
           productosSeleccionados();
           $('.cupones_bienvenida_todos .cupon_check').attr('disabled', true);
        }
    }else{
        console.log("No puedes agregar mas productos a esta lista.");
    }

});

Remember to remove inside the event, where you declare the variable that you already placed as global.

And so do it for every event. I hope you have understood and that you have been able to help.

    
answered by 27.09.2018 / 23:09
source
0

You could click on each one to disable the click of the other using unbind in the following way

$('.lista_productos_elegir').on('click','.sumarProducto1', function(e){
    $( ".lista_productos_elegir3").unbind( "click" ); 
    ........

and the same in the other list

EDITED To handle the click in a different way in the other list, you can assign it again after unbind

$('.lista_productos_elegir').on('click','.sumarProducto1', function(e){
  $( ".lista_productos_elegir3").unbind( "click" ); 
  $(".lista_productos_elegir3").on('click','.sumarProducto3',function(e){ 
    alert("solo se puede seleccionar productos de una lista"));
   });
    
answered by 27.09.2018 в 21:53