简体   繁体   English

表 - 使用jQuery进行实时搜索

[英]Table - Live Search with jQuery

I have been working on a solution for a Live Search on my table of data. 我一直在为我的数据表上的实时搜索解决方案。

When I search Jim it works as expected :-) 当我搜索Jim它按预期工作:-)

However, when I search Carey , no results appear. 但是,当我搜索Carey ,没有结果出现。 Why is this? 为什么是这样? :-( :-(

Demo: http://jsfiddle.net/L1d7naem/ 演示: http //jsfiddle.net/L1d7naem/

 $("#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) { $row.hide(); } else { $row.show(); } } }); }); 
 table, tr, td, th{ border: 1px solid blue; padding: 2px; } table th{ background-color: #999999; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><th>Forename</th><th>Surname</th><th>Extension</th></tr> <tr><td>Jim</td><td>Carey</td><td>1945</td></tr> <tr><td>Michael</td><td>Johnson</td><td>1946</td></tr> </table> <br /> <input type="text" id="search" placeholder=" live search"></input> 

Its because of the following line: 因为以下几行:

var id = $row.find("td:first").text();  

You are working on the first column of table only and "Carey" is in second column of table 您正在处理表的第一列,而“Carey”位于表的第二列

The behaviour you want can be achieved with the following corrections in the each loop (also note the < 0 in the condition...): 您可以通过每个循环中的以下更正来实现您想要的行为(还要注意条件中的< 0 ):

var id = $.map($row.find('td'), function(element) {
    return $(element).text()
}).join(' ');

if (id.indexOf(value) < 0) {
    $row.hide();
} else {
    $row.show();
}

Try this. 尝试这个。 You have to iterate through all columns and once you find any match just escape the loop using the return false; 你必须遍历所有列,一旦找到任何匹配,只需使用return false;转义循环return false; within each() function. each()函数中。 Also, indexOf returns -1 if string is not found. 此外,如果找不到string, indexOf返回-1。

 $("#search").on("keyup", function() { var value = $(this).val(); $("table tr").each(function(index) { if (index !== 0) { $row = $(this); $row.find("td").each(function(){ var id = $(this).text(); if (id.indexOf(value) < 0) { $row.hide(); } else { $row.show(); return false; } }); } }); }); 
 table, tr, td, th{ border: 1px solid blue; padding: 2px; } table th{ background-color: #999999; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><th>Forename</th><th>Surname</th><th>Extension</th></tr> <tr><td>Jim</td><td>Carey</td><td>1945</td></tr> <tr><td>Michael</td><td>Johnson</td><td>1946</td></tr> </table> <br /> <input type="text" id="search" placeholder=" live search"></input> 

      $("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $.map($row.find('td'), function(element) {
    return $(element).text()
}).join(' ');





            if (id.indexOf(value) <0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});

http://jsfiddle.net/Lyxex4tp/ http://jsfiddle.net/Lyxex4tp/

Try this: 尝试这个:

$("#search").on("keyup", function() {
var value = $(this).val();

$("table td").each(function() {
    if(value.match($(this).text)) {
        console.log($(this).text());
    }

    else {
        $("table").hide();
    }
});
});

Try matching with all the td elements. 尝试与所有td元素匹配。

Hope this works, 希望这有效,

$("#search").on("keyup", function() {
var value = $(this).val();

$("table tr").each(function(index) {
    if (index !== 0) {
        var id = $(this).children().text()
        if (id.indexOf(value) < 0) {
           $(this).hide();
        }else {
            $(this).show();
        }
    }
 });
});

Here is the working fiddle https://jsfiddle.net/44Lux7ze/ 这是工作小提琴https://jsfiddle.net/44Lux7ze/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM