Advertisement
Guest User

ff

a guest
Apr 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Limpiar(){
  2.     $("#txtModelo").val("");
  3.     $("#txtMarca").val("");
  4.     $("#txtPrecio").val("");
  5.     $("#txtCantidad").val("");
  6.     $("#txtDescuento").val("");
  7. }
  8.  
  9. function LlenarTabla(muebles) {
  10.     var tablaMuebles = $("#tablaMuebles");
  11.     var html = "<tr><th>Modelo</th><th>Marca</th><th>Precio</th><th>cantidad</th><th>Descuento</th></tr>";
  12.     muebles.forEach(function(item){
  13.         html += "<tr><td>"+item.modelo+"</td><td>"+item.marca+"</td><td>"+item.precio+"</td><td>"+item.cantidad+"</td><td>"+item.descuento+"</td></tr>";
  14.     });
  15.     tablaMuebles.html(html);
  16. }
  17.  
  18. $(document).ready(function(){
  19.     var btnLimpiar = $("#btnLimpiar");
  20.     btnLimpiar.click(function() {
  21.         Limpiar();
  22.     });
  23.        
  24.     var muebles = [
  25.         {
  26.             "modelo": "RKM-789",
  27.             "marca" : "IKEA",
  28.             "precio": 52300,
  29.             "cantidad": 3,
  30.             "descuento": 5
  31.         },
  32.         {
  33.             "modelo": "FGW-165",
  34.             "marca" : "KREA",
  35.             "precio": 69300,
  36.             "cantidad": 3,
  37.             "descuento": 5
  38.         }
  39.     ]
  40.  
  41.     var btnGuardar = $("#btnGuardar");
  42.     btnGuardar.click(function(){
  43.         var txtModelo = $("#txtModelo").val();
  44.         var txtMarca = $("#txtMarca").val();
  45.         var txtPrecio = $("#txtPrecio").val();
  46.         var txtCantidad = $("#txtCantidad").val();
  47.         var txtDescuento = $("#txtDescuento").val();
  48.  
  49.         var listaErrores = $("#listaErrores").css("color","#c62828");
  50.         var errores = [];
  51.  
  52.         if (txtModelo == "") {
  53.             errores.push("El nombre es requerido");
  54.         }else if(txtModelo.length < 3 || txtModelo.length >= 25){
  55.             errores.push("El campo modelo debe ser entre 3 y 25 caracteres");
  56.         }
  57.  
  58.         if (txtMarca == "") {
  59.             errores.push("La marca es requerida");
  60.         }else if(txtMarca.length < 3 || txtMarca.length >= 25){
  61.             errores.push("El campo marca debe ser entre 3 y 10 caracteres");
  62.         }
  63.  
  64.         if (txtPrecio == "") {
  65.             errores.push("El precio es requerido");
  66.         }else if(isNaN(txtPrecio)){ // verificamos que sea numero
  67.             errores.push("El precio debe ser numerica");
  68.         }
  69.  
  70.         if (txtCantidad == "") {
  71.             errores.push("La cantidad es requerida");
  72.         }else if(isNaN(txtCantidad)){
  73.             errores.push("El cantidad debe ser numerica");
  74.         }
  75.  
  76.         if (errores.length > 0) {
  77.             var html = "";
  78.             errores.forEach(function(item){
  79.                 html += "<li>"+item+"</li>";
  80.             });
  81.             listaErrores.html(html);
  82.             return;
  83.         }
  84.  
  85.         var datos = {
  86.             "modelo": txtModelo,
  87.             "marca" : txtMarca,
  88.             "precio": txtPrecio,
  89.             "cantidad": txtCantidad,
  90.             "descuento": txtDescuento
  91.         }
  92.         muebles.push(datos);
  93.         Limpiar();
  94.         LlenarTabla(muebles);
  95.     });
  96.  
  97.     LlenarTabla(muebles);
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement