Skip to content

Commit e13e755

Browse files
KB Botxristianstefanov
authored andcommitted
Added new kb article autocomplete-copy-text-itemtemplate
1 parent c0155f1 commit e13e755

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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: autocomplete, blazor, itemtemplate, clipboard, text, 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+
.copy-button {
39+
margin-left: 8px;
40+
}
41+
</style>
42+
43+
<TelerikAutoComplete Data="@AutoCompleteData" @bind-Value="@Role" Placeholder="Write your position">
44+
<ItemTemplate>
45+
@{
46+
var text = context.ToString();
47+
}
48+
<div style="display: flex; align-items: center; justify-content: space-between;">
49+
<strong>@text</strong>
50+
<TelerikButton OnClick="@(() => CopyToClipboard(text))" Class="copy-button" Icon="@SvgIcon.Copy" FillMode="@ThemeConstants.Button.FillMode.Outline" />
51+
</div>
52+
</ItemTemplate>
53+
</TelerikAutoComplete>
54+
55+
@code {
56+
private string Role { get; set; }
57+
private List<string> AutoCompleteData { get; set; }
58+
private List<string> SourceData { get; set; } = new List<string> { "Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Sales Agent", "Architect", "Designer" };
59+
60+
protected override void OnInitialized()
61+
{
62+
AutoCompleteData = SourceData;
63+
}
64+
65+
private void OnCheckBoxChangeHandler()
66+
{
67+
AutoCompleteData = new List<string>(SourceData);
68+
}
69+
70+
private async Task CopyToClipboard(string text)
71+
{
72+
await JS.InvokeVoidAsync("navigator.clipboard.writeText", text);
73+
}
74+
}
75+
````
76+
77+
## See Also
78+
79+
* [AutoComplete Documentation](slug:autocomplete-overview)
80+
* [Clipboard API Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
81+
* [Blazor JavaScript Interoperability](https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability)

0 commit comments

Comments
 (0)