-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.js
More file actions
138 lines (125 loc) · 4.45 KB
/
assignment.js
File metadata and controls
138 lines (125 loc) · 4.45 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//get json data
var json_data = JSON.parse(data);
function load() {
//change contents of load data button to refresh after it is clicked
document.getElementById("btn").innerHTML="Refresh";
//make sure that initially table is empty
document.getElementById("mytable").innerHTML='';
//creating columns and ther headings
//making a row for headings
var x = document.createElement("TR");
x.setAttribute("id", "headings");
document.getElementById("mytable").appendChild(x);
//making columns and giving names to them by taking attributes of the very first user
for(user in json_data) {
for(col_name in json_data[user]) {
var y = document.createElement("TH");
var t = document.createTextNode(col_name);
y.appendChild(t);
document.getElementById("headings").appendChild(y);
}
//adding edit delete column
var y = document.createElement("TH");
var t = document.createTextNode("Edit");
y.appendChild(t);
document.getElementById("headings").appendChild(y);
//breaking loop as attributes of only first user will do the work
break;
}
//adding rows of for all users
for(user in json_data) {
//make row
var x = document.createElement("TR");
//setting row id
x.setAttribute("id", "row-"+user);
document.getElementById("mytable").appendChild(x);
//push data into row
make_row(user);
}
}
function make_row(user) {
//get row by id
var row = document.getElementById("row-"+user);
//make sure that the row is empty
row.innerHTML='';
//push data into row
for(attribute in json_data[user]) {
var y = document.createElement("TD");
var t;
if(json_data[user][attribute] == null) t = document.createTextNode('');
else t = document.createTextNode(json_data[user][attribute]);
y.appendChild(t);
row.appendChild(y);
}
//add edit and delete buttons in the last cell
add_edit_delete(user);
}
function add_edit_delete(user) {
var edit=document.createElement("button");
var del=document.createElement("button");
edit.id = user;
del.id = user;
edit.innerHTML = "Edit";
del.innerHTML = "Delete";
//calling functions by passing user id
del.setAttribute("onClick", "del_row(this.id)");
edit.setAttribute("onClick", "edit_row(this.id)");
//create cell
var cell = document.createElement("TD");
//append buttons to the cell
cell.appendChild(edit);
cell.appendChild(del);
//append cell to the row
document.getElementById("row-"+user).appendChild(cell);
}
function del_row(user) {
//delete particular user from json file
delete json_data[user];
//reload the table
load();
}
function save_row(user) {
//updating row's attributes by taking values from input fields by their ids
for(attribute in json_data[user]) {
json_data[user][attribute] = document.getElementById(user+"-"+attribute).value;
}
//rewrite the contents of the row to show any change(s)
make_row(user);
}
function edit_row(user) {
//getting row by its id
row = document.getElementById("row-"+user);
//firstly clearing all contents of the row
row.innerHTML = '';
//making input fields and displaying old data as default
for(attribute in json_data[user]) {
var cell = document.createElement("TD");
var cell_data;
if(json_data[user][attribute] == null) cell_data = '';
else cell_data = json_data[user][attribute];
//making input field
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("value", cell_data);
input.setAttribute("id", user+"-"+attribute);
cell.appendChild(input);
row.appendChild(cell);
}
//change last cell contents to save and cancel buttons
var save=document.createElement("button");
var cancel=document.createElement("button");
save.id = user;
save.innerHTML = "Save";
cancel.id = user;
cancel.innerHTML = "Cancel";
//calling function by passing user id
cancel.setAttribute("onClick", "make_row(this.id)");
save.setAttribute("onClick", "save_row(this.id)");
//create cell
var cell = document.createElement("TD");
//append buttons to the cell
cell.appendChild(save);
cell.appendChild(cancel);
//append cell to the row
row.appendChild(cell);
}