Set focus to the Search Box

Set focus to the Search Box

fortunate_manfortunate_man Posts: 3Questions: 1Answers: 0
edited January 2017 in Free community support

Hello, I searched the entire forum and although I found information, I can not understand where the code is placed to focus the search box. (Sorry for my English) Here my code:

$(document).ready(function() {
$('#example').DataTable( {
initComplete: function () {
this.api().columns([1]).every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.header()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
} );
} );

Thank you.

Answers

  • fortunate_manfortunate_man Posts: 3Questions: 1Answers: 0

    Could it be that this is not working because my table is extremely large? I tried everything.

    This not work in my case: $('#example_filter input').focus();

  • fortunate_manfortunate_man Posts: 3Questions: 1Answers: 0
    edited January 2017

    I already found the solution:

    $(document).ready(function() {
    setTimeout(function () { $('div.dataTables_filter input').focus(); }, 5000);
    $('#example').DataTable( {
    initComplete: function () {
    this.api().columns([1]).every( function () {
    var column = this;
    var select = $('<select><option value=""></option></select>')
    .appendTo( $(column.header()).empty() )
    .on( 'change', function () {
    var val = $.fn.dataTable.util.escapeRegex(
    $(this).val()
    );
    column
    .search( val ? '^'+val+'$' : '', true, false )
    .draw();
    } );
    column.data().unique().sort().each( function ( d, j ) {
    select.append( '<option value="'+d+'">'+d+'</option>' )
    } );
    } );
    }
    } );
    } );

This discussion has been closed.