2

I have a DataTable in YUI. I'm trying to get the table to ignore all keyEvents. I've tried these methods:

YAHOO.util.Event.addListener(singleSelectDataTable, "keydown", function(oEvent) {
    YAHOO.util.Event.stopPropagation(oEvent); 
});

OR

YAHOO.util.Event.preventDefault(singleSelectDataTable.tableKeyEvent);    

OR

singleSelectDataTable.subscribe('tableKeyEvent', function(oArgs) { 
  YAHOO.util.Event.preventDefault(oArgs.event); 
});

I've looked at a couple of YUI examples to intercept click events, but they don't analogize to this specific scenario. I created a standalone HTML test file if that will help: http://pastebin.com/khfR4Stk. The foundational problem is that we don't want to support arrow key up or arrow key down in our tables; it's a scrolling table and in order for it to work properly we would have to adjust the scrolling thumb once the selection goes past the 'shown-window'.

The only other solution I could think of is to subscribe to the tableKeyEvent and then if the keypress is up-arrow, then unselect the newly selected row, selecting the previous row, doing the appropriate analogue for a down-arrow (basically undoing what the keypress just did). This didn't seem like the right solution…

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Son of the Wai-Pan
  • 12,371
  • 16
  • 46
  • 55

3 Answers3

0

The tableKeyEvent is raised after the up/down arrow key has been handled. So trying to stop that event will not help.

Looking at the _onTbodyKeydown function of the DataTable widget, I noticed that setting the selection mode to an invalid mode disables key arrow key navigation. Luckily it doesn't seem to break the other selection handling. At least not in your example.

So just change selectionMode:"single" to selectionMode:"" and you should be fine :-) (Of course there is no guarantee that this will work in future versions)

SuperRembo
  • 116
  • 2
  • 3
  • This isn't the appropriate solution since it disables the mouse from navigating the table. – Son of the Wai-Pan Jan 09 '11 at 12:20
  • I spoke too soon. My actual code is more complicated than the example given. The example given uses the following line: singleSelectDataTable.subscribe("rowClickEvent", singleSelectDataTable.onEventSelectRow); I use this in my actual code: singleSelectDataTable.subscribe("rowClickEvent", function(oArgs) {singleSelectDataTable.onEventSelectRow(oArgs.event, oArgs.target); The latter code doesn't work; is there a way to select the row even if selectionMode is off? – Son of the Wai-Pan Jan 09 '11 at 13:06
  • The mouse navigation does work if you use `singleSelectDataTable.onEventSelectRow(oArgs)` (onEventSelectRow documentation seems incorrect). But in that case the "standard" selection mode is used, which allows selecting multiple rows by using the Ctrl and Shift keys. You can work around this by providing a fake empty event object: `singleSelectDataTable.onEventSelectRow({'target': oArgs.target, 'event': {}});`. – SuperRembo Jan 10 '11 at 08:25
0

Try creating the equiv of this onclick

function noenter(evt)
{
    var k = evt.keyCode||evt.which;
    return k != 13;
}

Get the syntax for getCharCode and tell the script that when it receives input, it needs to deny it.

Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
0

can't you just add an eventhandler that returns false to the keydown event?

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91