javascript - DataTables (JQuery) on how to multi column filter text box given a array that contain the data? - Stack Overflow

I am trying to do a multi column filter as shown in this page (.html) using a array that contain all th

I am trying to do a multi column filter as shown in this page (.html) using a array that contain all the data (called 'my_array_data') but I could not get those filter text box displayed.

Below is the code:

<script type="text/javascript">

var asInitVals = new Array();

$(document).ready(function() {


            $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
            var oTable = $('#example').dataTable( {
                "aaData": my_array_data,
                "bProcessing": true,
                "bAutoWidth": false,
                "fnInitComplete": function() {
                                var oSettings = this.fnSettings();
                                for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ ){
                                    if(oSettings.aoPreSearchCols[i].sSearch.length>0){
                                        $("tfoot input")[i].value = oSettings.aoPreSearchCols[i].sSearch;
                                        $("tfoot input")[i].className = "";
                                    }
                                }
                            },
                "aoColumns": [
                    {
                        "sTitle": "From",
                        "sClass": "center",
                        "sWidth": "10%"
                    },
                    {
                        "sTitle": "To",
                        "sClass": "center",
                        "sWidth": "10%"
                    },
                    {
                        "sTitle": "Note",
                        "sClass": "center",
                        "sWidth": "80%"
                    }
                ]
            } );


            $("tfoot input").keyup( function () {
                /* Filter on the column (the index) of this element */
                oTable.fnFilter( this.value, $("tfoot input").index(this) );
            } );


            /*
             * Support functions to provide a little bit of 'user friendlyness' to the textboxes in
             * the footer
             */
            $("tfoot input").each( function (i) {
                asInitVals[i] = this.value;
            } );

            $("tfoot input").focus( function () {
                if ( this.className == "search_init" )
                {
                    this.className = "";
                    this.value = "";
                }
            } );

            $("tfoot input").blur( function (i) {
                if ( this.value == "" )
                {
                    this.className = "search_init";
                    this.value = asInitVals[$("tfoot input").index(this)];
                }
            } );


});

For your info: I do not have a <table> ... </table> as mention before that I store my data in the array called ''my_array_data' and therefore do not have <input class="search_init"/>. Also, "my_array_data" contain three column - basically named as - "From", "To", and "Note".

Any Insight in getting 3 columns of search filter for my array "my_array_data"?

Appreciate any help offer.

I am trying to do a multi column filter as shown in this page (http://www.datatables/examples/api/multi_filter.html) using a array that contain all the data (called 'my_array_data') but I could not get those filter text box displayed.

Below is the code:

<script type="text/javascript">

var asInitVals = new Array();

$(document).ready(function() {


            $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
            var oTable = $('#example').dataTable( {
                "aaData": my_array_data,
                "bProcessing": true,
                "bAutoWidth": false,
                "fnInitComplete": function() {
                                var oSettings = this.fnSettings();
                                for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ ){
                                    if(oSettings.aoPreSearchCols[i].sSearch.length>0){
                                        $("tfoot input")[i].value = oSettings.aoPreSearchCols[i].sSearch;
                                        $("tfoot input")[i].className = "";
                                    }
                                }
                            },
                "aoColumns": [
                    {
                        "sTitle": "From",
                        "sClass": "center",
                        "sWidth": "10%"
                    },
                    {
                        "sTitle": "To",
                        "sClass": "center",
                        "sWidth": "10%"
                    },
                    {
                        "sTitle": "Note",
                        "sClass": "center",
                        "sWidth": "80%"
                    }
                ]
            } );


            $("tfoot input").keyup( function () {
                /* Filter on the column (the index) of this element */
                oTable.fnFilter( this.value, $("tfoot input").index(this) );
            } );


            /*
             * Support functions to provide a little bit of 'user friendlyness' to the textboxes in
             * the footer
             */
            $("tfoot input").each( function (i) {
                asInitVals[i] = this.value;
            } );

            $("tfoot input").focus( function () {
                if ( this.className == "search_init" )
                {
                    this.className = "";
                    this.value = "";
                }
            } );

            $("tfoot input").blur( function (i) {
                if ( this.value == "" )
                {
                    this.className = "search_init";
                    this.value = asInitVals[$("tfoot input").index(this)];
                }
            } );


});

For your info: I do not have a <table> ... </table> as mention before that I store my data in the array called ''my_array_data' and therefore do not have <input class="search_init"/>. Also, "my_array_data" contain three column - basically named as - "From", "To", and "Note".

Any Insight in getting 3 columns of search filter for my array "my_array_data"?

Appreciate any help offer.

Share Improve this question edited Nov 18, 2011 at 1:31 Icarus 64k14 gold badges101 silver badges116 bronze badges asked Nov 18, 2011 at 1:25 Anderson KaruAnderson Karu 6673 gold badges10 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

@Anderson Karu yeah here is the sample for individual column filter above to header

goto herehttp://jsfiddle/s8F9V/1/

You need to define those 3 text boxes on your markup. Here's a shortened example of how I currently achieve the same thing using datatables:

<table id="example">
<thead>
    <tr>
        <th class="theader">
            Date
        </th>
        <th class="theader">
            Delay
        </th>
        <th class="theader">
            Status
        </th>
    </tr>
</thead>
<tbody>
</tbody>
<tfoot>
    <tr>
        <th class="centered">
            <input type="text" id="id1" name="id1" class="search_init" />
        </th>
        <th class="centered">
            <input type="text" id="id2" name="id2" class="search_init" />
        </th>
        <th class="centered">
            <input type="text" id="id3" name="id3" class="search_init" />
        </th>
    </tr>
</tfoot>
</table>

And my initialization code is something like:

var oTable = $("#example").dataTable({
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "iDisplayLength": 10,
    "bSortClasses": false, 
        //...etc etc
});

$("tfoot input").keyup(function(event) {
 /* Filter on the column (the index) of this element */
 if (event.keyCode == 13)//only on enter pressed
 {
    var colIndex = getColumnIndex(this.id);//this is a custom function I wrote. Ignore it
    oTable.fnFilter(this.value, colIndex);
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744335140a4569078.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信