Search using Javascript only in all table fields

Asked

Viewed 510 times

1

The script which I will post just below copied from the answer to that question: Static search list using Javascript instead of PHP, that was made here in the community.

    window.onload=function(){
    var filtro = document.getElementById('filtro-nome');
    var tabela = document.getElementById('lista');
    filtro.onkeyup = function() {
        var nomeFiltro = filtro.value;
        for (var i = 1; i < tabela.rows.length; i++) {
            var conteudoCelula = tabela.rows[i].cells[0].innerText;
            var corresponde = conteudoCelula.toLowerCase().indexOf(nomeFiltro) >= 0;
            tabela.rows[i].style.display = corresponde ? '' : 'none';
        }
    }
}

Straight to the point! I need you to search all the book names of the table (66 books), meeting the criteria posted only part of the code just below, however is only searching for the first name, I think it is because the script searches the line (Rows), and I do not know how to search in the other fields, If I type Judges or Ruth for example... do not seek! You can see here if you want.

        <table id="lista" class="degrade-tbl">
            <thead>
                <tr>
                    <th><div>Pesquisa</div><div><input id="filtro-nome"/></div>
        </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><a href="">Gênesis</a></td>
                    <td><a href="">Êxodo</a></td>
                    <td><a href="">Levítico</a></td>
                    <td><a href="">Números</a></td>
                </tr>
                <tr>
                    <td><a href="">Deuteronômio</a></td>
                    <td><a href="">Josué</a></td>
                    <td><a href="">Juízes</a></td>
                    <td><a href="">Rute</a></td>
                </tr>
        </tbody>
    </table> 

I found this script in Jquery, but in Github only allows HTML, CSS, Java Script. Could someone please help me?

Thanks in advance - Thank you!

My project is on Github as: bible, in case you want to take a look!

Do not forget that JESUS will return to seek all those who believe! Meditate on the word of Jesus in the gospel of John chapter 14.

Stay with God!

PEACE!

  • Where do the words come from? You have one <input> in HTML or comes from Javascript?

  • Good morning @Sergio. Words come from the index page in HTML itself and <input> is in the fourth line of the second code. It comes from HTML.

1 answer

1


You can do it like this:

const input = document.getElementById('filtro-nome');
const trs = [...document.querySelectorAll('#lista tbody tr')];

input.addEventListener('input', () => {
  const search = input.value.toLowerCase();
  trs.forEach(el => {
    const matches = el.textContent.toLowerCase().includes(search);
    el.style.display = matches ? 'block' : 'none';
  });
});
<table id="lista" class="degrade-tbl">
  <thead>
    <tr>
      <th>
        <div>Pesquisa</div>
        <div><input id="filtro-nome" /></div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href="">Gênesis</a></td>
      <td><a href="">Êxodo</a></td>
      <td><a href="">Levítico</a></td>
      <td><a href="">Números</a></td>
    </tr>
    <tr>
      <td><a href="">Deuteronômio</a></td>
      <td><a href="">Josué</a></td>
      <td><a href="">Juízes</a></td>
      <td><a href="">Rute</a></td>
    </tr>
  </tbody>
</table>

  • You are a blessing my brother. God bless you! Thank you very much from your heart. Regarding the accent @Sergio, for example Genesis has the circumflex accent in the 'e', if I do not put will not appear. It would take a lot of work to ignore and search even if it doesn’t accentuate?

  • @Marcus in Javascript this is kind of hard. Take a look here for inspiration: https://answall.com/q/148180/129

Browser other questions tagged

You are not signed in. Login or sign up in order to post.