2017-04-21 40 views
0

我想使用搜索欄搜索表格。我添加了一個代碼,但它只在表格的第一列進行搜索。我想讓表格的所有列都可搜索。你能指導我怎麼做?這是我的代碼。從查詢$row.find("td").text():「第一」應該返回行的所有列的文本使整個表格可搜索

$("#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(); 
       //$row.html('Records not found!'); 

       //$(".norecord").html('<td colspan="9">Records not found!</td>'); 
      } 
      else { 
       $row.show(); 

      } 
     } 


    }); 
}); 

回答

0

我會使用正則表達式而不是「indexOf」。是這樣的:

if ($(this).text().search(new RegExp(value, "i")) > -1) { 
    $(this).parents("tr").show(); 
} else { 
    $(this).parents("tr").hide(); 
} 

DEMO:https://jsfiddle.net/7qkgf14d/1/

,然後以後,如果你願意,你可以通過你想要搜索什麼樣的列指定。