Skip to content

Commit e2c5944

Browse files
committed
kb(grid): Add KB for focusable elements inside a navigable Grid template
1 parent b90431b commit e2c5944

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: Cannot Open a DropDownList inside a Navigable Grid
3+
description: Learn how to troubleshoot the inability to focus or open a Button, DropDownList, ComboBox, or other components inside a navigable Telerik Grid for Blazor.
4+
type: troubleshooting
5+
page_title: Cannot Open a DropDownList inside a Navigable Grid
6+
slug: grid-kb-cannot-open-dropdown-in-navigable-grid
7+
tags: telerik, blazor, grid, treelist
8+
ticketid: 1623773
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>
19+
Grid for Blazor, <br />
20+
TreeList for Blazor
21+
</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
26+
## Description
27+
28+
Dropdown components do not open inside a Grid column `<Template>` when the Grid has [`Navigable="true"`](https://demos.telerik.com/blazor-ui/grid/keyboard-navigation).
29+
30+
## Cause
31+
32+
When the Grid keyboard navigation is enabled, the table cells automatically gain focus when a cell is clicked. If the cell contains a focusable element, this element may lose focus unexpectedly on click.
33+
34+
## Solution
35+
36+
Normally, editor components belong to [`<EditorTemplate>`]({%slug grid-templates-editor%})s, but this article assumes that editor templates are not an option. Thus, other possible options include:
37+
38+
* If data operations like sorting and filtering are not necessary for the affected columns, place buttons or dropdown components like ComboBox or DropDownList inside a [Grid Command Column]({%slug components/grid/columns/command%}) instead. Grid command columns do not gain focus automatically when a nested focusable element is clicked.
39+
* If data operations for the affected columns are required, then use a container with `@onclick:stopPropagation` inside the `<GridColumn>` template. This will prevent the Grid from knowing about the clicks, so the data cell will not gain focus automatically.
40+
41+
>caption Using dropdowns, buttons and other focusable elements inside a navigable Grid
42+
43+
```RAZOR
44+
<TelerikGrid Data="@GridData" Navigable="true">
45+
<GridColumns>
46+
<GridColumn Field="@nameof(Product.Name)" Title="Cells will steal focus on click" HeaderClass="warning">
47+
<Template>
48+
@{ var dataItem = (Product)context; }
49+
<span class="template-span">
50+
<TelerikButton OnClick="@( () => OnButtonClick(dataItem.Id) )">@dataItem.Name</TelerikButton>
51+
<TelerikDropDownList Data="@DropDownListData" @bind-Value="@dataItem.Active" />
52+
</span>
53+
</Template>
54+
</GridColumn>
55+
<GridColumn Field="@nameof(Product.Name)" Title="Template with stopPropagation" HeaderClass="success">
56+
<Template>
57+
@{ var dataItem = (Product)context; }
58+
<span @onclick:stopPropagation class="template-span">
59+
<TelerikButton OnClick="@( () => OnButtonClick(dataItem.Id) )">@dataItem.Name</TelerikButton>
60+
<TelerikDropDownList Data="@DropDownListData" @bind-Value="@dataItem.Active" />
61+
</span>
62+
</Template>
63+
</GridColumn>
64+
<GridCommandColumn Title="Command Column" HeaderClass="success">
65+
@{ var dataItem = (Product)context; }
66+
<span class="template-span">
67+
<TelerikButton OnClick="@( () => OnButtonClick(dataItem.Id) )">@dataItem.Name</TelerikButton>
68+
<TelerikDropDownList Data="@DropDownListData" @bind-Value="@dataItem.Active" />
69+
</span>
70+
</GridCommandColumn>
71+
</GridColumns>
72+
</TelerikGrid>
73+
74+
<style>
75+
.template-span {
76+
display: flex;
77+
gap: 1em;
78+
padding: .3em;
79+
}
80+
81+
.warning {
82+
background-color: var(--kendo-color-warning);
83+
}
84+
85+
.success {
86+
background-color: var(--kendo-color-success);
87+
color: var(--kendo-color-on-success);
88+
}
89+
</style>
90+
91+
@code {
92+
List<Product> GridData { get; set; } = new();
93+
94+
List<bool> DropDownListData { get; set; } = new List<bool>() { true, false };
95+
96+
private void OnButtonClick(int id)
97+
{
98+
Console.WriteLine($"Button click on data item {id}");
99+
}
100+
101+
protected override void OnInitialized()
102+
{
103+
for (int i = 1; i <= 3; i++)
104+
{
105+
GridData.Add(new Product()
106+
{
107+
Id = i,
108+
Name = $"Name {i}",
109+
Active = i % 3 == 0
110+
});
111+
}
112+
}
113+
114+
public class Product
115+
{
116+
public int Id { get; set; }
117+
public string Name { get; set; } = string.Empty;
118+
public bool Active { get; set; }
119+
}
120+
}
121+
```
122+
123+
## See Also
124+
125+
* [Grid Keyboard Navigation Demo](https://demos.telerik.com/blazor-ui/grid/keyboard-navigation)
126+
* [Grid Column Template]({%slug grid-templates-column%})
127+
* [Grid Command Column]({%slug components/grid/columns/command%})

0 commit comments

Comments
 (0)