Friday, May 22, 2009

GWT disable text selection in an HTMLTable

While modifying FlexTable to handle cell selection It took me a while to figure out how to disable the native text selection behavior of the browsers on 'SHIFT+Click' events. Turns out this native javascript function does the job pretty well. I tried playing with event.preventDefault() and event.stopPropagation() but that didn't work.

private native static void disableTextSelectInternal(Element e, boolean disable)/*-{
if (disable) {
e.ondrag = function () { return false; };
e.onselectstart = function () { return false; };
e.style.MozUserSelect="none"
} else {
e.ondrag = null;
e.onselectstart = null;
e.style.MozUserSelect="text"
}
}-*/;

@Override
public void onKeyDown(KeyDownEvent event) {
if (event.isShiftKeyDown()) {
disableTextSelectInternal(table.getElement(), true);
}
...

6 comments:

Erik Uzureau said...

nice, succinct post. thanks!

Anonymous said...

And it works perfectly- thanks, saved me a lot of time

Jill said...

thanks! got it to work on firefox; need to test it on ie too :)

Anonymous said...

you are like god now !
thank you very much

Alexander said...

спасибо!

Alexander said...

spasibo