勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

tableのフィルター機能

   2014/07/02  jQuery

jQueryプラグインで簡単にフィルター機能が

つけられたのでメモ。

■必要なもの

http://sunnywalker.github.io/jQuery.FilterTable/

■使い方

jQueryプラグイン読み込む。

$('table').filterTable({quickList:['class','tag']});を記述。

<tbody>と<thead>をつけとかないと検索に巻き込まれることがあるので注意。

■サンプル

http://sample.taitan916.info/filter_table/

■ソース

<?
function makeRandStr($length) {
$r_str = '';
$str = array_merge(range('a', 'z'), range('0', '9'), range('A', 'Z"'));
for ($x = 0; $x < $length; $x++) {
$r_str .= $str[rand(0, count($str)-1)];
}
return $r_str;
}
?>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="./jquery.filtertable.min.js"></script><!--ライブラリを読み込む-->
<script>
$(function(){
$('table').filterTable({quickList:['class','tag']}); //quickListで好きなものを指定できる
});
</script>
<style>
.filter-table .quick { /*quickListのスタイル*/
margin-left: 0.5em;
font-size: 0.8em;
text-decoration: none;
}
td.alt { /*ソートで出てきたやつの色づけ*/
background-color: #ffc;
background-color: rgba(255, 255, 0, 0.2);
}
</style>
<title>filtertableのテスト</title>
</head>
<body>
<h1>filtertableのテスト</h1>
<table>

<thead>
<tr>
<th>seq</th>
<th>str</th>
<th>quickList</th>
</tr>

</thead>

<tbody>
<? for ($i = 0; $i <= 30; $i++) : ?>
<tr class="visible" style="display: table-row;">
<td><?=$i;?></td>
<td><?=makeRandStr(7);?></td><!--strのとこはランダム英数7文字-->
<td><?=($i % 2 == 0) ? 'class' : 'tag';?></td><!--偶数行はclassをつけて、気数行はtagをつける-->
</tr>
<? endfor ;?>

</tbody>
</table>
</body>
</html>

//20130612追記

導入しようとしてはまったのでメモ。

デフォでは8? か9行ぐらいtrが無いとフィルターの

テキストボックスが表示されない、というのが原因。

$("#result_table").filterTable({minRows : 1});

として指定することで1行以上で表示されるっぽい。

 - jQuery

  関連記事

jQueryでshow/hideよりaddClass/removeClassの方が速い

diaplay:none/blockする際に最近はshow()/hide()を ...

jQueryで関数処理中にブラウザのブロック(intro.js)

Ajaxで処理を行っている間、他の処理を行わせたくなかった。 「now load ...

jQueryでiframe内の要素を呼び出し

jQueryでiframeで開いたbody内の内容が欲しかった。 ■jQuery ...

IE11でフォーム送信時に二重登録される

Internet Explorer11でform送信時にデータが二重登録されると ...

jQueryでimgタグのsrcを変える

やりたかった事はイベントに応じてimgタグの src内のURLを変更。attrを ...