vijay kumar
vijay kumar

Reputation: 377

bootstrap datatable custom dropdown filters

I'm using bootstrap datatables in codeigniter project in footer i included this datatables js and initialized like

$('.datatable').dataTable({
            "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>",
            "sPaginationType": "bootstrap",
            "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
            }
        } );

Now i want custom filters in required list page on required column i tried as

<select id="s" name="s">
<option value="1">Hyd</option>
<option value="2">Warangal</option>
</select>

and js as

<script>
$(document).ready(function(){
    $('#s').change( function() {
        //alert($(this).val());
oTable.fnFilter( $(this).val(), 2 );
} );
});
</script>

I want to filter using drop downlist for city.

Upvotes: 4

Views: 18140

Answers (2)

batMask
batMask

Reputation: 744

Hope this is what you looking for, fiddle

$(document).ready(function() {
    var table =  $('#example').DataTable();

    $('#dropdown1').on('change', function () {
        table.columns(1).search( this.value ).draw();
    } );
    $('#dropdown2').on('change', function () {
        table.columns(2).search( this.value ).draw();
    } );
});

Peace!

Upvotes: 10

Yigit Yuksel
Yigit Yuksel

Reputation: 1170

You can do this in this way :

       $('.datatable').dataTable({
       "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>",
       "sPaginationType": "bootstrap",
       "oLanguage": {
           "sLengthMenu": "_MENU_ records per page"
       },
       initComplete: function() {
           this.api().columns().every(function() {
               $('#s').change(function() {

                   var val = $.fn.dataTable.util.escapeRegex(
                       $(this).val()
                   );

                   column.search(val ? '^' + val + '$' : '', true, false).draw();
               });
           });
       }

   });

Source

Upvotes: 0

Related Questions