How to filter with the search text box after press enter key?

How to filter with the search text box after press enter key?

pawelpawel Posts: 14Questions: 0Answers: 0
edited October 2010 in General
Hi Allan,

First of all, congratulations for your plugin, has been really useful for me !.

I'm working with a web service for the server side processing and it gives me every time the result of manage differents databases so...i wonder if there is any chance to change the input text box filter to search only after press the enter key. I've been trying to change the original code, (c.keyup and c.keypress), for this issue but it never works ! Has anyone got it? Could you give me some clue how to get it?

Regards,
Pawel.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    What to do is to just unbind the keypress event handler that DataTables puts on the input box, and then add your own which will call fnFilter ( http://datatables.net/api#fnFilter ) when the return key (keyCode 13) is detected.

    Allan
  • pawelpawel Posts: 14Questions: 0Answers: 0
    edited October 2010
    Hi Allan,

    Thanks for replying. I guess that u mean something like this ?

    $("thead input").keyup( function () {
    if (this.value == 13)
    oTable.fnFilter( Vals.value ) );
    else
    Vals += this.value;
    } );

    I haven't tried this code yet but my question is....how to set this functionallity to the default search text box?

    Regards,
    Pawel.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    More like this:

    [code]
    $("div.dataTables_filter input").keyup( function (e) {
    if (e.keyCode == 13) {
    oTable.fnFilter( this.value ) );
    }
    } );
    [/code]
    What you want to do is unbind the event handler DataTables adds ( http://api.jquery.com/unbind/ ) and then attach your own with the code above. That will then act as a filter with the return key.

    Allan
  • pawelpawel Posts: 14Questions: 0Answers: 0
    Lovely ! I'm gonna try it and I'll post the code to help anyone that wants to do the same.

    Thanks for your time Allan and congratulations again for your work,

    Cheers,
    Pawel.
  • pawelpawel Posts: 14Questions: 0Answers: 0
    edited October 2010
    Hi Allan,

    It was really simple to change, here is the code

    [code]
    $(document).ready(function() {
    var oTable = $('#test').dataTable( {
    "bPaginate": true,
    "bLengthChange": true,
    "bFilter": true,
    "bSort": true,
    "bInfo": true,
    "bAutoWidth": true } );
    $('#test_filter input').unbind();
    $('#test_filter input').bind('keyup', function(e) {
    if(e.keyCode == 13) {
    oTable.fnFilter(this.value);
    }
    });
    } );
    [/code]

    Thanks for your time, hope it helps !

    Regards,
    Pawel.
  • mathieufanneemathieufannee Posts: 17Questions: 0Answers: 0
    This worked for me too. Thanks a lot!
  • crunchfactorycrunchfactory Posts: 29Questions: 8Answers: 2
    edited May 2014

    Hi Allan,
    A little stumped, how is this done with the 1.10 build?

    Trying to figure out an alternative to oTable.fnFilter(this.value);

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    What's wrong with using fnFilter? The old API is still present and can still be used. Or you can use the new API's search() method. (remember also how to access the new API).

    Allan

This discussion has been closed.