2016-08-30 16 views
2

私はデータテーブルでライブ検索のソリューションを開発しています。テーブル - jQueryを使用したライブ検索

私はJimを検索する場合

:-)期待どおりに動作しますが、私はCareyを検索したとき、何も結果が表示されません。どうしてこれなの? :-(

デモ:理由は次の行の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>

+3

あり、それはまた、ページネーションとあまりにもソートを処理します。https: //datatables.net/ですが、あなたの質問に答えるために、あなたは最初のtd: '$ row.find(" td:first ")と比較しています。text()' – Pete

+0

@ Peteこれは多くのありがとう。私はそのプラグインを探しています。他のtdを検索するために現在のコードを解決するにはどうすればよいですか? :) – michaelmcgurk

+0

私はこのようにします:http://jsfiddle.net/L1d7naem/14/ – Pete

答えて

3

あなたが望む行動は(も...状態で< 0に注意してください)各ループで、次の修正を達成することができる。

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

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

であるあなたの最後の問題... – michaelmcgurk

+0

:)多くのおかげで、あなたはJSFiddleを持っていますか?これがどこに行くべきか説明できますか? – michaelmcgurk

+0

確か:http://jsfiddle.net/d04yfx8d/。私はtdsのすべての文字列値を追加し、その上にフィルタを適用します。 –

6

その:あなただけのテーブルの最初の列に取り組んでいる

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

と"キャリー"はテーブルの第2欄にある

+0

Aaah。 3つの列をすべて照会する方法を解決するにはどうすればよいですか? – michaelmcgurk

0

これを試してみてください:

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

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

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

は、すべてのtd要素とのマッチングを試してみてください。

+0

私はそれを試しましたが、これは私がAbhishekを入手したものです:http://jsfiddle.net/L1d7naem/11/ – michaelmcgurk

1
 $("#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/

+0

それはほぼ完璧です:-D私は '19'を入力すると何も表示されない理由を説明できますか?私は両方の行がまだ表示されることを期待していた。助けて? :) – michaelmcgurk

+1

次のコードは検索に3番目のtdを考慮しないため、入力すると何も起こりません。 –

+1

http://jsfiddle.net/Lbod9uj1/ –

2

はこれを試してみてください。すべての列を反復処理しなければならず、each()ファンクション内のreturn false;を使用してループをエスケープするだけの一致を見つけると、また、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>

0

希望、ここで

$("#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(); 
     } 
    } 
}); 
}); 

が作業フィドルこれを行うためのプラグインがありますhttps://jsfiddle.net/44Lux7ze/

関連する問題