Skip to content

Commit feb4918

Browse files
kendo-botKB BotTsvetomir-Hr
authored
Added new kb article grid-prevent-rowclick-when-selecting-text (#3111)
* Added new kb article grid-prevent-rowclick-when-selecting-text * chore: polish article --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Tsvetomir Hristov <[email protected]>
1 parent b764967 commit feb4918

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Prevent RowClick Event When Selecting Text in Grid for Blazor
3+
description: Learn how to prevent the RowClick event from being triggered in Grid for Blazor when selecting text using JavaScript interop.
4+
type: how-to
5+
page_title: Avoid RowClick Event Trigger During Text Selection in Grid for Blazor
6+
meta_title: Avoid RowClick Event Trigger During Text Selection in Grid for Blazor
7+
slug: grid-kb-prevent-rowclick-when-selecting-text
8+
tags: grid, blazor, rowclick, text-selection
9+
res_type: kb
10+
ticketid: 1692651
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td> Product </td>
18+
<td> Grid for Blazor </td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
I want to prevent the [OnRowClick](slug:grid-events#onrowclick) event in the [Grid for Blazor](slug:grid-overview) from executing when a user selects text by dragging to highlight it.
26+
27+
This knowledge base article also answers the following questions:
28+
- How to prevent row click event in a Blazor Grid during text selection?
29+
- How to check for text selection before firing Grid events?
30+
- How to use JavaScript interop for handling the `OnRowClick` event in Telerik Blazor Grid?
31+
32+
## Solution
33+
34+
To achieve this, use JavaScript interop to detect text selection. Follow these steps:
35+
36+
1. Define a JavaScript helper function that checks whether any text is currently selected on the page.
37+
2. Inject the IJSRuntime service into your Blazor component to enable JavaScript interop.
38+
3. Call the JavaScript function within your `OnRowClick` event handler to bypass logic when text is selected conditionally.
39+
40+
````RAZOR
41+
@inject IJSRuntime JS
42+
43+
<h3>Grid with Safe Row Click Handling</h3>
44+
45+
@if (!string.IsNullOrEmpty(ClickedPersonName))
46+
{
47+
<p><strong>Last clicked person:</strong> @ClickedPersonName</p>
48+
}
49+
50+
<TelerikGrid Data="@People"
51+
OnRowClick="@OnRowClickHandler"
52+
Height="300px">
53+
<GridColumns>
54+
<GridColumn Field="Id" Title="ID" />
55+
<GridColumn Field="Name" Title="Name" />
56+
<GridColumn Field="Email" Title="Email" />
57+
</GridColumns>
58+
</TelerikGrid>
59+
60+
@code {
61+
public class Person
62+
{
63+
public int Id { get; set; }
64+
public string Name { get; set; } = string.Empty;
65+
public string Email { get; set; } = string.Empty;
66+
}
67+
68+
private readonly List<Person> People = new()
69+
{
70+
new Person { Id = 1, Name = "Alice Johnson", Email = "[email protected]" },
71+
new Person { Id = 2, Name = "Bob Smith", Email = "[email protected]" },
72+
new Person { Id = 3, Name = "Carol Lee", Email = "[email protected]" }
73+
};
74+
75+
private string ClickedPersonName = "";
76+
77+
private async Task OnRowClickHandler(GridRowClickEventArgs args)
78+
{
79+
var isTextSelected = await JS.InvokeAsync<bool>("isTextSelected");
80+
if (isTextSelected)
81+
{
82+
ClickedPersonName = "Text was selected, row click ignored.";
83+
return;
84+
}
85+
86+
var item = (Person)args.Item;
87+
ClickedPersonName = item.Name;
88+
}
89+
}
90+
91+
@* Inline JavaScript for detecting text selection *@
92+
<script>
93+
window.isTextSelected = function () {
94+
return window.getSelection().toString().length > 0;
95+
};
96+
</script>
97+
````
98+
99+
## See Also
100+
101+
* [Grid OnRowClick event](slug:grid-events#onrowclick)

0 commit comments

Comments
 (0)