【问题标题】:Text selection in slickgridslickgrid 中的文本选择
【发布时间】:2013-12-04 10:30:03
【问题描述】:

我已将“enableTextSelectionOnCells”选项设置为 true 以在 slickgrid 中选择文本,但我只能在 IE 和 chrome 中选择文本,而不能在 Firefox 中选择文本。我知道这是 slickgrid 中的错误,它已在 slickgrid 2.2 中修复,但我使用的是 slickgrid V2.1,不想升级到 V2.2。有什么方法可以使用 slickgrid 2.1 在 Firefox 中选择文本

【问题讨论】:

  • 其实我在 2.2 中也没有发现它是防弹的,即使我刚刚更新到最后一个版本,它仍然不能完全工作。如果我双击文本并且文本选择并不总是有效,它不起作用。

标签: slickgrid


【解决方案1】:

我遇到了和你一样的问题,我终于从用户 icoxfog417 提出的拉取请求中找到了解决方案(感谢伙伴),拉取请求不是尚未获得批准(希望很快),但我尝试了它,它适用于我尝试过的所有 3 种浏览器(在我的情况下为 FF27、IE8、Chrome31)。您确实必须修改核心文件中的 1 个 slick.grid.js,但这是值得的 :)
拉取请求是这个:Pull Request #746: fix issue#739

代码更改很简单,如下所示:
修改文件slick.grid.js第2236行,将代码替换为:

// 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);
}

然后在第 2418 行插入(在setFocus() 函数之后),插入这个新代码:

//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);
  }
}

瞧!!!很高兴:)

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 2017-05-18
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2013-04-02
    • 2011-09-03
    相关资源
    最近更新 更多