skip to Main Content

I am trying search filter for html table. i want to show no records found when there no search field matches. It is not working as expected. no records tr should appear only when no match found.
please guide me. Thanks in advance.

even if record founds then also no records found is adding at the bottom

Here is the code

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<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>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<table id="myTable">
    <thead>
  <tr class="header">
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  </thead>
  <tbody id="listitems">
  <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>
  <tr id="NoRecords" colspan="2" style="display:none"><td>No records found</td></tr>
  </tbody>
</table>

<script>
function myFunction() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("listitems");
  tableNorecords = document.getElementById("NoRecords");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i];
    td2 = tr[i].getElementsByTagName("td")[1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
        tableNorecords.style.display = "none";
      } else {
        tr[i].style.display = "none";
        tableNorecords.style.display = "";
      }
    }       
  }
}
</script>

</body>
</html>

2

Answers


  1. function myFunction() {
          var input, filter, table, tr, td, i, txtValue;
          input = document.getElementById("myInput");
          filter = input.value.toUpperCase();
          table = document.getElementById("listitems");
          tableNorecords = document.getElementById("NoRecords");
          tr = table.getElementsByTagName("tr");
          var count=0;
          for (i = 0; i < tr.length; i++) {
            td = tr[i];
            td2 = tr[i].getElementsByTagName("td")[1];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
                count++;
              } else {
                tr[i].style.display = "none";
              }
            }       
          }
          if(count==0)
          {
            tableNorecords.style.display = "";
          }
          else
          {
            tableNorecords.style.display = "none";
          }
          
        }
     * {
          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;
        }
    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
    
        <table id="myTable">
            <thead>
          <tr class="header">
            <th style="width:60%;">Name</th>
            <th style="width:40%;">Country</th>
          </tr>
          </thead>
          <tbody id="listitems">
          <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>
          <tr id="NoRecords" colspan="2" style="display:none"><td>No records found</td></tr>
          </tbody>
        </table>
    Login or Signup to reply.
  2. Problem is that you need to check how many rows are "visible" after your loop.

    Add this after your loop:

    const visibleRowCount = Array.from(table.querySelectorAll('tr:not(#NoRecords)')).reduce((count, row) => {
      return count + (row.offsetHeight > 0 ? 1 : 0);
      }, 0);
    tableNorecords.style.display = visibleRowCount > 0 ? "none" : "";
    

    Demo

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <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>
    
      <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
    
      <table id="myTable">
        <thead>
          <tr class="header">
            <th style="width:60%;">Name</th>
            <th style="width:40%;">Country</th>
          </tr>
        </thead>
        <tbody id="listitems">
          <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>
          <tr id="NoRecords" colspan="2" style="display:none">
            <td>No records found</td>
          </tr>
        </tbody>
      </table>
    
      <script>
        function myFunction() {
          var input, filter, table, tr, td, i, txtValue;
          input = document.getElementById("myInput");
          filter = input.value.toUpperCase();
          table = document.getElementById("listitems");
          tableNorecords = document.getElementById("NoRecords");
          tr = table.getElementsByTagName("tr");
          for (i = 0; i < tr.length; i++) {
            td = tr[i];
            td2 = tr[i].getElementsByTagName("td")[1];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
              } else {
                tr[i].style.display = "none";
              }
            }
          }
          const visibleRowCount = Array.from(table.querySelectorAll('tr:not(#NoRecords)')).reduce((count, row) => {
            return count + (row.offsetHeight > 0 ? 1 : 0);
          }, 0);
          tableNorecords.style.display = visibleRowCount > 0 ? "none" : "";
        }
      </script>
    
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search