I have a (home) table with multiple physical rows that has a lot of logical rows that I want to scroll (also) using the keyboard. In case of concentration of the descendant who understands the key, I want the event to be processed by the descendant and ignored by my table.
Perhaps the code makes it clear:
scroll.keydown = function(e) { if (e.shiftKey | e.controlKey | e.altKey) return; var isText = e.target.type === "text"; var K = $.ui.keyCode; switch (e.keyCode) { case K.HOME: if (isText) return; scroll.move(-1000000); break; case K.END: if (isText) return; scroll.move(+1000000); break; case K.PAGE_UP: scroll.move(-10); break; case K.PAGE_DOWN: scroll.move(+10); break; case K.UP: scroll.move(-1); break; case K.DOWN: scroll.move(+1); break; default: return; } e.preventDefault(); }; <table tabindex="0" ng-keydown="scroll.keydown($event)" ...> ... <input ...>
This works fine, but it is very stupid: the table knows that it contains input elements, and knows that such an element can use, for example, the HOME key, so it does a test to leave it alone.
This is very wrong. Now I have added select , which will require a similar test in the case of DOWN for it to work.
Funny, there is another ng-keydown component inside my table that handles events in a very similar way, but no problem:
- When I press
DOWN while my internal component is focused, it hits the event first, and everything is fine. - When I press
DOWN and select focuses, my table first receives the event and scrolls. Instead, I want the choice to be open, as usual.
Tested in chrome version 60.0.3112.113 using angle symbols v1.5.0 and 1.12.1.
Plunkr
http://plnkr.co/edit/nr2JVJU1U16465F1WKkC?p=preview
Explanation
- Any event that is meaningless for the current focused child must be handled by the table.
- For example,
PAGE_UP means nothing for input , so you need to scroll through the table even if the focused child handles some other events, for example, HOME . - This is why I added
if (isText) return; in HOME and END only those that are useful in input , and not for other keys that are not.
source share