Skip to content

Commit 071bdee

Browse files
kendo-botKB Botxristianstefanov
authored
Added new kb article grid-copy-text-clipboard (#3290)
* Added new kb article grid-copy-text-clipboard * chore(kb): apply changes as per suggestions --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Hristian Stefanov <Hristian.Stefanov@progress.com>
1 parent 13fec59 commit 071bdee

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
<TelerikTextBox @bind-Value="@TheEditorValue" Placeholder="Paste text here.." Width="300px"></TelerikTextBox>
42+
<br/>
43+
<br/>
44+
<TelerikContextMenu @ref="@ContextMenuRef" Data="@MenuItems"
45+
OnClick="@((MenuItem item) => ContextMenuClickHandler(item))">
46+
</TelerikContextMenu>
47+
48+
<TelerikGrid Data="@GridData" @ref="@GridRef"
49+
Pageable="true"
50+
OnRowContextMenu="@OnContextMenu">
51+
<GridColumns>
52+
<GridColumn Field=@nameof(SampleData.ID) />
53+
<GridColumn Field=@nameof(SampleData.Name) />
54+
</GridColumns>
55+
</TelerikGrid>
56+
57+
@code {
58+
private string TheEditorValue { get; set; }
59+
private ObservableCollection<SampleData> GridData { get; set; }
60+
private List<MenuItem> MenuItems { get; set; }
61+
private SampleData SelectedPerson { get; set; }
62+
private TelerikContextMenu<MenuItem> ContextMenuRef { get; set; }
63+
private TelerikGrid<SampleData> GridRef { get; set; }
64+
65+
private async Task OnContextMenu(GridRowClickEventArgs args)
66+
{
67+
SelectedPerson = args.Item as SampleData;
68+
69+
if (args.EventArgs is MouseEventArgs mouseEventArgs)
70+
{
71+
await ContextMenuRef.ShowAsync(mouseEventArgs.ClientX, mouseEventArgs.ClientY);
72+
}
73+
}
74+
75+
private async Task ContextMenuClickHandler(MenuItem item)
76+
{
77+
if (item.Action != null)
78+
{
79+
item.Action.Invoke();
80+
}
81+
else if (item.CommandName == "CopyRow" && SelectedPerson != null)
82+
{
83+
string rowText = $"ID: {SelectedPerson.ID}, Name: {SelectedPerson.Name}";
84+
await CopyToClipboard(rowText);
85+
}
86+
87+
SelectedPerson = null;
88+
}
89+
90+
private async Task CopyToClipboard(string text)
91+
{
92+
await JS.InvokeVoidAsync("navigator.clipboard.writeText", text);
93+
}
94+
95+
protected override void OnInitialized()
96+
{
97+
MenuItems = new List<MenuItem>()
98+
{
99+
new MenuItem(){ Text = "Copy Row Text", Icon = SvgIcon.Copy, CommandName="CopyRow" },
100+
new MenuItem(){ Text = "Delete", Icon = SvgIcon.Trash, Action = DeleteItem }
101+
};
102+
103+
GridData = new ObservableCollection<SampleData>();
104+
for (int i = 1; i <= 20; i++)
105+
{
106+
GridData.Add(new SampleData()
107+
{
108+
ID = i,
109+
Name = $"Employee {i}"
110+
});
111+
}
112+
}
113+
114+
private void DeleteItem()
115+
{
116+
if (SelectedPerson != null)
117+
{
118+
GridData.Remove(SelectedPerson);
119+
}
120+
}
121+
122+
public class MenuItem
123+
{
124+
public string Text { get; set; }
125+
public ISvgIcon Icon { get; set; }
126+
public Action Action { get; set; }
127+
public string CommandName { get; set; }
128+
}
129+
130+
public class SampleData
131+
{
132+
public int ID { get; set; }
133+
public string Name { get; set; }
134+
}
135+
}
136+
````
137+
138+
## See Also
139+
140+
* [Grid Overview](slug:grid-overview)
141+
* [Context Menu Overview](slug:contextmenu-overview)
142+
* [Grid Context Menu Integration Demo](https://demos.telerik.com/blazor-ui/contextmenu/integration)
143+
* [Grid Context Menu Integration Documentation](slug:contextmenu-integration)

0 commit comments

Comments
 (0)