Hi,
thanks for your question, and sorry for the trouble.
Yes, that code that you pasted would trigger a search when the “Enter” key is pressed (and it can be integrated to the DataTables JS call by using a TablePress filter hook). However, it would not stop the “live” filtering from being executed whenever a key is pressed. So, that’s not that useful as is.
Can you provide more detail as to where you found that code?
Regards,
Tobias
Thanks for the reply, Tobias! I found it at this link:
http://www.datatables.net/forums/discussion/2990/how-to-filter-with-the-search-text-box-after-press-enter-key/p1
I also see it mentioned here, with an else method. Would that disable the on the fly search?
http://stackoverflow.com/questions/14619498/datatables-global-search-on-keypress-of-enter-key-instead-of-any-key-keypress
IF:
$("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);
}
});
} );
[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
Thanks so much for all your help, and for creating such a great plugin!
Hi,
thanks for those links and the code example!
That helps a lot! The key is that mentioned unbind(). I should have thought of this 🙂
Please try adding this to your theme’s “functions.php” or a small new plugin:
add_filter( 'tablepress_datatables_command', 'tablepress_filter_on_enter', 10, 5 );
function tablepress_filter_on_enter( $command, $html_id, $parameters, $table_id, $js_options ) {
$name = 'DT_' + str_replace( '-', '_', $html_id );
$command = <<<JS
var {$name} = $('#{$html_id}').dataTable({$parameters});
$('#{$html_id}_filter input').unbind().bind('keyup', function(e) {
if( 13 == e.keyCode )
{$name}.fnFilter( this.value );
});
JS;
return $command;
}
That should change the search behavior on all your tables.
Regards,
Tobias
Thanks Tobias! I might be doing it wrong, but when I add the code to my functions.php, the search box and pagination go away. Do you know why that might be happening?
Hi,
ah, bummer. Most likely I made a mistake in my code somewhere that is now causing an error.
Could you please post a link to the page with the table where this happens, so that I can have a direct look at this? Thanks!
Regards,
Tobias
Hi,
ah, found it… Small typo (after mixing up PHP and JavaScript).
Please change
$name = 'DT_' + str_replace( '-', '_', $html_id );
to
$name = 'DT_' . str_replace( '-', '_', $html_id );
Regards,
Tobias
Thanks Tobias! It works perfectly.
Hi,
very nice! Thanks for the confirmation!
Best wishes,
Tobias
Hi Tobias,
It is not working… please give me plugin for that….
when i put this code in function.php …. it shows error
Regards,
Mohaunix
Hi,
this should go into the “functions.php” of your theme. Are you really using that file?
Otherwise, you can easily create a plugin for it by copying the general structure from any other TablePress Extension. The important part is the comment part at the top.
Regards,
Tobias