使用javascript搜索带过滤器的表

时间:2017-01-20 04:20:04

标签: javascript jquery html

我想使用基于列的选择过滤器创建搜索框 我已经创造了这个 http://www.w3schools.com/code/tryit.asp?filename=FBWZ3PB6XZWP 但我无法通过"所有"柱

3 个答案:

答案 0 :(得分:0)

这应该取代评论// all

for (i = 1; i < tr.length; i++) {
    // text from all the tds
    var combinedText = "";
    // get all the tds (array of them notice there is no [selectz] at the end)
    var td = tr[i].getElementsByTagName("td");

    // loop over all of them and accumulate their text
    for(var j = 0; j < td.length; j++)
        combinedText += td[j].innerHTML.toUpperCase();

    // if filter is in the accumulated text then show the row else hide it
    if (combinedText.indexOf(filter) > -1) {
        tr[i].style.display = "";
    } else {
        tr[i].style.display = "none";
    }       
}

答案 1 :(得分:0)

我对您的代码进行了一些更改,现在正在运行。你可以找到下面的解决方案:)。我建议您稍微更改一下代码,以便更具可读性。

干杯!

<!DOCTYPE html>
<html>
<head>
<style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

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

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
  background-color: #f1f1f1;
}
</style>
</head>
<body>

<h2>My Customers</h2>
<select id="myselect2" name="myselect2">
	<option value="0">Name</option>
	<option value="1">Country</option>
	<option value="2">All</option>
</select>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<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>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Paris specialites</td>
    <td>France</td>
  </tr>
</table>

<script>
function myFunction() {
  var input, filter, table, tr, td, i, selectz;
  input = document.querySelector("#myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = document.querySelectorAll("tr");
  selectz=document.getElementById("myselect2").options[document.getElementById("myselect2").selectedIndex].value;
  
  if(selectz==2){
    
    tr.forEach(function(row){
        var content = row.innerHTML.toUpperCase(),
            display = 'none';
        if(content.indexOf(filter) != -1){
          display = '';
        }
        row.style.display = display;
    });
    
  }else{
  
    for (i = 0; i < tr.length; i++) {
    
      td = tr[i].getElementsByTagName("td")[selectz];
      if (td) {
        if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
          tr[i].style.display = "";
        } else {
          tr[i].style.display = "none";
        }
      }       
  }
  }
  
}
</script>

</body>
</html>

答案 2 :(得分:0)

你可以在&#34; selectz == 2&#34;情况下。

for (i = 1; i < tr.length; i++) {

    if (tr.textContent.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
    } else {
        tr[i].style.display = "none";
    }

}