Focus on input/filter

Focus on input/filter

leprimoleprimo Posts: 25Questions: 4Answers: 0
edited October 2010 in General
I need to put the focus on the filter input field after datatable is drawn. I'll try several notations, like [code]$("#mytablename_filter:input").focus();[/code]but nothing works. Can someone give me a hint, please?

Replies

  • leprimoleprimo Posts: 25Questions: 4Answers: 0
    *push*
    no ideas?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Close :-)

    [code]
    $("#mytableid_filter input").focus();
    [/code]
    Do that after DataTables has been initialised and it should work okay.

    Allan
  • leprimoleprimo Posts: 25Questions: 4Answers: 0
    edited October 2010
    ...yeah, sorry, I wrote '#mytablename' but I know, it must be the ID.
    That was not the point:
    [code]$("#mytableid_filter input").focus();[/code]
    works well, as long as not another language is in use.
    [code]
    $(document).ready(function() {
    $('#mytableid').dataTable( {
    "oLanguage": {"sUrl": "de_DE.txt"},
    "sAjaxSource": "myajax.php"
    } );
    $("#mytableid_filter input").focus();
    });
    [/code]
    does not work (in my case).
    de_DE.txt:
    [code]
    ...
    "sSearch": "Filter",
    ...
    [/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    If you want to do it with another language loaded by XHR, you'd need to do it with fnInitComplete ( http://datatables.net/usage/callbacks#fnInitComplete ). The reason for this is that the DataTables initialisation call will complete before the Ajax data has loaded (it's asynchronous...) so DataTables won't have drawn the lanauge or the filter at that time. fnInitComplete is provided for exactly this reason.

    Allan
  • leprimoleprimo Posts: 25Questions: 4Answers: 0
    Thank you,
    [code]...
    "fnInitComplete": function() {
    $("#mytableid_filter input").focus();
    }
    ...[/code]
    works fine with with German language.
This discussion has been closed.