Good evening. I'm having some trouble making another function work along with jQuery. The jQuery function is working as I would, but the one I created does not.
In fact, it is almost completely functional, this function selects all the names of the table and makes a filter bringing only the selected name in the combo box and giving hide in the others. It works, however, only on the page of the table that I am, if I select any name that is on the second page (Ex: Diego), it does not come to the first page as it should give hide in the others. But the names that are on the second page of the table combo box read.
Can anyone help me and have some hint of what I'm missing in the code?
Qs: The combo box has no css and placement, but is the one below the "Name" column.
//Iniciar função jQuery de filtros padrões
$(document).ready(function() {
$('#tab').dataTable({ordering:false});
});
//End
//Inicia função do combobox
function AdicionarFiltro(tabela, coluna) {
var cols = $("#" + tabela + " thead tr:first-child th").length;
if ($("#" + tabela + " thead tr").length == 1) {
var linhaFiltro = "<tr>";
for (var i = 0; i < cols; i++) {
linhaFiltro += "<th></th>";
}
linhaFiltro += "</tr>";
$("#" + tabela + " thead").append(linhaFiltro);
}
var colFiltrar = $("#" + tabela + " thead tr:nth-child(2) th:nth-child(" + coluna + ")");
$(colFiltrar).html("<select id='filtroColuna_" + coluna.toString() + "' class='filtroColuna'> </select>");
var valores = new Array();
$("#" + tabela + " tbody tr").each(function() {
var txt = $(this).children("td:nth-child(" + coluna + ")").text();
if (valores.indexOf(txt) < 0) {
valores.push(txt);
}
});
$("#filtroColuna_" + coluna.toString()).append("<option>TODOS</option>")
for (elemento in valores) {
$("#filtroColuna_" + coluna.toString()).append("<option>" + valores[elemento] + "</option>");
}
$("#filtroColuna_" + coluna.toString()).change(function() {
var filtro = $(this).val();
$("#" + tabela + " tbody tr").show();
if (filtro != "TODOS") {
$("#" + tabela + " tbody tr").each(function() {
var txt = $(this).children("td:nth-child(" + coluna + ")").text();
if (txt != filtro) {
$(this).hide();
}
});
}
});
};
AdicionarFiltro("tab", 2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script><linkhref="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<center>
<h1>Log das Integrações</h1>
</center>
<div class="table-responsive">
<table id="tab" class="display table" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Valor</th>
<th>Erro</th>
</tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>Davi</td>
<td>300</td>
<td>Não</td>
</tr>
<tr>
<td>002</td>
<td>Charles</td>
<td>200</td>
<td>Não</td>
</tr>
<tr>
<td>003</td>
<td>Sravani</td>
<td>400</td>
<td>Sim</td>
</tr>
<tr>
<td>004</td>
<td>Davi</td>
<td>300</td>
<td>Não</td>
</tr>
<tr>
<td>005</td>
<td>Ingrid</td>
<td>800</td>
<td>Não</td>
</tr>
<tr>
<td>006</td>
<td>Letícia</td>
<td>400</td>
<td>Sim</td>
</tr>
<tr>
<td>007</td>
<td>Ronaldo</td>
<td>800</td>
<td>Sim</td>
</tr>
<tr>
<td>008</td>
<td>Mike</td>
<td>300</td>
<td>Não</td>
</tr>
<tr>
<td>009</td>
<td>Andrew</td>
<td>300</td>
<td>Sim</td>
</tr>
<tr>
<td>010</td>
<td>Stephen</td>
<td>300</td>
<td>Não</td>
</tr>
<tr>
<td>011</td>
<td>Julio</td>
<td>400</td>
<td>Não</td>
</tr>
<tr>
<td>012</td>
<td>Marcos</td>
<td>300</td>
<td>Não</td>
</tr>
<tr>
<td>013</td>
<td>Diego</td>
<td>300</td>
<td>Não</td>
</tr>
</tbody>
</table>
</div>