【问题标题】:How to filter a table using Values and Color using javascript如何使用 JavaScript 使用值和颜色过滤表
【发布时间】:2020-08-13 09:20:59
【问题描述】:

我有用于按值过滤表的代码,

function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  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) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}

这是我的桌子

Table

这是生成表格的脚本

$(document).ready(function(){
 $('#load_data').click(function(){
  $.ajax({
   url:"OutputNew.csv",
   dataType:"text",
   success:function(data){
    var employee_data = data.split(/\r?\n|\r/);
    var table_data = '<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search Color"><table id="myTable" class="table table-bordered table-striped">';
    for(var count = 0; count<employee_data.length; count++) {
     var cell_data = employee_data[count].split(',');
     table_data += '<tr>';
     for(var cell_count=0; cell_count<cell_data.length; cell_count++){
      if(count === 0){
       table_data += '<th>'+cell_data[cell_count]+'</th>';
      }else{
          if(cell_data[cell_count] .includes("Not Matching")){
                var ret = cell_data[cell_count].replace('Not Matching','');
                if (ret == ""){
                  table_data += '<td>'+ret+'</td>'
                }else{
                  table_data += '<td><span class="badge-danger">'+ret+'</span></td>'
                }
          }else if(cell_data[cell_count] .includes("Matching")){
                var ret = cell_data[cell_count].replace('Matching','');
                if (ret == ""){
                  table_data += '<td>'+ret+'</td>'
                }else{
                  table_data += '<td><span class="badge-complete">'+ret+'</span></td>';
                }
          }else{
              table_data += '<td>'+cell_data[cell_count]+'</td>';
          }
      }
     }
     table_data += '</tr>';
    }
    table_data += '</table>';
    $('#employee_table').html(table_data);
   }
  });   
 }); 
});

CSS

div#loadbutton {
    margin-left: 158px;
}
h1#Heading {
    margin-left: 154px;
}
td { 
    border: 1px solid black; 
    word-wrap: break-word; 
} 
input#myInput {
    width: 343px;
    height: 49px;
    border-radius: 21px;
}
.table-responsive {
    min-height: .01%;
    overflow-x: auto;
    WIDTH: 223%;
    MARGIN-LEFT: -205PX;
}
.badge-Nill{
  display: inline-block;
    min-width: 49px;
    padding: 9px 9px;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    background-color: blue;
    border-radius: 10px;
    width: 127px;
}
.badge-danger {
    display: inline-block;
    min-width: 49px;
    padding: 9px 9px;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    background-color: red;
    border-radius: 10px;
    width: 127px;
    
}
th {
    text-align: left;
    width: 152px;
}
table {border-collapse:collapse; table-layout:fixed; width:310px;}
table td {border:solid 1px #fab; width:100px; word-wrap:break-word;}
.badge-complete {
    display: inline-block;
    min-width: 49px;
    padding: 9px 9px;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    color: #fff;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    background-color: limegreen;
    border-radius: 10px;
    width: 127px;
}
div#employee_table {
    margin-left: 170px;
    margin-right: 70px;
}

所以它过滤值,在这里我想使用 Value 和 color 过滤表。例如,如果我键入 ABSRED 它应该检索值为 ABS 和颜色 Red 的列。 .有什么办法吗?

【问题讨论】:

  • 单元格的颜色在 HTML 中是如何表示的?
  • 你需要显示 HTML 和 CSS... make minimal reproducible example Se 我们可以看到你是如何添加颜色的。
  • 能否也为表格添加 HTML 文件?
  • 是的当然@keidakida
  • 是的,我刚刚更新了代码@ikiK

标签: javascript filter


【解决方案1】:

顺便说一句,您还可以添加 AJAX 的示例数据,这将成为真正的 Minimal, Reproducible Example ,这样我就可以制作我赢得的 HTML 表格。

您正在将具有类&lt;span class="badge-danger"&gt;badge-complete 的跨度添加到td 中,因此只需简单地搜索innerHTML 而不是textContentinnerTexttd's 或整个tr 来查找这些类名.

为了能够搜索“颜色”,获取输入值,如果匹配red,将其切换到badge-danger 并搜索。绿色也是如此:

  if (filter === "RED") {
    filter = "badge-danger";
  } else if (filter === "GREEN") {
    filter = "badge-complete";
  }

