0

No momento, só consigo buscar os itens que estão na coluna BAIRRO no índice [0], como faço para conseguir buscar por EMPREENDIMENTO no índice [1], COMPLEMENTO no índice 2, e VALOR no índice 5 ?

Abaixo, segue um print mostrando a tabela e o filtro de pesquisa:

inserir a descrição da imagem aqui

E aqui, segue o código fonte de minha tabela

<table class="dataTable table table-bordered table-striped" id="myTable">

    <thead>
        <tr>
            <th>BAIRRO</th>
            <th>EMPREENDIMENTO</th>
            <th>COMPLEMENTO</th>
            <th>PROPRIETÁRIO</th>
            <th>CONTATO</th>
            <th>VALOR</th>
            <th>CADASTRADO EM</th>
        </tr>
    </thead>

    <tbody>
           <?php
                 foreach ($propertyDetails as $agenciamento) {
                      echo "<tr>";
                      echo "<td>$agenciamento->bairro</td>";
                      echo "<td>$agenciamento->empreendimento</td>";
                      echo "<td>$agenciamento->apartamento</td>";
                      echo "<td>$agenciamento->proprietario</td>";
                      echo "<td>$agenciamento->contato</td>";
                      echo "<td>$agenciamento->valor</td>";
                      echo "<td>$agenciamento->cadastro</td>";
                      echo "</tr>";
                }
            ?>

    </tbody>

E aqui, meu código em JAVASCRIPT

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

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0,1];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

1 Resposta 1

1

Pode fazer um laço for dentro do laço for existente. O primeiro percorre as linhas e o segundo percorre as colunas dentro de cada linha.

Declare uma array com os índices das colunas que você quer percorrer:

var indices = [0,1,2,5];

E o for aninhado dentro do for:

for (i = 0; i < tr.length; i++) {
 for(var x = 0; x < indices.length; x++){
     td = tr[i].getElementsByTagName("td")[indices[x]];
     if (td) {
        txtValue = td.textContent || td.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
          tr[i].style.display = "";
          break;
        } else {
          tr[i].style.display = "none";
        }
     }
  }
}

O segundo for irá percorrer a array indices, cujos valores são os índices das colunas onde se quer filtrar.

Veja que coloquei um break ali no if porque se algo já foi encontrado naquela coluna, já não precisa continuar a verificar as outras colunas.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .