从数据库中实时更新搜索栏中用户输入的每个字符的结果。

huangapple go评论80阅读模式
英文:

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

&lt;div class=&quot;search&quot;&gt;
    &lt;form action=&quot;/search&quot; method=&quot;post&quot;&gt;
      &lt;div class=&quot;form-floating mb-3&quot;&gt;
        &lt;input id=&quot;search-bar&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name&quot; placeholder=&quot;name&quot; name=&quot;name&quot; required&gt;
        &lt;label for=&quot;name&quot;&gt;name&lt;/label&gt;
      &lt;/div&gt;
      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-md&quot;&gt;Search&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  &lt;table class=&quot;table table-striped table-hover&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th scope=&quot;col&quot;&gt;First name &lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Last name&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Number&lt;/th&gt;
        &lt;th scope=&quot;col&quot;&gt;Date&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt; 
      &lt;% array.forEach(item =&gt; { %&gt;       
        &lt;th&gt;&lt;%= item.firstname %&gt; &lt;/th&gt;
        &lt;th&gt;&lt;%= item.lastname %&gt; &lt;/th&gt;
        &lt;th&gt;&lt;%= item.number %&gt; &lt;/th&gt;
        &lt;th&gt;&lt;%= item.date %&gt; &lt;/th&gt;
      &lt;% }) %&gt;
    &lt;/tbody&gt;
  &lt;/table&gt; 

This is script.js to listen to keys typed in by the user

document.getElementById(&#39;search-bar&#39;).addEventListener(&#39;keyup&#39;, function() {
  const searchTerm = this.value;
  fetch(`/search?q=${searchTerm}`)
});

This is my server.js

app.get(&#39;/search&#39;, function(req, res) {
  var title = &quot;&quot;;
  var body = &quot;&quot;;
  var script = &quot;&quot;;
  var classe= &quot;&quot;;
  var style = &quot;&quot;;
  const q = req.query.q;
  console.log(q);
  connection.query(&#39;SELECT * FROM STUDENT WHERE firstname LIKE ?&#39;,[q+&quot;%&quot;],function(err,data){
    if(err){
      console.log(err);
    }
    else{
      res.render(&#39;search&#39;,{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).

huangapple
  • 本文由 发表于 2023年1月7日 00:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75033845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定