Highlight cell value on doubleclick for copy

廉价感情. 提交于 2019-12-19 10:27:47

问题


I have a jqGrid. I would like to highlight a particular cell from a row, ondbClickRow. This would make the task of copying the value of a cell onto clipboard, easy for users. Can someone guide me on how to do this? Thanks!


回答1:


In general it would be possible, but you should probably switch off row selection to see highlighting immediately. So the code will be about the following:

beforeSelectRow: function () {
    return false;
},
ondblClickRow: function (rowid, iRow, iCol, e) {
    $(e.target).toggleClass('ui-state-highlight');
}

As the result you can have the grid like

see the corresponding demo here

UPDATED: If you need select the text in the grid cell you can use the idea described here. In case of usage inside of jqGrid the code could be the following:

var selectText = function (element) {
    var doc = element.ownerDocument, selection, range;
    if (doc.body.createTextRange) { // ms
        range = doc.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        selection = window.getSelection();
        if (selection.setBaseAndExtent) { // webkit
            selection.setBaseAndExtent(element, 0, element, 1);
        } else { // moz, opera
            range = doc.createRange();
            range.selectNodeContents(element);
            selection.removeAllRanges();
            selection.addRange(range);
        }
    }
};

$("#list").jqGrid({
    // ... jqGrid options
    ondblClickRow: function (rowid, iRow, iCol, e) {
        selectText(e.target);
    }
});

The next demo demonstrate this:



来源:https://stackoverflow.com/questions/8333933/highlight-cell-value-on-doubleclick-for-copy

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