redrain1345 redrain1345 - 8 months ago 46
jQuery Question

Filter multiple columns in jQuery filtered table

I'm having difficulty in my code. before the extra function for filtering out dashes, I had the table searched through all 3 columns. Now I can only search one. I've looked all over and found many examples but can't apply it to my code below. If anyone can help me search all 3 columns instead of only the first one with index[0]. I would like to use this type of method for filtering. I just don't know how to implement in my own code. If entering a phone number or a name or an id it should find it.

<!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>Number search</h2>

<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%;">Number</th>
<th style="width:60%;">Name</th>
<th style="width:60%;">ID</th>
</tr>
<tr>
<td>905-373-3333</td>
<td>Mike</td>
<td>4563</td>
</tr>
<tr>
<td>905-333-3333</td>
<td>adam</td>
<td>8963</td>
</tr>
<tr>
<td>416-373-3432</td>
<td>Jim</td>
<td>9363</td>
</tr>
</table>

<script>
function myFunction() {
var input, filter, table, tr, td, i, cleanedFilter;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");

cleanedFilter = filter.replace("-","");

for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];

if (td) {
cellContent = td.innerHTML.toUpperCase().replace(/-/g,"");

if (cellContent.indexOf(cleanedFilter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>

</body>
</html>

Answer Source

This does not work because you are only testing the index 0. you should also get the index 1 and 2. this might not be the best solution by try it. it works.

<!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>Number search</h2>    

<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%;">Number</th>
    <th style="width:60%;">Name</th>
    <th style="width:60%;">ID</th>    
  </tr>    
  <tr>    
    <td>905-373-3333</td>
    <td>Mike</td>
    <td>4563</td>    
  </tr>    
  <tr>    
     <td>905-333-3333</td>
    <td>adam</td>
    <td>8963</td>    
  </tr>    
  <tr>    
    <td>416-373-3432</td>
    <td>Jim</td>
    <td>9363</td>    
  </tr>    
</table>        

<script>    
function myFunction() {    
  var input, filter, table, tr, td, td1, td2, i, cleanedFilter;    
  input = document.getElementById("myInput");    
  filter = input.value.toUpperCase();    
  table = document.getElementById("myTable");    
  tr = table.getElementsByTagName("tr");          

  cleanedFilter = filter.replace("-","");          

  for (i = 0; i < tr.length; i++) {    
    td = tr[i].getElementsByTagName("td")[0];
    td1 = tr[i].getElementsByTagName("td")[1];
    td2 = tr[i].getElementsByTagName("td")[2];

    if (td) {    
      cellContent = td.innerHTML.toUpperCase().replace(/-/g,"");            
      cellContent1 = td1.innerHTML.toUpperCase().replace(/-/g,"");            
      cellContent2 = td2.innerHTML.toUpperCase().replace(/-/g,"");            

      if (cellContent.indexOf(cleanedFilter) > -1 || cellContent1.indexOf(cleanedFilter) > -1 || cellContent2.indexOf(cleanedFilter) > -1 ) {    
        tr[i].style.display = "";    
      } else {    
        tr[i].style.display = "none";    
      }    
    }          
  }    
}    
</script>        

</body>    
</html>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download