KeyTable is a Javascript library which provides keyboard navigation and event binding for any HTML table. With KeyTable Excel style table navigation can be employed to provide features such as editing of a table without requiring a mouse. As a further bonus, KeyTable integrates seamlessly with DataTables.
Let's jump straight into an example: the table below is editable through keyboard navigation. Simply navigate to the cell you wish to edit and hit return. This initialises the excellent jEditable plug-in for jQuery - make your edit and then hit return again to save.
Rendering engine | Browser | Platform(s) | Engine version | CSS grade |
---|---|---|---|---|
Trident | Internet Explorer 4.0 | Win 95+ | 4 | X |
Trident | Internet Explorer 5.0 | Win 95+ | 5 | C |
Trident | Internet Explorer 5.5 | Win 95+ | 5.5 | A |
Trident | Internet Explorer 6 | Win 98+ | 6 | A |
Trident | Internet Explorer 7 | Win XP SP2+ | 7 | A |
Gecko | Firefox 1.0 | Win 98+ / OSX.2+ | 1.7 | A |
Gecko | Firefox 3.0 | Win 2k+ / OSX.3+ | 1.9 | A |
Gecko | Camino 1.5 | OSX.3+ | 1.8 | A |
Gecko | Seamonkey 1.1 | Win 98+ / OSX.2+ | 1.8 | A |
Webkit | Safari 2.0 | OSX.4+ | 419.3 | A |
Webkit | Safari 3.0 | OSX.4+ | 522.1 | A |
Presto | Opera 9.5 | Win 88+ / OSX.3+ | - | A |
Misc | Dillo 0.8 | Embedded devices | - | X |
Misc | Links | Text only | - | X |
Other browsers | All others | - | - | U |
I've put together a couple of other demos:
Using KeyTable on a plain HTML is as simple as creating a new object:
var keys = new KeyTable();
By default, KeyTable will attach itself to the first table it finds in the DOM with the class 'KeyTable' and apply basic navigation controls. There are a number of initialisation parameters that you can pass into the constructor to provide a bit more control over the default behaviour:
Once KeyTable has been initialised you will most likely wish to add event listeners to the cell in the table. This is done thought the methods presented by the 'event' object of KeyTable. The following events are supported:
Each of these events is applied by calling event.{event_name}() (where event_name is of course one of the events from above). This function accepts either two or three inputs:
/* Example event listener */ keys.event.focus( 1, 3, function() { /* processing on cell 1,3 ... */ } ); /* this is exactly the same as */ keys.event.focus( $('#example tbody tr:eq(3) td:eq(1)')[0], function() { /* processing on cell 1,3 ... */ } );
As noted in the above arguments list, when passing in coordinates to attach an event listener to a cell, you can pass null for either (or both) the x and y coordinates. This generalises the event target to occur on the given x or y coordinate, regardless of the other point. For example this allows you to target whole rows or columns, rather than just individual cells.
/* Event listener for a row */ keys.event.focus( null, 0, function() { /* handler for focus events in first row ... */ } ); /* Event listener for a column */ keys.event.focus( 3, null, function() { /* handler for focus events in forth column ... */ } ); /* Event listener for all cells */ keys.event.focus( null, null, function() { /* handler for focus events on all cells ... */ } );
There are three parameters which are passed into the event handler, which can then be used by your handler function to perform whatever actions are necessary:
There is no return parameter expected from the event handler.
Events listeners can be removed through the 'event.remove.{event_name}' object. This function call expects the same inputs as the methods to add events, but in this case the event listener function is optional. If it is left off then all events of that type are removed from that cell.
It can be useful to get information from KeyTable about the currently focused cell. There are three different API functions to give you various pieces of information:
/* API example */ $(document).ready( function () { var keys = new KeyTable( { "table": document.getElementById('example') } ); var coords = keys.fnGetCurrentPosition(); alert( "Initially focused cell is: "+coords[0]+", "+coords[1] ); } );
KeyTable has been tested and works great on:
I expect that plenty of other browser will work just fine as well. There is one little hiccup in the key event handling. It would appear that there is very little consistency between browsers on however to handle repeat events on 'special' keys such as arrow keys. However, KeyTable does detection to overcome this issue.
KeyTable expects the target table to be well defined and to have a tbody tag. It will not work with out this! It also utilises jQuery's selector and event methods in order to provide easy cross platform implementation. However, since my usage of jQuery is limited to just these two components it would be relatively trivial to port KeyTable to any other library you like.
KeyTable is open source software (GPL v2 or BSD 3 point style) and the source can be downloaded from the link below:
If you are interested in a library for event handling on all elements, rather than KeyTable which specialises in table navigation, check out QFocuser by Daniel Steigerwald.
If you do apply KeyTable to any interesting use case, please let me know about it - it's always good to hear how the software on this site is being used!