I`m curretly working on a project using express and EJS and MySQL as DB. I tried put a search bar on a page that show a list of current events, the events are put on a table using EJS to get data from the database. The counter looks like its working, but the search doesnt hide the not expected results on the table. Any sugestions? (The snippet wont run properly the entire tablesince we dont get the rest of server and code)
$(document).ready(function() {
$(".search").keyup(function () {
var searchTerm = $(".search").val();
var listItem = $('.results tbody').children('tr');
var searchSplit = searchTerm.replace(/ /g, "'):containsi('")
$.extend($.expr[':'], {'containsi': function(elem, i, match, array){
return (elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
$(".results tbody tr").not(":containsi('" + searchSplit + "')").each(function(e){
$(this).attr('visible','false');
});
$(".results tbody tr:containsi('" + searchSplit + "')").each(function(e){
$(this).attr('visible','true');
});
var jobCount = $('.results tbody tr[visible="true"]').length;
$('.counter').text(jobCount + ' item(s)');
if(jobCount == '0') {$('.no-result').show();}
else {$('.no-result').hide();}
});
});
.results tr[visible='false'],
.no-result{
display:none;
}
.results tr[visible='true']{
display:table-row;
}
.counter{
padding:8px;
color:#ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>TDGEventos</title>
<link rel="icon" href="" title="icon">
<!--CSS -->
<link rel="stylesheet" href="/public/css/bootstrap.css">
<link rel="stylesheet" href="/public/css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="/public/js/quicksearch.js"></script>
<!--carrega o navbar e o footer-->
<% include layout-nav-footer %>
</head>
<body>
<!--Botao de pesquisa-->
<div class="container">
<div class="row col-lg-12">
<div class="form-group pull-right">
<h2>Eventos</h2>
<input type="text" class="search form-control d-flex" placeholder="Pesquisar eventos">
<span class="container counter pull-right"></span>
</div>
<!--Tabela de resultados-->
<table class="col-md-12 table table-hover table-bordered results">
<thead>
<tr>
<th>Nome</th>
<th>Data de Inicio</th>
<th>Categoria</th>
<th>Premiação</th>
<th class="text-center" colspan="2">Editar</th>
</tr>
<tr class="warning no-result">
<td colspan="5"><i class="fa fa-warning"></i>Sem resultados!</td>
</tr>
</thead>
<tbody>
<% for (var i = 0; i < eventos.length; i++) { %>
<tr>
<td><%= eventos[i].nome_evento %></td>
<td><%= eventos[i].data_formatada %></td>
<td><%= eventos[i].categoria %></td>
<td><%= eventos[i].premiacao %></td>
<td hidden><%= eventos[i].idUser = usuario%></td>
<form action="/entrar-evento" method="POST" >
<input style="display:none"name="id" value="<%=eventos[i].id%>">
<td><button id="cadastrar" class="btn btn-dark btn-change6" type="submit">entrar</button></a></td>
</form>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</body>
</html>