27

I am using Datatables plugin of jQuery. I am using server side processing functionality for my ASP.Net project.

Its get frustrating when each time I try to type something in global search, with each letter I type it calls the server side method and brings result for me.

It gets more frustrating when the data to be filter is large.

Is there any option or way to call search method on keypress of enter key and not on any key press?

8 Answers 8

27

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 when the return key (keyCode 13) is detected.

$("div.dataTables_filter input").keyup( function (e) {
    if (e.keyCode == 13) {
        oTable.fnFilter( this.value );
    }
} );

Else

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

2 Comments

In the first option unless I'm mistaken, there is an erroneous ) after this.value.
u saved so much of my time. I used else part. Just changed id. :-) Worked like magic for me. :)
27

I tried Techie's code, too. Of course, I also got the error message fnFilter is not a function. Actually, replacing the line oTable.fnFilter(this.value); through oTable.search( this.value ).draw(); would do the job, but in my case, the unbind/bind functions were executed before my server-side searched table was initialised. Therefore, I put the unbind/bind functions into the initComplete callback function, and everything works fine:

$(document).ready(function() {
    var oTable = $('#test').dataTable( {
        "...": "...",
        "initComplete": function(settings, json) {
            $('#test_filter input').unbind();
            $('#test_filter input').bind('keyup', function(e) {
                if(e.keyCode == 13) {
                    oTable.search( this.value ).draw();
                }
            }); 
        }
    });    
});

6 Comments

how are you able to pass oTable as oTable.search(...) when initComplete is actually being run as the final part of the oTable initialization?
Not sure if get what you mean, @Tall'jeezzy ? Once the DataTable's initialization is finished, I add the key listener for the enter key in the search field, and I can use the oTable object since its initialization is finished...
what's special about initComplete that made it work?
I tried to explain it in my description. What exactly is not clear to you?
Its works so fine ... for early version to DataTables.net.
|
8

I end up doing this in Datatables(v1.10.15). I also prevent the backspace and the delete button from sending search request if input was empty.

$.extend( $.fn.dataTable.defaults, {
    "initComplete": function(settings, json) {
        var api = this.api();
        var textBox = $('#datatable_filter label input');
        textBox.unbind();
        textBox.bind('keyup input', function(e) {
            if(e.keyCode == 8 && !textBox.val() || e.keyCode == 46 && !textBox.val()) {
                // do nothing ¯\_(ツ)_/¯
            } else if(e.keyCode == 13 || !textBox.val()) {
                api.search(this.value).draw();
            }
        }); 
    }
});

2 Comments

from where table object came?
@PrashantPokhriyal It has been a long time. I don't remember but I think it should be a DataTable instance. var table = $('#datatable').DataTable({ /* ... */ });
6

Here is how I managed to do it:

$(document).on('focus', '.dataTables_filter input', function() {

    $(this).unbind().bind('keyup', function(e) {
        if(e.keyCode === 13) {
            oTable.search( this.value ).draw();
        }
    });

});

2 Comments

oTable just in case is = this.api() in the function InitComplete.
This is the easiest and neatest way to do it. IMO.
5

Here is how to handle it with the api change in version 1.10

    //prevents form submissions if press ENTER in textbox
    $(window).keydown(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });

    var searchbox = $('#ordergrid_filter input');
    searchbox.unbind();
    searchbox.bind('keyup', function (e) {
        if (e.keyCode == 13) {
            ogrid.search(this.value).draw();
        }
    });

    var uitool = '';
    searchbox.on("mousedown", function () {
        uitool = 'mouse';
    });
    searchbox.on("keydown", function () {
        uitool = 'keyboard';
    });

    //Reset the search if the "x" is pressed in the filter box
    searchbox.bind('input', function () {
        if ((this.value == "") && (ogrid.search() != '') && (uitool == 'mouse')) {
            ogrid.search('').draw();
            return;
        }
    });

1 Comment

Works for me, but took me a while to realise I use "table" instead of "ogrid"
1

Finally got it working using this way

var oTable = $('#table-name').dataTable({
    processing: true,
    serverSide: true,
    ajax: "file.json",
    initComplete: function() {
        $('#table-name_filter input').unbind();
        $('#table-name_filter input').bind('keyup', function(e) {
            if(e.keyCode == 13) {
                oTable.fnFilter(this.value);
            }
        });
    },
    ....

Cheers

2 Comments

what's special about initComplete that made it work?
initComplete function unbind the default trigger and add one of my own
1

That's it code bellow It works so fine !

$(function() {

            var  table = $('#DataTable1').DataTable({
                 proccessing: true,
                 searching: true,
                 paging: true,
                 serverSide: true,
                 initComplete: function() {
                     $('.dataTables_filter input').unbind();
                     $('.dataTables_filter input').bind('keyup', function(e){
                         var code = e.keyCode || e.which;
                         if (code == 13) { 
                             table.search(this.value).draw();
                         }
                     });
                 },
                 ajax: {
                    url: '@Url.Action("Paginacao")',
                    type: 'POST' 
                },
                language: {
                    url: '/plugins/datatables/lang/Portuguese-Brasil.json'
                },
                columns:
                [
                        { "data": "id", visible: false },
                        { "data": "nome", "autoWidth": true },
                        { "data": "cnpj", "autoWidth": true },
                    {
                        "render": function(data, type, full, meta) {
                            return '<a [email protected]("Editar", "Usuario")?id='+full.id+'><b><i class=\"fa fa-edit bigfonts\"></i> Editar</b></a>';
                        }
                    }
                ]
            });

        });

Comments

0

You can use with jQuery.

// get the global text
var globalSearch = $("#txtGlobal").val();

// then put them in the search textboxes
$("input[type='search']").val(globalSearch);
// trigger keyup event on the datatables
$("input[type='search']").trigger("keyup.DT");

$("input[type='search']") will get all of the search textboxes.

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.