This is a migrated thread and some comments may be shown as answers.

Copy Grid selection to clipboard

4 Answers 403 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jacob
Top achievements
Rank 1
Jacob asked on 15 Nov 2011, 10:24 AM
I have a grid with the selectable option sat to "multiple, cell". I use the change() event on the Grid that fires when selection changes. In this callback function, I can call this.select() to get a jQuery object/array of objects of the selected element(s). So I have something like:

jQuery(td.k-state-selected, td.k-state-selected, td.k-state-focused, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected, td.k-state-selected)

Now, I could build something to copy this object's text content (or anything else, cause I have the jQuery object at hand) to the clipboard, but it's rather difficult to keep the table structure that I actually had in the table. How do I know which TD is the last in selection and is followed by a line break (new TR)? My selection was partial, so I can't use :last-child or similar since there are more TDs in this TR (see attached file).

Also, any way to stop the normal, in browser text selection when I have enabled selection of cells? 

And finally: any tips on a nice copy-to-clipboard solution? It'd be nice to do without Flash if possible, and browser requirements are rather high (IE8+).

Thanks.

/Jacob

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 16 Nov 2011, 09:19 AM
Hello Jacob,

You could traverse all cells and keep their parent row. When the parent row changes then you are on a new row. Something like this:

var selected = $("#grid").data("kendoGrid").select();
 
var row = selected[0].parentNode;
var text = "";
 
selected.each(function() {
    text += $(this).text;
    if (row != this.parentNode) {
         // we are on a new row
        text += "\n";
        row = this.parentNode;
    }
});


You can check this stackoverflow question for an answer how to disable text selection.

 As far as I know cross-browser clipboard access is only possible via Flash. You can check this stackoverflow question for more info.

All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jacob
Top achievements
Rank 1
answered on 16 Nov 2011, 09:36 AM
Thanks for your answer Atanas!

Regarding disabling the text selection in browser with CSS: is it how you do it in this demo? Or is something broken in my grid config? I assumed that you disable the selection automatically when the selectable-optinon is enabled, as it seems to be the case in the demo. 

/Jacob
0
Jacob
Top achievements
Rank 1
answered on 16 Nov 2011, 09:54 AM
Ooops, sorry. I mixed up browsers there. You example behaves exactly as my code. You seem to have disabled the text selection by default on webkit, while Firefox (at least FF 8.0) has text selection enabled. Guess I'll go with the
-moz-user-select: none
My mistake. Thanks again.
0
Nikolay Rusev
Telerik team
answered on 16 Nov 2011, 10:31 AM
Hello Jacob,

We are returning false on selectstart event to prevent text selection when mouse moves. 

Greetings,
Nikolay Rusev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Jacob
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jacob
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or