Sap*_*app 27 search jquery live tablerow
我想通过表行进行实时搜索,使用jQuery,"live"这个词是关键,因为我想在文本输入中键入关键字,在同一个站点上我想要jQuery自动排序(或删除那些与搜索查询不匹配的表行.
这是我的HTML:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>214215</td><td>442</td></tr>
<tr><td>1252512</td><td>556</td></tr>
<tr><td>2114</td><td>4666</td></tr>
<tr><td>3245466</td><td>334</td></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
如果我愿意的话.通过搜索Unique ID,它应该显示从唯一ID的特定数字开始的唯一行.铁.如果我在搜索输入框中输入"2",则应保留以下行,因为它们开头2:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>214215</td><td>442</td></tr>
<tr><td>2114</td><td>4666</td></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
如果我要输入24,那么应该只有一行可见,因为它从24:
<table>
<tr><th>Unique ID</th><th>Random ID</th></tr>
<tr><td>24111</td><td>54364</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
如果你们能给我一些关于如何做这样的事情的提示,我会非常感激.
谢谢.
Nop*_*ope 59
我不确定这是多么有效但这有效:
$("#search").on("keyup", function() {
var value = $(this).val();
$("table tr").each(function(index) {
if (index != 0) {
$row = $(this);
var id = $row.find("td:first").text();
if (id.indexOf(value) != 0) {
$(this).hide();
}
else {
$(this).show();
}
}
});
});?
Run Code Online (Sandbox Code Playgroud)
我确实添加了一些简单的突出显示逻辑,您或将来的用户可能会发现这些逻辑很方便.
添加一些基本突出显示的方法之一是em在匹配的文本周围包装标签,并使用CSS将黄色背景应用于匹配的文本,即:( em{ background-color: yellow }),类似于:
// removes highlighting by replacing each em tag within the specified elements with it's content
function removeHighlighting(highlightedElements){
highlightedElements.each(function(){
var element = $(this);
element.replaceWith(element.html());
})
}
// add highlighting by wrapping the matched text into an em tag, replacing the current elements, html value with it
function addHighlighting(element, textToHighlight){
var text = element.text();
var highlightedText = '<em>' + textToHighlight + '</em>';
var newText = text.replace(textToHighlight, highlightedText);
element.html(newText);
}
$("#search").on("keyup", function() {
var value = $(this).val();
// remove all highlighted text passing all em tags
removeHighlighting($("table tr em"));
$("table tr").each(function(index) {
if (index !== 0) {
$row = $(this);
var $tdElement = $row.find("td:first");
var id = $tdElement.text();
var matchedIndex = id.indexOf(value);
if (matchedIndex != 0) {
$row.hide();
}
else {
//highlight matching text, passing element and matched text
addHighlighting($tdElement, value);
$row.show();
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
演示 - 应用一些简单的突出显示
小智 26
这是一个搜索两列的版本.
$("#search").keyup(function () {
var value = this.value.toLowerCase().trim();
$("table tr").each(function (index) {
if (!index) return;
$(this).find("td").each(function () {
var id = $(this).text().toLowerCase().trim();
var not_found = (id.indexOf(value) == -1);
$(this).closest('tr').toggle(!not_found);
return not_found;
});
});
});
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/rFGWZ/369/
yck*_*art 16
FrançoisWahl接近,但有点短:
$("#search").keyup(function() {
var value = this.value;
$("table").find("tr").each(function(index) {
if (!index) return;
var id = $(this).find("td").first().text();
$(this).toggle(id.indexOf(value) !== -1);
});
});
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/ARTsinn/CgFd9/
这是它的纯 Javascript 版本,带有对 ALL COLUMNS 的实时搜索:
function search_table(){
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("search_field_input");
filter = input.value.toUpperCase();
table = document.getElementById("table_id");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td") ;
for(j=0 ; j<td.length ; j++)
{
let tdata = td[j] ;
if (tdata) {
if (tdata.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
break ;
} else {
tr[i].style.display = "none";
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100665 次 |
| 最近记录: |