-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassFormulario.js
More file actions
66 lines (63 loc) · 2.27 KB
/
classFormulario.js
File metadata and controls
66 lines (63 loc) · 2.27 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Producto{
constructor(name,price,quantity){
this.name=name;
this.price=price;
this.qty=quantity;
}
}
class UI{
addProduct(product){
const productList=document.getElementById('productList');
const element=document.createElement('div');
element.innerHTML =`
<div class='card text-center mb-4'>
<div class='card-body'>
<strong>Name:</strong> ${product.name}
<strong>Price:</strong> ${product.price}
<strong>Quantity:</strong> ${product.qty}
<a href='#' class='btn btn-danger' name='delete'>Delete</a>
</div>
</div>
`;
productList.appendChild(element);
}
resetForm(){
document.getElementById('product-form').reset();
}
deleteProduct(element){
if(element.name==='delete'){
element.parentElement.parentElement.parentElement.remove();
this.showMessage('Producto eliminado exitosamente','danger');
}
}
showMessage(mensaje,clase){
const element=document.createElement('div');
element.className=`alert alert-${clase} mt-2`;
element.appendChild(document.createTextNode(mensaje));
const contenedor=document.querySelector('.container');
const app=document.querySelector('#App');
contenedor.insertBefore(element,app);
setTimeout(()=>{
document.querySelector('.alert').remove();
},2500)
}
}
const btn=document.getElementById("product-form").addEventListener('submit',(e)=>{
console.log(e);
const name=document.getElementById('name').value;
const qty=document.getElementById('quantity').value;
const price=document.getElementById('price').value;
const product=new Producto(name,price,qty);
console.log(product);
var ui=new UI();
ui.addProduct(product);
ui.resetForm();
ui.showMessage('Producto agregado exitosamente','success') ;
e.preventDefault();
})
const btnDelete=document.getElementById("productList").addEventListener('click',(e)=>{
if(e.target.name==='delete'){
var ui=new UI();
ui.deleteProduct(e.target);
}
})