Capture keypressed event on the Datatable.
Capture keypressed event on the Datatable.
I'm having a lot of trouble trying to capture keypressed event on my datatable.
I have tried jQuery("#mytable").keypress, jQuery("#mytable").live("keypressed") and keydown as well but the event handler is not executed.
I added the keypressed event to jQuery(document).keypress which worked very well but doesn't help me because I only want to capture the event from the datatable when the user has set focus on it pressed some key.
Im trying to implement a ctrl-a, select all, functionality for my table. Would be extremely happy if anyone could give me a hint on how to solve this.
I have tried jQuery("#mytable").keypress, jQuery("#mytable").live("keypressed") and keydown as well but the event handler is not executed.
I added the keypressed event to jQuery(document).keypress which worked very well but doesn't help me because I only want to capture the event from the datatable when the user has set focus on it pressed some key.
Im trying to implement a ctrl-a, select all, functionality for my table. Would be extremely happy if anyone could give me a hint on how to solve this.
This discussion has been closed.
Replies
Allan
Trying to figure out an alternative way to do ctrl-a, select all, function to the table.
Maybe bind the keypress event, and if any of the rows in the datatable has css style "selected" and is visible, then ctrl-a should add the class "selected" to all tr's of the table and at the same time prevent the default action of ctrl-a and propagation of the ctrl-a
[code]
// When ctrl+a is typed on the keyboard select all the rows in the table (the table has to have focus)
$('.dataTables_scrollBody').keydown
(
function(event)
{
if ((event.ctrlKey) && (event.which === 65))
{
event.preventDefault();
$('tbody tr' ,this).addClass('ui-selected').each( function(){ that.fnSetSelected(this); } );
that.fnSetButtonState(that);
}
}
);
[/code]
Hope this helps,
Drew
Everything else is just ignored. How do you manage to get keydown to work on dataTables_scrollBody? do you use any extra plugins?
[code]
var oEvent;
$('.dataTables_scrollBody tbody')
.selectable
(
{
filter : 'tr',
selecting : function (event, ui)
{
// Set the focus to the scroll body so that we can do a ctrl+a on the table
$('.dataTables_scrollBody').focus();
....
[/code]
I tried something very similar earlier today but I missed ".dataTables_scrollBody tbody" and tried to bind it directly to the table. That didnt work. Thanx!
The jquery ui team is working on an update for the selectable command that will have more keyboard functionality including ctrl+a and up and down arrows, as well as shift and ctrl clicking.
http://wiki.jqueryui.com/Selectable
Absolutely no idea when they will have it released though.
Allan
I put a click event on the tr-rows of the datatable, which focused on the table with
$('.dataTables_scrollBody').click(function(e){
$('.dataTables_scrollBody').focus();
});
and then put a keypress eventhandler like this:
$('.dataTables_scrollBody').keypress(function(e){
// if A and cmd/ctrl select all trs.
// else do nothing
});
with focus on the table, keypress actually worked.
mind that the code above is a little psuedo code that I have never executed.
Allan