Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

jquery - datatables global search on keypress of enter key instead of any key keypress

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
                }
            }); 
        }
    });    
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...