JavaScript - [Ayuda] Reemplazar elemento de la cesta y calcular el precio total

 
Vista:
sin imagen de perfil
Val: 10
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

[Ayuda] Reemplazar elemento de la cesta y calcular el precio total

Publicado por TrustMaster (6 intervenciones) el 05/11/2019 22:12:19
Buenas noches, estaba intentando realizar un procedimiento a través de un script en el que me gustaría poder hacer unos arreglos, entre ellos que al añadir productos no se añada uno tras otro sino que este se sustituya y aumente la cantidad de productos y por otro lado con un botón que al hacer click me genere una funcion en donde me calcule el precio total de toda la cesta, pero no consigo obtener un buen resultado.

Os adjunto el html y el js para ver si me podéis echar una mano:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var arrayProductos;
var cantidad=1;
var arrayCesta=[];
 
 function inicio(){
     producto();
 }
 
 
 function Producto(idProducto, nombreProducto, cantidad, precio){
     this.idProducto= idProducto;
     this.nombreProducto = nombreProducto;
     this.cantidad = cantidad;
     this.precio = precio;
 
 }
 
 function producto(){
     var placa = new Producto("0", "Placa base Gigabyte GA-H110M-S2H", 1,"42,14");
     var ssd = new Producto("1","Disco duro Kingston A-400 SSD 480GB", 1,"45,45");
     var ram = new Producto("2","Memoria RAM DDR4 Corsair Vengeance LPX 3000 Mhz", 1,"69,12");
     arrayProductos = [placa, ssd, ram];
    for (i = 0; i < arrayProductos.length; i++) {
          document.getElementById("tablaProductos").innerHTML+="<tr><td>"+arrayProductos[i].nombreProducto+"</td> <td>"+arrayProductos[i].cantidad+"</td> <td>"+arrayProductos[i].precio+"</td><td><input type='button' onclick='anadir("+i+")'  size='50' name='btnAnadir'></input></td></tr>";
 
     }
 
 }
 
 function anadir(i){
     arrayCesta.push(i);
     acumulador="<tr><td>Producto</td><td>Cantidad cesta</td><td>Precio unitario</td><td>Precio total</td><td></td></tr>";
     for (var p = 0; p < arrayCesta.length; p++) {
           document.getElementById("cesta").innerHTML+=arrayCesta[p];
 
         acumulador+="<tr id='lin_"+p+"'><td>"+arrayProductos[arrayCesta[p]].nombreProducto+"</td> <td id='cantidad'>"+arrayProductos[arrayCesta[p]].cantidad+"</td> <td>"+arrayProductos[arrayCesta[p]].precio+"</td><td><input type='button' onclick='quitarDeCesta("+p+");'  size='50' value='Eliminar'></td></tr>";
     }
     document.getElementById("cesta").innerHTML=acumulador;
}
 function quitarDeCesta(i){
	 arrayCesta.splice(i, 1);
	 var quita_p_cesta= document.getElementById("lin_"+i);
	 quita_p_cesta.parentNode.removeChild(quita_p_cesta);
 
 }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
 
        <script type="text/javascript" src="javascript.js"></script>
        <link rel="stylesheet" href="estilos.css" type="text/css" title="actual" />
    </head>
    <body onload="inicio()">
        <div id="products" class="productos">
 
                        <table id="tablaProductos"  class="tabla">
                            <tr>
                                <td>Producto</td>
                                <td>Cantidad</td>
                                <td>Precio</td>
 
                            </tr>
                        </table>
 
            <table align="center" border="2" id="cesta" class="cesta">
                <tr>
                    <td>Producto</td>
                    <td>Cantidad cesta</td>
                    <td>Precio unitario</td>
                    <td>Precio total</td>
                </tr>
            </table>
 
        </div>
 
 
    </body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de joel
Val: 3.506
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

[Ayuda] Reemplazar elemento de la cesta y calcular el precio total

Publicado por joel (895 intervenciones) el 06/11/2019 08:44:06
De la manera que lo estas haciendo, es un tanto complicado, ya que en el arrayCesta solo guardas el indice del elemento, y te faltaría guardar también la cantidad de cada uno.

Te he modificado un poco el código para que arrayCesta sea un objeto clave:valor, que contenga el id y la cantidad... haber si te sirve:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var arrayProductos;
var cantidad=1;
var arrayCesta={};
 
 function inicio(){
     producto();
 }
 
 
 function Producto(idProducto, nombreProducto, cantidad, precio){
     this.idProducto= idProducto;
     this.nombreProducto = nombreProducto;
     this.cantidad = cantidad;
     this.precio = precio;
 
 }
 
 function producto(){
     var placa = new Producto("0", "Placa base Gigabyte GA-H110M-S2H", 1,"42,14");
     var ssd = new Producto("1","Disco duro Kingston A-400 SSD 480GB", 1,"45,45");
     var ram = new Producto("2","Memoria RAM DDR4 Corsair Vengeance LPX 3000 Mhz", 1,"69,12");
     arrayProductos = [placa, ssd, ram];
    for (i = 0; i < arrayProductos.length; i++) {
          document.getElementById("tablaProductos").innerHTML+="<tr><td>"+arrayProductos[i].nombreProducto+"</td> <td>"+arrayProductos[i].cantidad+"</td> <td>"+arrayProductos[i].precio+"</td><td><input type='button' onclick='anadir("+i+")'  size='50' name='btnAnadir'></input></td></tr>";
 
     }
 
 }
 
 function anadir(i){
    if (arrayCesta[i]==undefined)
    {
        arrayCesta[i]=1;
    }else{
        arrayCesta[i]+=1;
    }
    acumulador="<tr><td>Producto</td><td>Cantidad cesta</td><td>Precio unitario</td><td>Precio total</td><td></td></tr>";
    for (var p in arrayCesta) {
        acumulador+="<tr id='lin_"+p+"'><td>"+arrayProductos[p].nombreProducto+"</td> <td id='cantidad'>"+arrayCesta[p]+"</td> <td>"+arrayProductos[p].precio+"</td><td><input type='button' onclick='quitarDeCesta("+p+");'  size='50' value='Eliminar'></td></tr>";
    }
    document.getElementById("cesta").innerHTML=acumulador;
}
 function quitarDeCesta(i){
	 arrayCesta.splice(i, 1);
	 var quita_p_cesta= document.getElementById("lin_"+i);
	 quita_p_cesta.parentNode.removeChild(quita_p_cesta);
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar