英文:
Live update results from database wih every charachter the user types in the search bar
问题
我想根据用户在搜索栏中输入的字符来实时更新结果表格。
这是我的 search.ejs:
<div class="search">
<form action="/search" method="post">
<div class="form-floating mb-3">
<input id="search-bar" type="text" class="form-control" id="name" placeholder="name" name="name" required>
<label for="name">name</label>
</div>
<button type="submit" class="btn btn-primary btn-md">Search</button>
</form>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">First name </th>
<th scope="col">Last name</th>
<th scope="col">Number</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
<% array.forEach(item => { %>
<th><%= item.firstname %> </th>
<th><%= item.lastname %> </th>
<th><%= item.number %> </th>
<th><%= item.date %> </th>
<% }) %>
</tbody>
</table>
这是监听用户键入的键的 script.js:
document.getElementById('search-bar').addEventListener('keyup', function() {
const searchTerm = this.value;
fetch(`/search?q=${searchTerm}`)
});
这是我的 server.js:
app.get('/search', function(req, res) {
var title = "";
var body = "";
var script = "";
var classe= "";
var style = "";
const q = req.query.q;
console.log(q);
connection.query('SELECT * FROM STUDENT WHERE firstname LIKE ?',['%' + q + '%'],function(err,data){
if(err){
console.log(err);
}
else{
res.render('search',{style:style,classe:classe,array:data,modalTitle:title,modalBody:body,script});
}
});
});
在这里,我正在查询我的数据库以获取以用户输入的字符开头的学生姓名。当我将数据记录到控制台时,我得到了我想要的响应,每次按键都会更新,但是发送到模板的数组为空,因此在表格上没有数据显示。我理解在第一次渲染页面时,初始数组是空的,但我的问题是如何在从数据库获取数组后再次将数组渲染到我的模板?
我尝试了很多次,但数组总是空的,无法发送到模板。
英文:
I want to live update a table f results based on the charachters the user type in the search-bar
This is my search.ejs
<div class="search">
<form action="/search" method="post">
<div class="form-floating mb-3">
<input id="search-bar" type="text" class="form-control" id="name" placeholder="name" name="name" required>
<label for="name">name</label>
</div>
<button type="submit" class="btn btn-primary btn-md">Search</button>
</form>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">First name </th>
<th scope="col">Last name</th>
<th scope="col">Number</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
<% array.forEach(item => { %>
<th><%= item.firstname %> </th>
<th><%= item.lastname %> </th>
<th><%= item.number %> </th>
<th><%= item.date %> </th>
<% }) %>
</tbody>
</table>
This is script.js to listen to keys typed in by the user
document.getElementById('search-bar').addEventListener('keyup', function() {
const searchTerm = this.value;
fetch(`/search?q=${searchTerm}`)
});
This is my server.js
app.get('/search', function(req, res) {
var title = "";
var body = "";
var script = "";
var classe= "";
var style = "";
const q = req.query.q;
console.log(q);
connection.query('SELECT * FROM STUDENT WHERE firstname LIKE ?',[q+"%"],function(err,data){
if(err){
console.log(err);
}
else{
res.render('search',{style:style,classe:classe,array:data,modalTitle:title,modalBody:body,script});
}
});
});
Here I am querying my database for the name of students starting with the charachters typed by the user, when i log the data to the consol i get the response just like i want nad it' updated with evey key stroke, but the the array sent to the template is empty so i don't get in data to show on the table, i understand that the initial array is sent empty while rendering the page for the first time, but my question is how to render the array again to my template after getting the array from the database ?
I tried a lot but the array is always sent empty to the template
答案1
得分: 1
fetch
调用在调用后返回 HTML 文档,但实际上你并没有对响应进行任何操作。因此,页面将始终保持不变。而不是发送整个页面,你应该创建一个新的路由,仅发送搜索结果数组,然后使用 JavaScript 在页面上显示结果,而不是使用 EJS(EJS 仅在首次渲染页面时进行评估,但对于搜索结果等动态数据,你必须使用 JavaScript 手动更新页面中的 DOM)。
英文:
The fetch
call is returning the HTML document after it's called, but you're not actually doing anything with the response. Thus, the page will always remain the same. Instead of sending back the entire page, you should make a new route that only sends back the array of search results, and then use javascript to display the results on the page instead of using EJS (EJS is only evaluated when the page is rendered for the first-time, but for dynamic data like the results of a search you have to use javascript to manually update the DOM in the page).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论