勉強したことのメモ

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

  関連記事

八地方区分→都道府県→路線→駅名の連携したセレクトメニュー の実装方法

八地方区分(関東、近畿等)をプルダウンから選ぶと区分内の都道府県プルダウンが表示 ...

Ajaxで画像のアップロード(jquery.upload)

業務中に画像の選択後、アップロードボタンを押して画像のアップ、 その後フォームの ...

jQueryのプラグインで簡単にイメージスライダーを作成(bxslider)

画像のスライダーを作る時に便利なプラグインであるbxslider。簡単に使えるし ...

画像アップロード前の時点で画像が選択されているか確認

やりたかった事は、フォーム内で画像をアップロードする際、 ちゃんと画像がローカル ...

jQueryで後から追加された要素にイベントを設定

jQueryで.html()等を使って追加した要素に対してイベントを設定したかっ ...