-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
71 lines (64 loc) · 2.36 KB
/
script.js
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
67
68
69
70
71
var selectedRow = null;
function onFormSubmit(){
event.preventDefault();
var formData = readFormData();
if(selectedRow === null){
insertNewProduct(formData);
}else{
updateRecord(formData);
}
resetForm();
}
//retrieve the data
function readFormData(){
var formData = {};
formData["productCode"] = document.getElementById("productCode").value;
formData["product"] = document.getElementById("product").value;
formData["qty"] = document.getElementById("qty").value;
formData["perPrice"] = document.getElementById("perPrice").value;
return formData;
}
//Insert data
function insertNewProduct(data){
var table = document.getElementById("storeList").getElementsByTagName("tbody")[0];
var newRow = table.insertRow(table.length);
var cell1 = newRow.insertCell(0);
cell1.innerHTML = data.productCode;
var cell2 = newRow.insertCell(1);
cell2.innerHTML = data.product;
var cell3 = newRow.insertCell(2);
cell3.innerHTML = data.qty;
var cell4 = newRow.insertCell(3);
cell4.innerHTML = data.perPrice;
var cell5 = newRow.insertCell(4);
cell5.innerHTML = `<button onClick='onEdit(this)'>Edit</button> <button onClick='onDelete(this)'>Delete</button>`
}
//Edit the data
function onEdit(td){
selectedRow = td.parentElement.parentElement;
document.getElementById('productCode').value = selectedRow.cells[0].innerHTML;
document.getElementById('product').value = selectedRow.cells[1].innerHTML;
document.getElementById('qty').value = selectedRow.cells[2].innerHTML;
document.getElementById('perPrice').value = selectedRow.cells[3].innerHTML;
}
function updateRecord(formData){
selectedRow.cells[0].innerHTML = formData.productCode;
selectedRow.cells[1].innerHTML = formData.product;
selectedRow.cells[2].innerHTML = formData.qty;
selectedRow.cells[3].innerHTML = formData.perPrice;
}
//Delete the data
function onDelete(td){
if(confirm('Do you want to delete this record?')){
row = td.parentElement.parentElement;
document.getElementById('storeList').deleteRow(row.rowIndex);
}
resetForm();
}
//Reset the data
function resetForm(){
document.getElementById('productCode').value = '';
document.getElementById('product').value = '';
document.getElementById('qty').value = '';
document.getElementById('perPrice').value = '';
}