[英]Hide table when search results 0
I have a table and a search textbox. 我有一张桌子和一个搜索文本框。 I am filtering the table when I type something in the searchbox using this script:
使用以下脚本在搜索框中键入内容时,我正在过滤表格:
$(document).ready(function(){
// Write on keyup event of keyword input element
$("#buscador").keyup(function(){
var $rows1 = $('#tablaproyectos1 tbody>tr');
$('#buscador').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows1.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
});
});
My question is: How can I hide the table when there are no results from the search? 我的问题是:如果搜索没有结果,如何隐藏表格?
Something like this should work: 这样的事情应该起作用:
$(document).ready(function(){ // Write on keyup event of keyword input element $("#buscador").keyup(function(){ var $rows1 = $('#tablaproyectos1 tbody>tr'); $('#buscador').keyup(function() { $('#tablaproyectos1').show(); var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); $rows1.show().filter(function() { var text = $(this).text().replace(/\\s+/g, ' ').toLowerCase(); return !~text.indexOf(val); }).hide().addClass('hide'); if ($('#tablaproyectos1 tbody>tr:visible').length == 0) { $('#tablaproyectos1').hide(); } }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="buscador" /> <table id="tablaproyectos1" style="border: 2px solid blue;"> <tbody> <tr> <td>Esto es una prueba de celda</td> </tr> <tr> <td>Texto de relleno</td> </tr> <tr> <td>Otra celda de prueba</td> </tr> <tr> <td>Suficiente para comprobar el buscador</td> </tr> </tbody> </table>
It works counting the visible rows. 它可以计算可见行。 If total length of this is 0, then hide parent table.
如果总长度为0,则隐藏父表。 Table is always showed at the beginning.
表格始终显示在开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.