44 lines
920 B
JavaScript
44 lines
920 B
JavaScript
"use strict";
|
|
|
|
// the last search query
|
|
var last_search = null;
|
|
|
|
function search()
|
|
{
|
|
const terms = document.getElementById('terms');
|
|
if (terms && terms.value && terms.value.length >= 3) {
|
|
console.log(terms.value);
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
last_search = '/search/' + terms.value;
|
|
let xhr = new XMLHttpRequest();
|
|
xhr.open('GET', last_search, true);
|
|
xhr.onload = function() {
|
|
if (this.responseURL.endsWith(last_search)) {
|
|
populate(JSON.parse(this.response));
|
|
}
|
|
};
|
|
xhr.onerror = function() {
|
|
console.log("search call failed");
|
|
};
|
|
xhr.send();
|
|
}
|
|
|
|
function populate(parts)
|
|
{
|
|
const results = document.getElementById('results');
|
|
results.innerHTML = null;
|
|
for (const part of parts) {
|
|
const tr = document.createElement('tr');
|
|
for (const key of ["name", "description"]) {
|
|
const td = document.createElement('td');
|
|
td.innerHTML = part[key];
|
|
tr.appendChild(td);
|
|
}
|
|
results.appendChild(tr);
|
|
}
|
|
}
|
|
|