forked from nighthawkcoders/flask_portfolio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.html
103 lines (96 loc) · 4.03 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
<!DOCTYPE html>
<!-- Preparing search using asynchronous "fetch" (no page reload, no wait)
inspiration from https://www.youtube.com/watch?v=QKcVjdLEX_s
-->
{% extends "layouts/layoutcrud.html" %}
{% block meta %}
<title>SQL search</title>
{% endblock %}
{% block content %}
<div style="font-family: monospace" class="container py-4 bg-primary">
<!-- heading -->
<header class="pb-3 mb-4 border-bottom">
<a href="#" class="d-flex align-items-center text-light text-decoration-none">
<span class="fs-4">User Database Search</span>
</a>
</header>
<!-- search box -->
<div class="container">
<div class="row">
<div class="col">
<div class="mb-3">
<div class="form-group">
<label for="term"></label><input type="text" class="form-control" id="term" placeholder="enter search term">
<label></label> <!-- used for spacer -->
</div>
<button class="btn btn-primary bg-secondary" id="myBtn" onclick="search_data();">Search</button>
</div>
</div>
</div>
</div>
</div>
<!-- result of search -->
<div style="font-family: monospace" class="container">
<br><br>
<div class="row" id="result">
</div>
</div>
<script>
function search_data() {
// fetch standard requires data set to a name-value pair
const term = document.getElementById("term");
const body = {
term: term.value
};
// fetch call with header options
fetch('/search/term/', {
method: "POST",
credentials: "include",
body: JSON.stringify(body),
cache: "no-cache",
headers: new Headers({
"content-type": "application/json"
})
})
// async then replies with response header
.then(function (response) {
// prepare HTML search result container for new output
const resultContainer = document.getElementById("result");
// clean up from previous search
while (resultContainer.firstChild) {
resultContainer.removeChild(resultContainer.firstChild);
}
// trap error response from Web API
if (response.status !== 200) {
const errorMsg = 'Database response error: ' + response.status;
console.log(errorMsg);
const div = document.createElement("div");
div.innerHTML = errorMsg;
resultContainer.appendChild(div);
return;
}
// response contains valid result
response.json().then(function(data) {
// loop through JSON and build HTML output
for (let i = 0; i < data.length; i++) {
const div = document.createElement("div");
div.innerHTML = data[i].name + ' ' + data[i].email;
resultContainer.appendChild(div);
}
})
})
}
// Get the input field
const input = document.getElementById("term");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById("myBtn").click();
}
});
</script>
{% endblock %}