javascript - 从数据库中使用 javascript 搜索栏

标签 javascript database searchbar

对于我的项目,我无法使用 php。我发现的关于带有数据库的搜索栏的所有内容都是在 php 或 html 中(没有数据库)。这就是为什么我被困住了。使用此代码,我可以访问我的数据库,并且它确实正确显示了我列表中的所有工作室。但现在的问题是我应该如何做一个与这些信息相关的搜索栏呢? (我不在乎它是否只搜索名称(ateliers.nom)或全部) 希望你能帮助我!

<input type="text" id = "search" placeholder="search...">
<input type="submit" name = 'searchsubmit' value="Rechercher">
<table>
    <tr>
        <th>Atelier</th>
        <th>Nombres d'inscrits</th>
        <th>Lieu</th>
        <th>Chef</th>
        <th>Date</th>
        <th>Places disponibles</th>
    </tr>
    <c:forEach var="ateliers" items="${listAteliers}" varStatus="status">
        <tr>
            <th><c:out value="${ateliers.nom}" default="Empty"></c:out></th>
            <th><c:out value="${ateliers.nbinscrit}" default="Empty"></c:out></th>
            <th><c:out value="${ateliers.lieu}" default="Empty"></c:out></th>
            <th><c:out value="${ateliers.chef}"></c:out></th>
            <th><c:out value="${ateliers.date}" default="Empty"></c:out></th>
            <th><c:out value="${ateliers.place}" default="Empty"></c:out></th>
            <th><a href="<c:url value="./atelierdisplay/${listAteliers[status.index].id}"/>">Afficher</a></th>
            <th><a href="<c:url value="./atelieredit/${listAteliers[status.index].id}"/>">Editer</a></th>
        </tr>
    </c:forEach>
</table>

最佳答案

function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
#myInput {
    background-image: url('/css/searchicon.png'); /* Add a search icon to input */
    background-position: 10px 12px; /* Position the search icon */
    background-repeat: no-repeat; /* Do not repeat the icon image */
    width: 100%; /* Full-width */
    font-size: 16px; /* Increase font-size */
    padding: 12px 20px 12px 40px; /* Add some padding */
    border: 1px solid #ddd; /* Add a grey border */
    margin-bottom: 12px; /* Add some space below the input */
}

#myTable {
    border-collapse: collapse; /* Collapse borders */
    width: 100%; /* Full-width */
    border: 1px solid #ddd; /* Add a grey border */
    font-size: 18px; /* Increase font-size */
}

#myTable th, #myTable td {
    text-align: left; /* Left-align text */
    padding: 12px; /* Add padding */
}

#myTable tr {
    /* Add a bottom border to all table rows */
    border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
    /* Add a grey background color to the table header and on hover */
    background-color: #f1f1f1;
}
 <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">

<table id="myTable">
  <tr class="header">
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
</table> 

关于javascript - 从数据库中使用 javascript 搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44412360/

相关文章:

javascript - CoffeeScript 函数参数

c# - RavenDB - 筛选文档并使用索引/查询获取计数

javascript - 消除键盘输入的抖动?

sql - 在数据仓库中使用 View

android - Android 应用的搜索栏

ios - 打字时搜索栏消失

javascript - 如何使用 PhantomJS 提交表单?

javascript - 当我将其上方的div更改为固定时,如何阻止固有定位的元素跳转页面?

javascript - 如何限制我的 React 组件的大小并添加滚动?

c# - ASP.NET MVC - 我在数据库中存储和显示图像时遇到问题