这是您搜索列的方式,在下面的示例中,这将搜索前 3 列:

  for (var i = 0; i < rows.length; i++) {
    var Col1 = rows[i].cells[0].innerHTML;
    var Col2 = rows[i].cells[1].innerHTML;
    var Col3 = rows[i].cells[2].innerHTML;
    if (Col1.indexOf(filter) > -1 ||
      Col2.indexOf(filter) > -1 ||
      Col3.indexOf(filter) > -1) {
      rows[i].style.display = "";
    } else {
      rows[i].style.display = "none";
    }
  }

因此,从您发布的图片来看,您需要定位最后 4 列,如下所示:

    for (var i = 0; i < rows.length; i++) {
    var Col1 = rows[i].cells[4].innerHTML;
    var Col2 = rows[i].cells[5].innerHTML;
    var Col3 = rows[i].cells[6].innerHTML;
    var Col4 = rows[i].cells[7].innerHTML;
    if (Col1.indexOf(filter) > -1 ||
      Col2.indexOf(filter) > -1 ||
      Col3.indexOf(filter) > -1 ||
      Col4.indexOf(filter) > -1 ) {
      rows[i].style.display = "";
    } else {
      rows[i].style.display = "none";
    }
  }

颜色示例:

function filterTable(event) {
  var filter = document.getElementById("myInput").value.toUpperCase();
  var rows = document.querySelector("#myTable tbody").rows;

  if (filter === "RED") {
    filter = "badge-danger";
  } else if (filter === "GREEN") {
    filter = "badge-complete";
  }

  for (var i = 0; i < rows.length; i++) {
    var Col1 = rows[i].cells[0].innerHTML;
    var Col2 = rows[i].cells[1].innerHTML;
    var Col3 = rows[i].cells[2].innerHTML;
    if (Col1.indexOf(filter) > -1 ||
      Col2.indexOf(filter) > -1 ||
      Col3.indexOf(filter) > -1) {
      rows[i].style.display = "";
    } else {
      rows[i].style.display = "none";
    }
  }
}

document.querySelector('#myInput').addEventListener('keyup', filterTable, false);
div#loadbutton {
  margin-left: 158px;
}

h1#Heading {
  margin-left: 154px;
}

td {
  border: 1px solid black;
  word-wrap: break-word;
}

input#myInput {
  width: 343px;
  height: 49px;
  border-radius: 21px;
}

.table-responsive {
  min-height: .01%;
  overflow-x: auto;
  WIDTH: 223%;
  MARGIN-LEFT: -205PX;
}

.badge-Nill {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: blue;
  border-radius: 10px;
  width: 127px;
}

.badge-danger {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: red;
  border-radius: 10px;
  width: 127px;
}

th {
  text-align: left;
  width: 152px;
}

table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 310px;
}

table td {
  border: solid 1px #fab;
  width: 100px;
  word-wrap: break-word;
}

.badge-complete {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: limegreen;
  border-radius: 10px;
  width: 127px;
}

div#employee_table {
  margin-left: 170px;
  margin-right: 70px;
}
<input type="text" id="myInput" name="myInput" onkeyup="filterTable()" placeholder="color"><br>

<table id="myTable">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Points</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="badge-danger">Jill</span></td>
      <td>Smith</td>
      <td>50</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td><span class="badge-danger">94</span></td>
    </tr>
    <tr>
      <td>Adam</td>
      <td><span class="badge-danger">Johnson</span></td>
      <td>67</td>
    </tr>
    <tr>
      <td>Bo</td>
      <td><span class="badge-complete">Nilson</span></td>
      <td>35</td>
    </tr>
  </tbody>
</table>

组合: 将其结合起来需要更多的工作:

当您搜索名称时,它会过滤这些名称,但是当您在该颜色之上搜索颜色时,它会找到所有颜色而不考虑名称,因此您需要说只搜索那些尚未隐藏的行:

if (tr[i].style.display !== "none") {
  tr[i].style.display = "";
}

您还必须像这样调节颜色:

if (filter === "RED" || filter === "R" || filter === "RE") {
    filter = "badge-danger";
  }

因为如果你只留下red这个词,当你输入后面的r时,它会全部隐藏,我们有条件只搜索不隐藏,因为只有red这个词会翻译成badge-danger。在文本上你不需要那个。或者制作一个带有颜色名称的下拉列表...

这是在一个输入被清空时全部重置:

if (filter==="") {
document.getElementById("myInput").value="";
document.getElementById("myInputtext").value="";
[...document.querySelectorAll("#myTable tbody tr")].forEach(el => { el.style.display = "" });
}

