当用户在输入中输入时,我正在搜索表并相应地显示结果。
但我面临的问题是搜索后隐藏了表头。
这是我的代码
$(document).ready(function()
{
$('#searchinputtext').keyup(function()
{
var tr = $('#videosfromtagstable tbody tr'); //use tr not td
if ($(this).val().length >= 2)
{
var inputdata = $.trim($("#searchinputtext").val());
$('#errmsgnovideos').hide();
var noElemvideo = true;
var val = $.trim(this.value).toLowerCase();
el = tr.filter(function()
{
return $(this).find('td:eq(0)').text().toLowerCase().indexOf(val) >= 0;
}); // <==== closest("tr") removed
if (el.length >= 1)
{
noElemvideo = false;
}
//now you fadeIn/Out every row not every cell
tr.not(el).fadeOut();
el.fadeIn();
if (noElemvideo)
if (inputdata !== '')
{
$('#errmsgnovideos').html('No Results Matched').show();
}
else
{
$('#errmsgnovideos').hide();
}
}
else
{
tr.fadeIn(); //show all if length does not match the required number of characters
$('#errmsgnovideos').hide();
}
})
});
http://jsfiddle.net/cod7ceho/307/
您能否告诉我如何解决此问题。
答案 0 :(得分:2)
使用<thead>
<input type="text" id="searchinputtext" class="form-control" placeholder="Search">
<span id="errmsgnovideos"></span>
<table class="mytable1 table table-bordered table-hover" id="videosfromtagstable">
<thead>
<tr class="existingvideos">
<th width="20%">Name</th>
<th width="35%">File</th>
<th width="30%">Course</th>
<th width="15%">SEEPZ</th>
</tr>
</thead>
<tbody class="connectedSortable ui-sortable">
<tr video-id="48" title="" class="newvideos exercises-add-table-content">
<td>Dsada</td>
<td><a href="xxx" target="_blank">dftyr.mp4</a></td>
<td>
<span class="btn btn-sm btn-success btn-circle">Five</span>
</td>
<td><i class="fa fa-check"></i></td>
</tr>
<tr video-id="49" title="" class="newvideos exercises-add-table-content">
<td>Fds</td>
<td><a href="xxx" target="_blank">dftyr.mp4</a></td>
<td>
<span class="btn btn-sm btn-success btn-circle">Five</span>
</td>
<td><i class="fa fa-check"></i></td>
</tr>
</tbody>
</table>
演示:http://jsfiddle.net/cod7ceho/308/
或者如果您的类型“我无法修改我的HTML”
$('#videosfromtagstable tbody tr:not(:first)');
答案 1 :(得分:0)
<input type="text" id="searchinputtext" class="form-control" placeholder="Search">
<table class="mytable1 table table-bordered table-hover" id="videosfromtagstable">
<tbody class="connectedSortable ui-sortable">
<tr class="existingvideos">
<th width="20%">Name</th>
<th width="35%">File</th>
<th width="30%">Course</th>
<th width="15%">SEEPZ</th>
</tr>
<tr video-id="48" title="" class="newvideos exercises-add-table-content">
<td>Dsada</td>
<td><a href="xxx" target="_blank">dftyr.mp4</a></td>
<td>
<span class="btn btn-sm btn-success btn-circle">Five</span>
</td>
<td><i class="fa fa-check"></i></td>
</tr>
<tr video-id="49" title="" class="newvideos exercises-add-table-content">
<td>Fds</td>
<td><a href="xxx" target="_blank">dftyr.mp4</a></td>
<td>
<span class="btn btn-sm btn-success btn-circle">Five</span>
</td>
<td><i class="fa fa-check"></i></td>
</tr>
</tbody>
<tfoot>
<tr><td colspan="3" id="errmsgnovideos"></td></tr>
</tfoot>
</table>
$(document).ready(function()
{
$('#searchinputtext').keyup(function()
{
var tr = $('#videosfromtagstable tbody tr').not(".existingvideos"); //use tr not td
if ($(this).val().length >= 2)
{
var inputdata = $.trim($("#searchinputtext").val());
$('#errmsgnovideos').hide();
var noElemvideo = true;
var val = $.trim(this.value).toLowerCase();
el = tr.filter(function()
{
return $(this).find('td:eq(0)').text().toLowerCase().indexOf(val) >= 0;
}); // <==== closest("tr") removed
if (el.length >= 1)
{
noElemvideo = false;
}
//now you fadeIn/Out every row not every cell
tr.not(el).fadeOut();
el.fadeIn();
if (noElemvideo)
if (inputdata !== '')
{
$('#errmsgnovideos').html('No Results Matched').show();
}
else
{
$('#errmsgnovideos').hide();
}
}
else
{
tr.fadeIn(); //show all if length does not match the required number of characters
$('#errmsgnovideos').hide();
}
})
});
你试试这可以帮到你