Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown filter jquery datatables

Tags:

Here is my code:

$(document).ready(function() {
    /* Initialise the DataTable */
    var oTable = $('#example').dataTable({
        "oLanguage": {
            "sSearch": "Search all columns:"
        },
        "iDisplayLength": 10,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bFilter": true,
    }); 

    /* Add a select menu for each TH element in the table footer */
    $("thead th").each( function ( i ) {
        this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
        $('select', this).change( function () {
            oTable.fnFilter( $(this).val(), i );
        } );
    } );        
} );

Im using the jquery datatables plugin, its working perfectly just like this example:

http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html

What i would like to do is rather than having a dropdown for each column i would like a dropdown only on one specific column.

So i presume i need to change:

$("thead th").each( function ( i ) {

But im not sure what to put. Any help would be much appreciated, thanks in advance.

like image 304
Codded Avatar asked Mar 08 '12 11:03

Codded


2 Answers

You can also create your own select list and position it anywhere you like outside the table.

<select id="mySelect">
    <option value="">Select</option>
    <option value="1">1</option>
    ...
</select>

<script>
    $('#mySelect').on('change',function(){
        var selectedValue = $(this).val();
        oTable.fnFilter("^"+selectedValue+"$", 0, true); //Exact value, column, reg
    });
</script>
like image 94
crashtestxxx Avatar answered Sep 24 '22 06:09

crashtestxxx


If you need only on one column you could do

var indexOfMyCol = 2;//you want it on the third column
$("thead th").each( function ( i ) {
    if(i === indexOfMyCol){ 
      this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
      $('select', this).change( function () {
        oTable.fnFilter( $(this).val(), i );
      } );
    }
} );   
like image 35
Nicola Peluchetti Avatar answered Sep 22 '22 06:09

Nicola Peluchetti