【问题标题】:Search/Filter multiple table on all columns using W3.CSS Javascript使用 W3.CSS Javascript 在所有列上搜索/过滤多个表
【发布时间】:2017-10-08 16:50:22
【问题描述】:

背景: 我正在为一个班级项目工作,我们必须为我们的虚构公司创建一个 CRM 系统。对于该项目,我们使用 Python、HTML、CSS、W3.CSS、Javascript、Flask、Jinja2、Bootstrap 和 Spyder 的 Python 组合,其余部分在 Sublime 中编辑。

我们为我们显示的表格连接到 SQLite 数据库,“销售人员”或用户可以使用 javascript 循环搜索每个不同页面上显示的表格。

我的问题: 1.当对数据进行搜索时,我只能对最左边的列进行排序。 如何更改 javascript 以便可以通过多个列执行搜索/过滤?

注意:如果有人有兴趣提供我对这个项目的更多想法,请告诉我您是否愿意听听我正在努力完成的工作。

谢谢

链接到 w3schools 搜索/过滤代码: https://www.w3schools.com/howto/howto_js_filter_table.asp

链接到我正在使用的主页模板: https://www.w3schools.com/w3css/tryw3css_templates_social.htm

“客户”页面/表格的代码

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
<title>Clients</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

</style>
</head>
<body>
<br>
<br>
<br>

<table id="Clients" class="w3-table-all">
<!-- Search Function W3 -->
<div class="container">
  <div class="col-lg-6 col-lg-offset-3 text-right">
     <input align="right" type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for Names..">
      </div>
</div>
<caption><h1 style="color:black;"align="center">Clients</h1></caption>
            <tr>
                <th>Name</th>
                <th>Representative ID</th>
                <th>Carrier ID</th>
                <th>Email</th>
                <th>Phone Number</th>
                <th>Company ID</th>

            </tr>


  {% for item in CRM %}



   <tr>
     <td>{{ item[1] }}</td>
     <td>{{ item[2] }}</td>
     <td>{{ item[3] }}</td>
     <td>{{ item[4] }}</td>
     <td>{{ item[5] }}</td>
     <td>{{ item[6] }}</td>

    </tr>

  {% endfor %}
</table>
<!-- Javascript for Search Function -->
<script>
function myFunction() {
  // Declare variables 
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("Clients");
  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";
      }
    } 
  }
}
</script>

</body>
</html>



{% endblock %}

【问题讨论】:

  • 请您发布您编译的 HTML 和 CSS(特别是表格),以便我们重新创建您的问题。还值得注意的是,div 不是table 元素的有效子元素,可能会导致问题。

标签: javascript python html css w3.css


【解决方案1】:

我有脚本问题的解决方案:

function myFunction() {
  // Declare variables 
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("Clients");
  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++) {
    td0 = tr[i].getElementsByTagName("td")[0];
    td1 = tr[i].getElementsByTagName("td")[1];
    td2 = tr[i].getElementsByTagName("td")[2];
    // and so on...
    if (td) {
      if (td0.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        if (td1.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        if (td2.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
      }
      }
    } 
  }
}

你只需要遵循这个方案。 希望我的回答对您有所帮助。
[编辑]
我将解释脚本的作用:
基本上,您正在使用IndexOf() 方法解决一些条件,如果输入和列表中的元素之间没有出现,则返回 -1。因此,例如,如果这样:

td0.innerHTML.toUpperCase().indexOf(filter) 返回-1,这意味着输入和td0 中存在的文本之间不会出现任何情况。因此,您将需要移动到下一个将重复同一件事的条件(用另一个词)。你会一直这样做,直到你走遍了所有的桌子 (td0, td1, td2...)。
每个条件下都写tr[i].style.display = "",表示如果条件成立,我们不隐藏行,但是如果最后没有有效的条件,在最后一个else {}条件下,你会写tr[i].style.display = "none" ,这将隐藏表格中的行。

【讨论】:

  • 感谢您帮助 StackOverflow。不确定您是否注意到这个问题是针对一个班级项目并且是 3 年前提出的。怀疑OP仍在上课。如果您确实希望您的答案能够帮助将来可能有相同问题的人,也许可以通过解释您的脚本正在做什么来扩展您的答案。继续努力。
猜你喜欢
  • 2017-12-04
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2015-12-02
  • 2019-08-15
  • 2021-12-15
  • 1970-01-01
  • 2016-02-23
相关资源
最近更新 更多