-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathuniversity.js
156 lines (141 loc) · 4.28 KB
/
university.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var SERVER_URL = "http://dev.cs.smu.ca:8171";
//var SERVER_URL = "http://127.0.0.1:8171"
function saveUniversity() {
// $("#universityForm").validate()
if ($("#name").val() == "" || $("#phone").val() == "" || $("#address").val() == "") {
$("#name").focus();
$("#error_alert").append("All three options cannot be null");
setTimeout(function () {
$("#error_alert").empty();
}, 800);
} else {
var param = {
Name: $("#name").val(),
Address: $("#address").val(),
PhoneNumber: $("#phone").val()
}
console.log(SERVER_URL + "/saveUniversity");
$.post(SERVER_URL + "/saveUniversity", param, function (data) {
alert(data);
}).fail(function (error) {
if (error.responseText == "") {
alert("There is no available running server");
} else {
console.log(error);
alert(error);
}
});
}
}
function deleteUniversity() {
if ($("#name").val() == "") {
$("#name").focus();
$("#error_alert").append("delete name cannot be null");
setTimeout(function () {
$("#error_alert").empty();
}, 800);
} else {
var param = {
Name: $("#name").val(),
Address: $("#address").val(),
PhoneNumber: $("#phone").val()
};
$.post(SERVER_URL + "/deleteUniversity", param, function (data) {
alert(data);
}).fail(function (error) {
if (error.responseText == "") {
alert("There is no available running server");
} else {
console.log(error);
alert(error.responseText);
}
});
}
}
function searchRecord() {
if ($("#search_keyword").val() == "") {
$("#search_keyword").focus();
$("#error_alert").append("search key word cannot be null");
setTimeout(function () {
$("#error_alert").empty();
}, 2000);
} else {
var newObj = {
Name: $("#search_keyword").val()
};
$.post(SERVER_URL + "/searchUniversity", newObj, function (data) {
var trHTML = 'Below are all the records:' + '<thead>\n' +
' <tr>\n' +
' <th scope="col">University Name</th>\n' +
' <th scope="col">Email Address</th>\n' +
' <th scope="col">Phone Number</th>\n' +
' </tr>\n' +
' </thead>\n' +
' <tbody>';
var result = [];
for (i = 0; i < data.length; i++) {
result.push([])
result[i].push(data[i]["Name"])
result[i].push(data[i]["Address"])
result[i].push(data[i]["PhoneNumber"])
}
var $select = $('#search_result');
var listitems = '';
for (i = 0; i < result.length; i++) {
trHTML += '<tr>';
for (j = 0; j < result[i].length; j++) {
trHTML += '<td>' + result[i][j] + '</td>';
}
trHTML += '/<tr>';
trHTML += '/<tbody>';
}
$('#search_result').empty();
$('#search_result').append(trHTML);
}).fail(function (error) {
if (error.responseText == "") {
alert("There is no available running server");
} else {
console.log(error);
alert(error.responseText);
}
});
}
}
function displayRecords() {
$.post(SERVER_URL + "/displayUniversity", function (data) {
var trHTML = 'Below are all the records:' + '<thead>\n' +
' <tr>\n' +
' <th scope="col">University Name</th>\n' +
' <th scope="col">Email Address</th>\n' +
' <th scope="col">Phone Number</th>\n' +
' </tr>\n' +
' </thead>\n' +
' <tbody>';
var result = [];
for (i = 0; i < data.length; i++) {
result.push([])
result[i].push(data[i]["Name"])
result[i].push(data[i]["Address"])
result[i].push(data[i]["PhoneNumber"])
}
var $select = $('#search_result');
var listitems = '';
for (i = 0; i < result.length; i++) {
trHTML += '<tr>';
for (j = 0; j < result[i].length; j++) {
trHTML += '<td>' + result[i][j] + '</td>';
}
trHTML += '/<tr>';
trHTML += '/<tbody>';
}
$('#search_result').empty();
$('#search_result').append(trHTML);
}).fail(function (error) {
if (error.responseText == "") {
alert("There is no available running server");
} else {
console.log(error);
alert(error.responseText);
}
});
}