-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
105 lines (89 loc) · 3.17 KB
/
app2.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var selectedRow = null
console.log("working");
document.getElementById("btn").addEventListener("click",function(e) {
e.preventDefault();
validation();
});
function onFormSubmit() {
var formData = readFormData();
if (selectedRow == null)
insertNewRecord(formData);
else
updateRecord(formData);
clearForm();
}
function getData()
{
console.log("ok");
readFormData();
// var fname=documet.getElementById("fullname")
}
function clearForm(){
document.getElementById("fullname").value="";
document.getElementById("birth").value="";
document.getElementById("mob").value="";
selectedRow = null;
}
function onEdit(sel){
selectedRow = sel.parentElement.parentElement;
document.getElementById("fullname").value = selectedRow.cells[0].innerHTML;
document.getElementById("birth").value = selectedRow.cells[1].innerHTML;
document.getElementById("mob").value = selectedRow.cells[2].innerHTML;
console.log("clicked edit button")
}
function onDelete(sel){
if (confirm('Are you sure to delete selected record ?')) {
row = sel.parentElement.parentElement;
console.log(row);
document.getElementById("tview").deleteRow(row.rowIndex);
}
console.log("clicked delete button")
}
function updateRecord(formData) {
selectedRow.cells[0].innerHTML = formData.fullName;
selectedRow.cells[1].innerHTML = formData.datebirth;
selectedRow.cells[2].innerHTML = formData.fmob;
}
function readFormData(){
var formData = [];
formData["fullName"] = document.getElementById("fullname").value;
formData["datebirth"] = document.getElementById("birth").value;
formData["fmob"] = document.getElementById("mob").value;
// document.getElementById("id2").innerText=formData;
return formData;
}
function insertNewRecord(formData){
var ins =document.getElementById("tview").getElementsByTagName("tbody")[0];
console.log(ins);
var row = ins.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = formData.fullName;
cell2.innerHTML = formData.datebirth;
cell3.innerHTML = formData.fmob;
cell4.innerHTML=cell4.innerHTML = `<button onClick="onEdit(this)"><u>Edit</u></button>
<button onClick="onDelete(this)"><u>Delete</u></button>`
clearForm();
return formData;
}
// function updateRecord(formData) {
function validation(){
let full=document.getElementById("fullname").value;
let dbirth=document.getElementById("birth").value;
let dmob=document.getElementById("mob").value;
var phoneno = /^\d{10}$/;
if (full == ""){
alert("please fill the name field")
}
else if(!Date.parse(dbirth) ){
alert("please fill the date of birth field")
}
else if (dmob == "" ){
alert("please fill mobile number the field");
}
else{
onFormSubmit();
}
}