-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (66 loc) · 2.38 KB
/
index.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
72
var selectedRow = null;
function onFormSubmit(e) {
event.preventDefault();
var formData = readFormData();
if (selectedRow == null){
insertNewRecord(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 the data
function insertNewRecord(data) {
var table = document.getElementById("storeList").getElementsByTagName('tbody')[0];
var newRow = table.insertRow(table.length);
cell1 = newRow.insertCell(0);
cell1.innerHTML = data.productCode;
cell2 = newRow.insertCell(1);
cell2.innerHTML = data.product;
cell3 = newRow.insertCell(2);
cell3.innerHTML = data.qty;
cell4 = newRow.insertCell(3);
cell4.innerHTML = data.perPrice;
cell4 = newRow.insertCell(4);
cell4.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 = '';
selectedRow = null;
}