-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.html
224 lines (219 loc) · 6.59 KB
/
search.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!-- Simple app to search in json file
file="students.json"
-->
<html>
<head>
<title>Search Window</title>
</head>
<body>
<!-- responsive page -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- bootstrap -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
/>
<h1>Search Window</h1>
<input
type="text"
id="search"
placeholder="Search with Name/Entry Number/Kerberos/Course"
/>
<!-- take input max results to show -->
<!-- dropdown to select maxResults 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 10000 -->
<select id="maxResults">
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="2000">2000</option>
<option value="10000">10000</option>
</select>
<div id="result"></div>
<script>
// read file
var students = new XMLHttpRequest();
students.open('GET', 'students.json', false);
students.send(null);
var students = JSON.parse(students.responseText);
function showmore(name) {
// set the xx div visible
var xx = document.getElementById('xx' + name);
if (xx.style.display === 'none') {
xx.style.display = 'block';
} else {
xx.style.display = 'none';
}
}
function getTable(st) {
var html = '';
html += '<table class="table table-striped table-bordered">';
// columns are semester names {semester1, semester2, ...}
// sample course 2102-ASL340. extract semester as 2102
var columns = '';
for (var i = 0; i < st.courses.length; i++) {
var course = st.courses[i];
var semester = course.substring(0, 4);
if (columns.indexOf(semester) == -1) {
columns += semester + ',';
}
}
columns = columns.split(',');
columns.pop();
for (var i = 0; i < columns.length; i++) {
html += '<th>' + columns[i] + '</th>';
}
html += '</tr>';
// rows are courses
var courses = [[]];
for (var i = 0; i < st.courses.length; i++) {
var course = st.courses[i];
var semester = course.substring(0, 4);
var course = course.substring(5);
var index = columns.indexOf(semester);
if (courses[index] == undefined) {
courses[index] = [];
}
courses[index].push(course);
}
// transpose the courses
var max = 0;
for (var i = 0; i < courses.length; i++) {
if (courses[i].length > max) {
max = courses[i].length;
}
}
for (var i = 0; i < max; i++) {
html += '<tr>';
for (var j = 0; j < courses.length; j++) {
if (courses[j][i] == undefined) {
html += '<td></td>';
} else {
html += '<td>' + courses[j][i] + '</td>';
}
}
html += '</tr>';
}
// add courses to respective columns
// semester is a column
// for(var i=0;i<st.courses.length;i++) {
// var course = st.courses[i];
// var semester = course.substring(0,4);
// var course = course.substring(5);
// html += '<tr>';
// for(var j=0;j<columns.length;j++) {
// if(columns[j] == semester) {
// html += '<td>' + course + '</td>';
// } else {
// html += '<td></td>';
// }
// }
// html += '</tr>';
// }
html += '</table>';
// html += '<tr><th>Sr</th><th>Course</th></tr>';
// for (var i = 0; i < st.courses.length; i++) {
// html +=
// '<tr><td>' + (i + 1) + '</td><td>' + st.courses[i] + '</td></tr>';
// }
// html += '</table>';
return html;
}
function getdata(st) {
// return html with formatted data of each student
var html = '';
html += '<div>';
html += '<h3>' + st.name + '</h3>';
html += '<p>' + st.kerberos + '</p>';
// show button to show more info and pass student object
html +=
'<button onclick=\'showmore("' + st.name + '")\'>Courses</button>';
// show more info
html += "<div style='display:none' id='xx" + st.name + "'>";
// display courses in table
html += getTable(st);
html += '</div>';
html += '</div>';
return html;
}
function kerberosToEntry(kerberos) {
var entry = '';
// kerberos = cs5190421 to entry = 2019CS50421
// year = 19
// dept = CS5
// roll = 0421
var year = kerberos.substring(3, 5);
var dept = kerberos.substring(0, 3);
var roll = kerberos.substring(5);
entry = '20' + year + dept + roll;
// entry = '20' + year + dept + roll;
return entry;
}
// ismatch
function ismatch(st, search) {
if (st.name.toLowerCase().indexOf(search) != -1) {
return true;
}
if (st.kerberos.toLowerCase().indexOf(search) != -1) {
return true;
}
for (var i = 0; i < st.courses.length; i++) {
if (st.courses[i].toLowerCase().indexOf(search) != -1) {
return true;
}
}
if (kerberosToEntry(st.kerberos.toLowerCase()).indexOf(search) != -1) {
return true;
}
if (st.kerberos.toLowerCase().indexOf(search) != -1) {
return true;
}
return false;
}
// search function
function searchStudents() {
var search = document.getElementById('search').value;
var result = document.getElementById('result');
var html = '';
var res = [];
for (var i = 0; i < students.length; i++) {
// var result = students[i].name.toLowerCase().indexOf(search.toLowerCase());
// var result = ismatch(students[i],search.toLowerCase());
// console.log(result);
// search case insensitive
if (
// result==true
ismatch(students[i], search.toLowerCase())
) {
// show name, kerberos, courses registered
// console.log(students[i]);
res.push(students[i]);
// html += getdata(students[i]);
// html += "<div>" + students[i].name + " " + students[i].kerberos + " " + students[i].courses + "</div>";
}
}
res.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
var maxResults = document.getElementById('maxResults').value;
for (var i = 0; i < res.length && i < maxResults; i++) {
// for (var i = 0; i < max(res.length, maxResults); i++) {
html += getdata(res[i]);
}
result.innerHTML = html;
}
// show result when typing
document
.getElementById('search')
.addEventListener('keyup', searchStudents);
// show result when range is changed
document
.getElementById('maxResults')
.addEventListener('change', searchStudents);
</script>
</body>
</html>