0
I’m using the function below to do a "live" search on a table. The problem is that I want it to bring data to all columns, and it’s currently only bringing the first.
function pesquisaTabela() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("search");
filter = input.value.toUpperCase();
table = document.getElementById("dados");
tr = table.getElementsByTagName("tr");
console.log("ssss"+tr.length);
// 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];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
console.log(tr[i]);
} else {
tr[i].style.display = "none";
}
}
}
}
Table
<table class"table-highlight" id="dados">
<thead>
<tr>
<th data-field="id">Item</th>
<th data-field="numero">Número de Rastreamento</th>
<th data-field="data">Enviado em</th>
<th data-field="status">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Pacote 01</td>
<td><a class="waves-effect waves-light btn" href="#modal1">BR021WMR000016437462</a></td>
<td>20/01/2017</td>
<td><span>Em trânsito</span></td>
</tr>
<tr>
<td>Pacote 02</td>
<td><a class="waves-effect waves-light btn" href="#modal1">SP021WMR000016437462</a></td>
<td>15/01/2017</td>
<td><span>Entregue</span></td>
</tr>
<tr>
<td>Pacote 03</td>
<td><a class="waves-effect waves-light btn" href="#modal1">CR021WMR000016437462</a></td>
<td>20/01/2017</td>
<td><span>Cancelado</span></td>
</tr>
</tbody>
</table>
pardon, but in this way the search did not work for any field.
– Lucas Torres
@Lucastorres puts your HTML with example text in the table and search text, so I mount an example and place here.
– Sergio
OK. I added the table text
– Lucas Torres
@Lucastorres improved the answer with your HTML.
– Sergio
worked, but the styles didn’t apply. You know what can be?
– Lucas Torres
@Lucastorres adapts my jsFiddle (https://jsfiddle.net/kmvp7yx5/) with your CSS, otherwise it’s impossible to know what might be missing.
– Sergio
https://gist.github.com/torreslucas13/e60d22256a7300002bbc63fe360c7502 This is the code, after the search, apparently the table loses the formatting. It wouldn’t be because of "block" and "None"
– Lucas Torres
@Lucastorres uses
table-row
instead ofblock
. That seems to be the initial style.– Sergio