Text selection in slickgrid

岁酱吖の 提交于 2020-01-05 04:36:08

问题


I have set 'enableTextSelectionOnCells' option to true to select text in slickgrid but I can only select text in IE and chrome but not in firefox. I know this is bug in slickgrid and it had been fixed in slickgrid 2.2 but I am using slickgrid V2.1 and don't want to upgrade to V2.2. Is there any way to select text in firefox using slickgrid 2.1


回答1:


I had the same problem as you have and I finally found the solution from a pull request made by the user icoxfog417 (thanks mate), the pull request is not yet approved (hopefully soon) but I tried it and it works on all 3 browsers which I tried (in my case FF27, IE8, Chrome31). You do have to modify 1 of the core file slick.grid.js but it's worth it :)
The pull request is this one: Pull Request #746: fix issue#739

The code change is simple and looks like this:
Modify the file slick.grid.js at line 2236, replace the code with this:

// if this click resulted in some cell child node getting focus,
// don't steal it back - keyboard events will still bubble up
// IE9+ seems to default DIVs to tabIndex=0 instead of -1, so check for cell clicks directly.
if (e.target != document.activeElement || $(e.target).hasClass("slick-cell")) {
    var selection = getTextSelection(); //store text-selection and restore it after
    setFocus();
    setTextSelection(selection);
}

then insert at line 2418 (after the setFocus() function), insert this new code:

//This get/set methods are used for keeping text-selection. These don't consider IE because they don't loose text-selection.
function getTextSelection(){
  var textSelection = null;
  if (window.getSelection) {
    var selection = window.getSelection();
    if (selection.rangeCount > 0) {
      textSelection = selection.getRangeAt(0);
    }
  }
  return textSelection;
}

function setTextSelection(selection){
  if (window.getSelection && selection) {
    var target = window.getSelection();
    target.removeAllRanges();
    target.addRange(selection);
  }
}

Voilà!!! Quite happy about it :)



来源:https://stackoverflow.com/questions/20095803/text-selection-in-slickgrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!