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!
Asked
Active
Viewed 3,082 times
1 Answers
2
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:
-
Thanks Oleg. This is pretty close to what I wanted. In the "Date" column from your example, is there any way to get the entire date selected and not get delimited by - or /? The column my users would be selecting is URL. – Jai Dec 01 '11 at 14:31
-
@Jai: I don't understand what you mean. How you can see on the picture above the whole cell will be highlighted. – Oleg Dec 01 '11 at 15:39
-
I tried posting an image but failed to do so. Please look at the data 01-Oct-2007 in the following screenshot. [screenshot](https://picasaweb.google.com/115278692335129964011/Stackof#5681191684714414994) I am looking to do a selection for copy instead of just highlighting the cell – Jai Dec 01 '11 at 16:43
-
1@Jai: There are LARGE difference between highlighting of the cell and selection of the text in the cell! I created new demo for you (see the **UPDATED** part of my answer). – Oleg Dec 01 '11 at 17:27
-
1Thank you very much. I always find your contributions to stackoverflow very valuable – Jai Dec 01 '11 at 18:13