2

質問用の簡単なテストケースを用意しました。

ディスクに保存してブラウザで開くだけです。すぐに機能し、何もダウンロードしたりインストールしたりする必要はありません。

これが私のテストケースのスクリーンショットです:

スクリーンショット

私の質問は、ユーザーが果物やキャンディーを選択したときに、テーブルの行をどのようにフィルタリングできるかということです。ここで呼び出す関数fnDraw()は何ですか?

私のテストファイルindex.html

<html>
<head>
<style type="text/css" title="currentStyle">
        @import "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/DataTables/DataTables/master/media/js/jquery.dataTables.js"></script>
<script type="text/javascript">

$(function() { 
        var my_table = $('#my_table').dataTable( {
            bJQueryUI: true,
            aoColumns: [
                /* type */ { bVisible: false, bSearchable: false },
                /* thing */ null
            ]
        });

        $(':checkbox').click(function() {
            alert('XXX what to do here? XXX');
        });
});

</script>
</head>
<body>

<p>What should be shown:</p>
<p><label><input type="checkbox" value="fruit">fruit</label></p>
<p><label><input type="checkbox" value="candy">candy</label></p>

<table id="my_table">
<thead>
<tr>
<th>Type</th>
<th>Thing</th>
</tr>
</thead>
<tbody>
<tr><td>fruit</td><td>apple</td></tr>
<tr><td>fruit</td><td>banana</td></tr>
<tr><td>fruit</td><td>pear</td></tr>
<tr><td>candy</td><td>jelly</td></tr>
<tr><td>candy</td><td>toffee</td></tr>
<tr><td>candy</td><td>fudge</td></tr>
</tbody>
</table>
</body>
</html>

ヒントありがとうございます!

更新:DataTablesフォーラムでも質問しました。

4

2 に答える 2

3

これがafnFilteringを使用した私自身の解決策です。それはうまく機能します。

fnFilterを使用したfbasのソリューションは、検索文字列が検索フィールドに表示されるため、好きではありませんでした。(しかし、それでもあなたの提案に感謝します)。

<html>
<head>
<style type="text/css" title="currentStyle">
        @import "http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.2/jquery.dataTables.min.js"></script>
<script type="text/javascript">

$.fn.dataTableExt.afnFiltering.push(
        function(oSettings, aData, iDataIndex) {
                var isFruit = (aData[0] == 'fruit');
                return (isFruit ? $('#fruit').is(':checked') :
                                  $('#candy').is(':checked'));
        }
);

$(function() { 
        var my_table = $('#my_table').dataTable( {
                bJQueryUI: true,
                aoColumns: [
                        /* type */  { bVisible: false, 
                                      bSearchable: false },
                        /* thing */ null
                ],

        });

        $(':checkbox').click(function() {
                my_table.fnDraw();
        });
});

</script>
</head>
<body>

<p>What should be shown:</p>
<p><label><input type="checkbox" id="fruit">fruit</label></p>
<p><label><input type="checkbox" id="candy">candy</label></p>

<table id="my_table">
<thead>
<tr>
<th>Type</th>
<th>Thing</th>
</tr>
</thead>
<tbody>
<tr><td>fruit</td><td>apple</td></tr>
<tr><td>fruit</td><td>banana</td></tr>
<tr><td>fruit</td><td>pear</td></tr>
<tr><td>candy</td><td>jelly</td></tr>
<tr><td>candy</td><td>toffee</td></tr>
<tr><td>candy</td><td>fudge</td></tr>
</tbody>
</table>
</body>
</html>
于 2011-11-12T13:44:37.343 に答える
0

チェックボックスを反復処理するハンドラーを(変更時に)チェックボックスに追加し、「type」列のfnFilterに渡す検索文字列(正規表現を含む)を作成します

つまり、「fruit」がチェックされている場合、fnFilterは「^fruit$」を受け取ります。

つまり、「candy」がチェックされている場合、fnFilterは「^candy$」を受け取ります。

つまり、両方がチェックされている場合、fnFilterは「^ candy $ |^fruit$」を受け取ります。

その列の検索文字列を使用してfnFilterを呼び出しますが、reg式のフィルタリングのために3番目のパラメーターを「true」に設定します

http://www.datatables.net/ref#fnFilter

于 2011-11-09T17:50:11.080 に答える