forked from bandipurhouse/studentdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
49 lines (44 loc) · 1.19 KB
/
script.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
var sbar = document.querySelector('#sbar')
var sbut = document.querySelector('#sbut')
var ssel = document.querySelector('#ssel')
var students = document.querySelector('#students')
db = null
function loadFile(filePath) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filePath, false);
xmlhttp.send();
db = xmlhttp.status == 200 ? xmlhttp.responseText : null;
}
function search(query){
if(query == "") return db;
rval = ""
for(var i = 0; i < dblist.length; i++){
entry = dblist[i]
field = entry.split('\t')[ssel.selectedIndex]
if(field.match(query)){
rval += dblist[i] + "\n";
}
}
return rval;
}
function searchcallback(){
query = sbar.value;
dval = search(query);
students.innerHTML = dval;
}
loadFile('db/list')
while(db == null){
setTimeout(loadFile('db/list'),100);
console.log('db load failed, reloading');
}
console.log('db loaded');
if(db.endsWith('\n')) db = db.slice(0,db.length-1)
dval = db;
dblist = db.split('\n')
students.innerHTML = dval;
sbut.addEventListener('click', searchcallback);
sbar.addEventListener("keyup", ({key}) => {
if (key === "Enter") {
searchcallback()
}
})