组合示例: 搜索adambogreenred...

function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInputtext");
  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) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        if (tr[i].style.display !== "none") {
          tr[i].style.display = "";
        }
      } else {
        tr[i].style.display = "none";
      }
    }
  }
  
if (filter==="") {
document.getElementById("myInput").value="";
document.getElementById("myInputtext").value="";
[...document.querySelectorAll("#myTable tbody tr")].forEach(el => { el.style.display = "" });
}
}





function filterTable(event) {
  var filter = document.getElementById("myInput").value.toUpperCase();
  var rows = document.querySelector("#myTable tbody").rows;

  if (filter === "RED" || filter === "R" || filter === "RE") {
    filter = "badge-danger";
  } else if (filter === "GREEN" || filter === "G" || filter === "GR" || filter === "GRE" || filter === "GREE") {
    filter = "badge-complete";
  }

  for (var i = 0; i < rows.length; i++) {
    var Col1 = rows[i].cells[0].innerHTML;
    var Col2 = rows[i].cells[1].innerHTML;
    var Col3 = rows[i].cells[2].innerHTML;
    if (Col1.indexOf(filter) > -1 ||
      Col2.indexOf(filter) > -1 ||
      Col3.indexOf(filter) > -1) {
      if (rows[i].style.display !== "none") {
        rows[i].style.display = "";
      }
    } else {
      rows[i].style.display = "none";
    }
  }
if (filter==="") {
document.getElementById("myInput").value="";
document.getElementById("myInputtext").value="";
[...document.querySelectorAll("#myTable tbody tr")].forEach(el => { el.style.display = "" });
}    
}

document.querySelector('#myInputtext').addEventListener('keyup', myFunction, false);
document.querySelector('#myInput').addEventListener('keyup', filterTable, false);
div#loadbutton {
  margin-left: 158px;
}

h1#Heading {
  margin-left: 154px;
}

td {
  border: 1px solid black;
  word-wrap: break-word;
}

.table-responsive {
  min-height: .01%;
  overflow-x: auto;
  WIDTH: 223%;
  MARGIN-LEFT: -205PX;
}

.badge-Nill {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: blue;
  border-radius: 10px;
  width: 127px;
}

.badge-danger {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: red;
  border-radius: 10px;
  width: 127px;
}

th {
  text-align: left;
  width: 152px;
}

table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 310px;
}

table td {
  border: solid 1px #fab;
  width: 100px;
  word-wrap: break-word;
}

.badge-complete {
  display: inline-block;
  min-width: 49px;
  padding: 9px 9px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  background-color: limegreen;
  border-radius: 10px;
  width: 127px;
}

div#employee_table {
  margin-left: 170px;
  margin-right: 70px;
}
<input type="text" id="myInput" name="myInput" placeholder="color"><br>
<input type="text" id="myInputtext" name="myInput" placeholder="First Name"><br>

<table id="myTable">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Points</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span class="badge-danger">Jill</span></td>
      <td>Smith</td>
      <td>50</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td><span class="badge-danger">94</span></td>
    </tr>
    <tr>
      <td>Adam</td>
      <td><span class="badge-danger">Johnson</span></td>
      <td>67</td>
    </tr>
    <tr>
      <td>Adam</td>
      <td><span class="badge-complete">Johnson</span></td>
      <td>67</td>
    </tr>
    <tr>
      <td>Bo</td>
      <td><span class="badge-complete">Nilson</span></td>
      <td>35</td>
    </tr>
    <tr>
      <td>Bo</td>
      <td><span class="badge-danger">Nilson</span></td>
      <td>35</td>
    </tr>
  </tbody>
</table>

参考how to filter multiple columns, or whole rows

很确定还有改进的余地,但我在这方面浪费了足够多的时间,这足以让你开始并了解如何去做。

【讨论】:

  • document.querySelector('#myInputtext').addEventListener('keyup', myFunction, false); ===> 遇到这样的错误 \
  • @neerajasreelaxmi ?!
  • 您是否将输入的 id 调整为 myInputtext,您面前有可行的解决方案...什么是错误? @neerajasreelaxmi
猜你喜欢
  • 2021-05-24
  • 2014-12-20
  • 2013-09-07
  • 2020-06-27
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 2019-10-11
  • 2021-06-16
相关资源
最近更新 更多