javascript - Search through HTML table columns -


i developing site contains live search. live search used search contacts in contact list (an html table). contact list table 2 columns, each column containing contact. search works but, returns whole row, not matching columns.

meaning if search in table 1 in snippet below; search returns whole row ( || b ), not a. there way refine function search through columns instead of rows?

hope explained myself clearly.

<table>    <tr>      <td>a</td>      <td>b</td>    </tr>      <tr>      <td>c</td>      <td>d</td>    </tr>  </table>

function

<script>                 function myfunction() {               //variables                var input, filter, table, tr, td, i;               input = document.getelementbyid("search");               filter = input.value.touppercase();               table = document.getelementbyid("table");               tr = table.getelementsbytagname("tr");                (i = 0; < 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> 

i've modified code iterate td elements in table. instead of hiding cells don't contain filter text i've opted apply opacity them. makes clearer in example happening.

when doing work on key down, don't forget debounce event. see post introduction: https://davidwalsh.name/javascript-debounce-function

function myfunction() {    //variables     var      input = document.getelementbyid("search"),      filter = input.value.touppercase(),      table = document.queryselector('table'),      cells = table.queryselectorall('td');      (var = 0; < cells.length; i++) {      var cell = cells[i];      if (cell.innerhtml.touppercase().indexof(filter) > -1) {        cell.classlist.remove('no-match');      } else {        cell.classlist.add('no-match');      }    }  }    const    form = document.getelementbyid('form'),    input = document.getelementbyid("search");      form.addeventlistener('submit', onformsubmit);  input.addeventlistener('keyup', onkeyup);    function onformsubmit(event) {    event.preventdefault();    myfunction();  }    function onkeyup(event) {    // debounce event in code or run performance issues.    myfunction();  }
.no-match {   opacity: .2;  }
<form id="form">    <label>      filter text      <input type="text" id="search"/>    </label>    <button>filter</button>  </form>    <table>    <tr>      <td>a</td>      <td>b</td>    </tr>      <tr>      <td>c</td>      <td>d</td>    </tr>  </table>


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -