-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.js
More file actions
115 lines (105 loc) · 4.34 KB
/
crud.js
File metadata and controls
115 lines (105 loc) · 4.34 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
const api_url = "http://192.168.1.12:1234/users"
// const api_url = "http://192.168.249.221:1234/users"
function loadData(records = []) {
var table_data = "";
for(let i=0; i<records.length; i++) {
table_data += `<tr>`;
table_data += `<td>${records[i]['RollNo']}</td>`;
table_data += `<td>${records[i]['FirstName']}</td>`;
table_data += `<td>${records[i]['LastName']}</td>`;
table_data += `<td>${records[i]['City']}</td>`;
table_data += `<td>${records[i]['Age']}</td>`;
table_data += `<td>${records[i]['ItemList']}</td>`;
table_data += `<td>`;
table_data += `<a href="edit.html?RollNo=${records[i]['RollNo']}"><button class="btn btn-primary">Edit</button></a>`;
table_data += ' ';
table_data += `<button class="btn btn-danger" onclick=deleteData('${records[i]['RollNo']}')>Delete</button>`;
table_data += `</td>`;
table_data += `</tr>`;
}
console.log(table_data);
document.getElementById("tbody").innerHTML = table_data;
}
function getData(){
fetch(api_url)
.then((response) => response.json())
.then((data) =>{
loadData(data);
});
}
function getDataByID(RollNo){
fetch(`${api_url}edit?RollNo=${RollNo}`)
.then((response) => response.json())
.then((data) =>{
console.log(data);
document.getElementID("RollNo").value = data[0]['RollNo']
document.getElementID("FirstName").value = data[0]['FirstName']
document.getElementID("LastName").value = data[0]['LastName']
document.getElementID("Age").value = data[0]['Age']
document.getElementID("City").value = data[0]['City']
document.getElementID("ItemList").value = data[0]['ItemList']
});
}
function postData() {
var RollNo = document.getElementById("RollNo").value;
var FirstName = document.getElementById("FirstName").value;
var LastName = document.getElementById("LastName").value;
var Age = document.getElementById("Age").value;
var City = document.getElementById("City").value;
var ItemList = document.getElementById("ItemList").value;
data = {RollNo: RollNo, FirstName: FirstName, LastName: LastName, Age: Age, City: City, ItemList: ItemList};
fetch(api_url, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then((response) => response.json())
.then((data) => {
console.log(data);
window.location.href = "index.html";
})
}
function putData() {
var RollNo = document.getElementById("RollNo").value;
var FirstName = document.getElementById("FirstName").value;
var LastName = document.getElementById("LastName").value;
var Age = document.getElementById("Age").value;
var City = document.getElementById("City").value;
var ItemList = document.getElementById("ItemList").value;
data = {RollNo: RollNo, FirstName: FirstName, LastName: LastName, Age: Age, City: City, ItemList: ItemList};
fetch(api_url, {
method: "PUT",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then((response) => response.json())
.then((data) => {
console.log(data);
window.location.href = "index.html";
})
}
function deleteData(RollNo) {
console.log(RollNo);
user_input = confirm("Are you sure you want to delete this record?");
if(user_input) {
fetch(api_url+"?rollno="+RollNo, {
method: "DELETE",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
//body: JSON.stringify({"RollNo": RollNo})
})
.then((response) => response.json())
.then((data) => {
console.log(data);
window.location.reload();
})
}
}