tableのフィルター機能
2014/07/02
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プラグインの作成
面白そうだったのでメモ。 ■参考サイト http://www.entacl.in ...
-
-
jQueryとCSSで指定したテキストに対してラインマーカーを引く方法
サイト内で強調したいテキストに対して蛍光ペンでマーカーを引くようなアニメーション ...
-
-
jQueryのanimate()でページスクロール後に他の処理を実行させる方法
jQueryのanimate()でページスクロール後に特定の要素を非表示にしたい ...
-
-
「display: block !important;」指定された要素をjQueryで非表示にする方法
jQueryで特定の要素を非表示にしたかったのでhide()を使用したところ非表 ...
-
-
エリア→都道府県→路線→駅名を連携する絞り込みセレクトメニューの設置方法
エリア→都道府県→路線→駅名のドリルダウン検索をプルダウン形式で設置したい。以前 ...