|
| 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-kb-checkbox-column-prevent-click |
| 7 | +position: |
| 8 | +tags: grid, checkbox, prevent, click, deselection |
| 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. Call a JavaScript function in the `OnAfterRenderAsync` method and in the `OnRead` event handler of the Grid. |
| 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 | +@using Telerik.DataSource.Extensions; |
| 37 | +@inject IJSRuntime JS; |
| 38 | +
|
| 39 | +<TelerikGrid TItem="@SampleModel" |
| 40 | + OnRead="@OnGridRead" |
| 41 | + AutoGenerateColumns="true" |
| 42 | + Sortable="true" |
| 43 | + Pageable="true" |
| 44 | + FilterMode="@GridFilterMode.FilterRow" |
| 45 | + Height="400px" |
| 46 | + SelectionMode="@GridSelectionMode.Multiple" |
| 47 | + SelectedItems="@SelectedRows"> |
| 48 | + <GridColumns> |
| 49 | + <GridCheckboxColumn SelectAll="true" CheckBoxOnlySelection="false" /> |
| 50 | + </GridColumns> |
| 51 | +</TelerikGrid> |
| 52 | +
|
| 53 | +<script suppress-error="BL9992"> |
| 54 | + function handleDeselection() { |
| 55 | + setTimeout(function() { |
| 56 | + preventDeselection() |
| 57 | + }, 300) |
| 58 | + } |
| 59 | +
|
| 60 | + function preventClickHandler(event) { |
| 61 | + if (event.target === event.currentTarget) { |
| 62 | + event.preventDefault(); |
| 63 | + event.stopPropagation(); |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + function preventDeselection() { |
| 68 | + document.querySelectorAll('.k-grid-content .k-table-row td:first-child').forEach(function(td) { |
| 69 | + td.addEventListener('click', preventClickHandler, true); |
| 70 | + }); |
| 71 | + } |
| 72 | +</script> |
| 73 | +
|
| 74 | +@code { |
| 75 | + private List<SampleModel> GridData { get; set; } |
| 76 | +
|
| 77 | + private IEnumerable<SampleModel> SelectedRows { get; set; } = Enumerable.Empty<SampleModel>(); |
| 78 | +
|
| 79 | + private string LastOnRead { get; set; } |
| 80 | +
|
| 81 | + protected override void OnInitialized() |
| 82 | + { |
| 83 | + GenerateData(); |
| 84 | + SelectedRows = new List<SampleModel>() { GridData.ElementAt(2) }; |
| 85 | +
|
| 86 | + base.OnInitialized(); |
| 87 | + } |
| 88 | +
|
| 89 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 90 | + { |
| 91 | + if (firstRender) |
| 92 | + { |
| 93 | + await Task.Delay(1); |
| 94 | + await JS.InvokeVoidAsync("handleDeselection"); |
| 95 | + } |
| 96 | + await base.OnAfterRenderAsync(firstRender); |
| 97 | + } |
| 98 | +
|
| 99 | + private async Task OnGridRead(GridReadEventArgs args) |
| 100 | + { |
| 101 | + var result = GridData.ToDataSourceResult(args.Request); |
| 102 | + args.Data = result.Data; |
| 103 | + args.Total = result.Total; |
| 104 | +
|
| 105 | + var now = DateTime.Now; |
| 106 | + LastOnRead = now.ToLongTimeString() + "." + now.Millisecond; |
| 107 | +
|
| 108 | + await Task.Delay(1); |
| 109 | + await JS.InvokeVoidAsync("handleDeselection"); |
| 110 | + } |
| 111 | +
|
| 112 | + private void GenerateData() |
| 113 | + { |
| 114 | + GridData = new List<SampleModel>(); |
| 115 | +
|
| 116 | + for (int i = 1; i <= 100; i++) |
| 117 | + { |
| 118 | + GridData.Add(new SampleModel() { Id = i, Text = $"Item{i}" }); |
| 119 | + } |
| 120 | + } |
| 121 | +
|
| 122 | + public class SampleModel |
| 123 | + { |
| 124 | + public int Id { get; set; } |
| 125 | + public string Text { get; set; } |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
0 commit comments