|
| 1 | +--- |
| 2 | +title: Copy Text from Grid Row to Clipboard |
| 3 | +description: Learn how to add a right-click context menu option to copy text from a Grid to the clipboard and paste it into the Editor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Copy Text from Grid to Clipboard and Paste into UI for Blazor Editor |
| 6 | +meta_title: How to Copy Text from Grid to Clipboard and Paste into UI for Blazor Editor |
| 7 | +slug: grid-kb-copy-text-clipboard |
| 8 | +tags: blazor, clipboard, context-menu, copy-text, grid |
| 9 | +res_type: kb |
| 10 | +ticketid: 1695036 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Grid for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I want to add a right-click context menu to a Grid, which allows copying text from the Grid to the clipboard. The copied text can then be pasted into an Editor or anywhere else. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +To achieve this, implement a context menu for the Grid and include a "Copy to Clipboard" option. Follow these steps: |
| 31 | + |
| 32 | +1. Define the context menu and its items. |
| 33 | +2. Handle the right-click event to show the context menu. |
| 34 | +3. Implement the logic to copy the selected row's text to the clipboard by using the [navigator clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard). |
| 35 | + |
| 36 | +````Razor |
| 37 | +@using System.Collections.Generic |
| 38 | +@using System.Collections.ObjectModel |
| 39 | +@inject IJSRuntime JS |
| 40 | +
|
| 41 | +<TelerikEditor @bind-Value="@TheEditorValue" Width="650px" Height="400px"></TelerikEditor> |
| 42 | +
|
| 43 | +<TelerikContextMenu @ref="@ContextMenuRef" Data="@MenuItems" |
| 44 | + OnClick="@((MenuItem item) => ContextMenuClickHandler(item))"> |
| 45 | +</TelerikContextMenu> |
| 46 | +
|
| 47 | +<TelerikGrid Data="@GridData" @ref="@GridRef" |
| 48 | + Pageable="true" |
| 49 | + OnRowContextMenu="@OnContextMenu"> |
| 50 | + <GridColumns> |
| 51 | + <GridColumn Field=@nameof(SampleData.ID) /> |
| 52 | + <GridColumn Field=@nameof(SampleData.Name) /> |
| 53 | + </GridColumns> |
| 54 | +</TelerikGrid> |
| 55 | +
|
| 56 | +@code { |
| 57 | + private string TheEditorValue { get; set; } |
| 58 | + private ObservableCollection<SampleData> GridData { get; set; } |
| 59 | + private List<MenuItem> MenuItems { get; set; } |
| 60 | + private SampleData SelectedPerson { get; set; } |
| 61 | + private TelerikContextMenu<MenuItem> ContextMenuRef { get; set; } |
| 62 | + private TelerikGrid<SampleData> GridRef { get; set; } |
| 63 | +
|
| 64 | + public class MenuItem |
| 65 | + { |
| 66 | + public string Text { get; set; } |
| 67 | + public ISvgIcon Icon { get; set; } |
| 68 | + public Action Action { get; set; } |
| 69 | + public string CommandName { get; set; } |
| 70 | + } |
| 71 | +
|
| 72 | + private async Task OnContextMenu(GridRowClickEventArgs args) |
| 73 | + { |
| 74 | + SelectedPerson = args.Item as SampleData; |
| 75 | +
|
| 76 | + if (args.EventArgs is MouseEventArgs mouseEventArgs) |
| 77 | + { |
| 78 | + await ContextMenuRef.ShowAsync(mouseEventArgs.ClientX, mouseEventArgs.ClientY); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + private async Task ContextMenuClickHandler(MenuItem item) |
| 83 | + { |
| 84 | + if (item.Action != null) |
| 85 | + { |
| 86 | + item.Action.Invoke(); |
| 87 | + } |
| 88 | + else if (item.CommandName == "CopyRow" && SelectedPerson != null) |
| 89 | + { |
| 90 | + string rowText = $"ID: {SelectedPerson.ID}, Name: {SelectedPerson.Name}"; |
| 91 | + await CopyToClipboard(rowText); |
| 92 | + } |
| 93 | +
|
| 94 | + SelectedPerson = null; |
| 95 | + } |
| 96 | +
|
| 97 | + private async Task CopyToClipboard(string text) |
| 98 | + { |
| 99 | + await JS.InvokeVoidAsync("navigator.clipboard.writeText", text); |
| 100 | + } |
| 101 | +
|
| 102 | + protected override void OnInitialized() |
| 103 | + { |
| 104 | + MenuItems = new List<MenuItem>() |
| 105 | + { |
| 106 | + new MenuItem(){ Text = "Copy Row Text", Icon = SvgIcon.Copy, CommandName="CopyRow" }, |
| 107 | + new MenuItem(){ Text = "Delete", Icon = SvgIcon.Trash, Action = DeleteItem } |
| 108 | + }; |
| 109 | +
|
| 110 | + GridData = new ObservableCollection<SampleData>(); |
| 111 | + for (int i = 1; i <= 20; i++) |
| 112 | + { |
| 113 | + GridData.Add(new SampleData() |
| 114 | + { |
| 115 | + ID = i, |
| 116 | + Name = $"Employee {i}" |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | +
|
| 121 | + private void DeleteItem() |
| 122 | + { |
| 123 | + if (SelectedPerson != null) |
| 124 | + { |
| 125 | + GridData.Remove(SelectedPerson); |
| 126 | + } |
| 127 | + } |
| 128 | +
|
| 129 | + public class SampleData |
| 130 | + { |
| 131 | + public int ID { get; set; } |
| 132 | + public string Name { get; set; } |
| 133 | + } |
| 134 | +} |
| 135 | +```` |
| 136 | + |
| 137 | +## See Also |
| 138 | + |
| 139 | +* [Grid Overview](slug:grid-overview) |
| 140 | +* [Context Menu Overview](slug:contextmenu-overview) |
0 commit comments