Pesquisa tabela
Search Table
by Rapha Souza
HTML
<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<h2>My Customers</h2>
<div class="row">
<div class="col-md-3">
<!--input type="text" class="campo" id="nome" onkeyup="myFunction(this.id)" placeholder="Search for names.." title="Type in a name" -->
</div>
<div class="col-md-3">
<input type="text" class="campo" id="Country" onkeyup="buscaColunaTabela(this.id, 1)" placeholder="Search for Country.." title="Type in a name">
</div>
</div>
<table id="retornoTabela" class="table">
<thead class="header ">
<th style="width:60%;" class="dropup">
<a id="drop1" href="#" role="button" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Nome<span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
<li role="presentation"><a role="menuitem" tabindex="-1" onclick="sortTable('asc', 0)">Classificar Ordem Crescente</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" onclick="sortTable('desc', 0)">Classificar Ordem Decrescente</a></li>
<li role="presentation">
<div class="col-md-12 form-group">
<label>Filtrar</label>
<input type="text" class="form-control" id="nome" onkeyup="buscaColunaTabela(this.id, 0)" placeholder="Search for names.." title="Type in a name">
</div>
</li>
</ul>
</th>
<th style="width:40%;">Country</th>
</thead>
<tbody>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
...
JavaScript
function buscaColunaTabela(campo, coluna) {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById(campo);
/*
var coluna;
if (campo == "nome") {
coluna = 0;
}
if (campo == "Country") {
coluna = 1;
}
*/
filter = input.value.toUpperCase();
table = document.getElementById("retornoTabela");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[coluna];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
function sortTable(ordem, coluna) {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("retornoTabela");
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[coluna];
y = rows[i + 1].getElementsByTagName("TD")[coluna];
//check if the two rows should switch place:
if (ordem == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
if (ordem == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch)...