Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...