|
| 1 | +--- |
| 2 | +title: Grid CheckBox column prevent accidental click |
| 3 | +description: How to prevent an accidental click in the CheckBox column of the Grid |
| 4 | +type: how-to |
| 5 | +page_title: Grid CheckBox column prevent accidental click |
| 6 | +slug: grid-checkbox-column-prevent-click |
| 7 | +position: |
| 8 | +tags: |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +An accidental click outside the checkbox in the Grid's CheckBox column deselects the currently selected rows. How to prevent that from happening and ensure that only a click on the checkbox deselects the respective row? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +You can prevent the click event of the cells in the CheckBox column of the Grid with JavaScript. |
| 31 | + |
| 32 | +1. Handle the `OnRowClick` event of the Grid and call a JavaScript function in it. |
| 33 | +2. In the function, attach a `click` event handler to the `td` Html elements in the CheckBox column and prevent the event conditionally |
| 34 | + |
| 35 | +```razor |
| 36 | +@inject IJSRuntime JS; |
| 37 | +
|
| 38 | +<TelerikGrid Data="@GridData" |
| 39 | + SelectionMode="@GridSelectionMode.Multiple" |
| 40 | + SelectedItems="@SelectedEmployees" |
| 41 | + OnRowClick="@OnRowClickHandler" |
| 42 | + Pageable="true"> |
| 43 | + <GridColumns> |
| 44 | + <GridCheckboxColumn SelectAll="true" CheckBoxOnlySelection="false" /> |
| 45 | + <GridColumn Field="@nameof(Employee.Name)" /> |
| 46 | + <GridColumn Field="@nameof(Employee.Team)" /> |
| 47 | + </GridColumns> |
| 48 | +</TelerikGrid> |
| 49 | +
|
| 50 | +<script suppress-error="BL9992"> |
| 51 | + function handleDeselection() { |
| 52 | + setTimeout(function() { |
| 53 | + preventDeselection() |
| 54 | + }, 300) |
| 55 | +
|
| 56 | + } |
| 57 | +
|
| 58 | + function preventClickHandler(event) { |
| 59 | + if (event.target === event.currentTarget) { |
| 60 | + event.preventDefault(); |
| 61 | + event.stopPropagation(); |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + function preventDeselection() { |
| 66 | + document.querySelectorAll('.k-grid-content .k-table-row td:first-child').forEach(function(td) { |
| 67 | + td.addEventListener('click', preventClickHandler, true); |
| 68 | + }); |
| 69 | + } |
| 70 | +</script> |
| 71 | +
|
| 72 | +@code { |
| 73 | + private List<Employee> GridData { get; set; } = new(); |
| 74 | +
|
| 75 | + private IEnumerable<Employee> SelectedEmployees { get; set; } = Enumerable.Empty<Employee>(); |
| 76 | +
|
| 77 | + private string SelectedItemsChangedLog { get; set; } = string.Empty; |
| 78 | +
|
| 79 | + private async Task OnRowClickHandler(GridRowClickEventArgs args) |
| 80 | + { |
| 81 | + // call a function that will prevent deselection of already selected rows |
| 82 | + await JS.InvokeVoidAsync("handleDeselection"); |
| 83 | + } |
| 84 | +
|
| 85 | +
|
| 86 | + protected override void OnInitialized() |
| 87 | + { |
| 88 | + for (int i = 1; i <= 15; i++) |
| 89 | + { |
| 90 | + GridData.Add(new Employee() |
| 91 | + { |
| 92 | + EmployeeId = i, |
| 93 | + Name = $"Employee {i}", |
| 94 | + Team = $"Team {i % 3 + 1}" |
| 95 | + }); |
| 96 | + } |
| 97 | +
|
| 98 | + SelectedEmployees = new List<Employee>() { GridData.ElementAt(2) }; |
| 99 | + } |
| 100 | +
|
| 101 | + public class Employee |
| 102 | + { |
| 103 | + public int EmployeeId { get; set; } |
| 104 | + public string Name { get; set; } = string.Empty; |
| 105 | + public string Team { get; set; } = string.Empty; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
0 commit comments