21

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.

1
  • 2
    I think using 'i' you can control which column you want to show into Commented Mar 8, 2012 at 11:22

7 Answers 7

26

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>
Sign up to request clarification or add additional context in comments.

6 Comments

This in my opinion is the best answer and usage
this supposes that you already know the values of that column , sometimes it's not the case
But if bStateSave set to true, you'll not get the selected value after refresh
var oTable = $('#example').DataTable(); $('#main_cat_select').on('change',function(){ var selectedValue = $(this).val(); oTable.column( 3 ) .search( selectedValue ) .draw(); });
3 is column number. @user9437856 you can use like that.
|
12

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 );
      } );
    }
} );   

8 Comments

Thanks, so easy when you think about it :) Can i Give the dropdown a title? like.... Please select
@Codded in fnCreateSelect replace var r='<select><option value=""></option>', with var r='<select><option value="">Please Select</option>',
Im not switched on today, could have worked that one out :) Thanks for the help
How about sorting the select into alphabetical order?
@Codded on line 52 before return asResultData; you should aplly a sort to asResultData
|
3

Maybe times have changed, but with no plugin and using dataTables 1.10.12and it's @api, as a person in the comments suggested, you can use the zero based index integer from an array for the corresponding table. Example code, important bits are on line 2 below. I'm searching just on the 4th column, and this is coffeescript but you get the idea.

    $('#example').DataTable initComplete: ->
                    @api().columns([3]).every ->
                            column = this
                            select = $('<select><option value="">Sort Column(All)</option></select>').appendTo($(column.header()).empty()).on('change', ->
                                    val = $.fn.dataTable.util.escapeRegex($(this).val())
                                    column.search(val ? '^'+val+'$' : '', true, false).draw()
                                    return
                            )
                            column.data().unique().sort().each (d, j) ->
                                    select.append '<option value="' + d + '">' + d + '</option>'
                                    return
                            return
                    return

Comments

2

You can use date tables column filter see http://jquery-datatables-column-filter.googlecode.com/svn/trunk/customFilters.html

It is a second level plugin for datatables.

Jovan

Comments

2

You can use the columnfilter plugin...

$(document).ready(function(){
     $('#example').dataTable()
          .columnFilter({
            aoColumns: [ { type: "select", values: [ 'Gecko', 'Trident', 'KHTML', 'Misc', 'Presto', 'Webkit', 'Tasman']  },
                     { type: "text" },
                     null,
                     { type: "number" },
             { type: "select" }
                ]

        });
});

Comments

2

I think something like following might do the trick

$("thead th").each( function ( i ) {
    if(i==1)
    {
        this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
        $('select', this).change( function () {
            oTable.fnFilter( $(this).val(), i );
        } ); 
    }
} );  

1 Comment

Thank you, i have used the other answer but this would work aswel :)
-1
<select id="dropdown1">
<option value="">Select</option>
<option value="London">London</option>
<option value="San Francisco">San Francisco</option>
</select>

$(document).ready(function() {
var table = $('#example').DataTable();
$('#dropdown1').on('change', function() {
table.columns(1).search(this.value).denter code hereraw();
});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.