Skip to content

Commit 82e0fbd

Browse files
kendo-botKB Botxristianstefanov
authored
Added new kb article autocomplete-copy-text-itemtemplate (#3018)
* Added new kb article autocomplete-copy-text-itemtemplate * chore(Autocomplete): add suggestions --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Hristian Stefanov <Hristian.Stefanov@progress.com>
1 parent 62e3bdc commit 82e0fbd

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: How to Copy Text from an ItemTemplate
3+
description: Learn how to allow users to copy text from the ItemTemplate in the AutoComplete for Blazor component.
4+
type: how-to
5+
page_title: How to Enable Text Copy in AutoComplete Dropdown Blazor
6+
slug: autocomplete-kb-copy-text-itemtemplate
7+
tags: blazor, autocomplete, itemtemplate, clipboard, copy, paste
8+
res_type: kb
9+
ticketid: 1689288
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>AutoComplete for Blazor</td>
19+
<td>DropDownList for Blazor</td>
20+
<td>ComboBox for Blazor</td>
21+
<td>MultiSelect for Blazor</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
26+
## Description
27+
28+
I want to allow users to copy the text from the dropdown list in the [AutoComplete component](slug:autocomplete-overview).
29+
30+
## Solution
31+
32+
To enable text copying from the dropdown list in the AutoComplete, use the `ItemTemplate` to display the desired text alongside a button that uses JavaScript and the Clipboard API for copying. Here’s an example implementation:
33+
34+
````RAZOR
35+
@inject IJSRuntime JS
36+
37+
<style>
38+
.autocomplete-item {
39+
display: flex;
40+
align-items: center;
41+
justify-content: space-between;
42+
gap: 8px;
43+
}
44+
</style>
45+
46+
<TelerikAutoComplete Data="@AutoCompleteData" @bind-Value="@Role" Placeholder="Write your position">
47+
<ItemTemplate>
48+
@{
49+
var text = context.ToString();
50+
}
51+
<div class="autocomplete-item">
52+
<strong>@text</strong>
53+
<TelerikButton OnClick="@(() => CopyToClipboard(text))"
54+
Icon="@SvgIcon.Copy"
55+
FillMode="@ThemeConstants.Button.FillMode.Outline" />
56+
</div>
57+
</ItemTemplate>
58+
</TelerikAutoComplete>
59+
60+
@code {
61+
private string Role { get; set; }
62+
private List<string> AutoCompleteData { get; set; }
63+
private List<string> SourceData { get; set; } = new()
64+
{
65+
"Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Sales Agent", "Architect", "Designer"
66+
};
67+
68+
protected override void OnInitialized()
69+
{
70+
AutoCompleteData = SourceData;
71+
}
72+
73+
private void OnCheckBoxChangeHandler()
74+
{
75+
AutoCompleteData = new List<string>(SourceData);
76+
}
77+
78+
private async Task CopyToClipboard(string text)
79+
{
80+
try
81+
{
82+
var isSupported = await JS.InvokeAsync<bool>("eval", "!!(navigator.clipboard && navigator.clipboard.writeText)");
83+
if (isSupported)
84+
{
85+
await JS.InvokeVoidAsync("navigator.clipboard.writeText", text);
86+
}
87+
else
88+
{
89+
Console.WriteLine("Clipboard API not supported in this browser.");
90+
// Optionally show a notification to the user
91+
}
92+
}
93+
catch (Exception ex)
94+
{
95+
Console.Error.WriteLine($"Clipboard copy failed: {ex.Message}");
96+
// Optionally show a fallback UI message to the user
97+
}
98+
}
99+
}
100+
````
101+
102+
## See Also
103+
104+
* [AutoComplete Documentation](slug:autocomplete-overview)
105+
* [Clipboard API Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
106+
* [Blazor JavaScript Interoperability](https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability)

0 commit comments

Comments
 (0)