From 5e50a4361258efcf75ee2659173c87b3eef044e7 Mon Sep 17 00:00:00 2001 From: IvanDanchev Date: Mon, 16 Jun 2025 09:46:14 +0300 Subject: [PATCH 1/2] kb(Grid): prevent checkbox column click --- .../grid-checkbox-column-prevent-click.md | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 knowledge-base/grid-checkbox-column-prevent-click.md diff --git a/knowledge-base/grid-checkbox-column-prevent-click.md b/knowledge-base/grid-checkbox-column-prevent-click.md new file mode 100644 index 000000000..b44aae54b --- /dev/null +++ b/knowledge-base/grid-checkbox-column-prevent-click.md @@ -0,0 +1,108 @@ +--- +title: Grid CheckBox column prevent accidental click +description: How to prevent an accidental click in the CheckBox column of the Grid +type: how-to +page_title: Grid CheckBox column prevent accidental click +slug: grid-checkbox-column-prevent-click +position: +tags: +res_type: kb +--- + +## Environment + + + + + + + + +
ProductGrid for Blazor
+ + +## Description + +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? + +## Solution + +You can prevent the click event of the cells in the CheckBox column of the Grid with JavaScript. + +1. Handle the `OnRowClick` event of the Grid and call a JavaScript function in it. +2. In the function, attach a `click` event handler to the `td` Html elements in the CheckBox column and prevent the event conditionally + +```razor +@inject IJSRuntime JS; + + + + + + + + + + + +@code { + private List GridData { get; set; } = new(); + + private IEnumerable SelectedEmployees { get; set; } = Enumerable.Empty(); + + private string SelectedItemsChangedLog { get; set; } = string.Empty; + + private async Task OnRowClickHandler(GridRowClickEventArgs args) + { + // call a function that will prevent deselection of already selected rows + await JS.InvokeVoidAsync("handleDeselection"); + } + + + protected override void OnInitialized() + { + for (int i = 1; i <= 15; i++) + { + GridData.Add(new Employee() + { + EmployeeId = i, + Name = $"Employee {i}", + Team = $"Team {i % 3 + 1}" + }); + } + + SelectedEmployees = new List() { GridData.ElementAt(2) }; + } + + public class Employee + { + public int EmployeeId { get; set; } + public string Name { get; set; } = string.Empty; + public string Team { get; set; } = string.Empty; + } +} +``` From 62179d50f0094226fd98811c5ce76af0d17ca39c Mon Sep 17 00:00:00 2001 From: IvanDanchev Date: Tue, 17 Jun 2025 10:13:42 +0300 Subject: [PATCH 2/2] kb(Grid): add PR review suggestions --- .../grid-checkbox-column-prevent-click.md | 80 ++++++++++++------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/knowledge-base/grid-checkbox-column-prevent-click.md b/knowledge-base/grid-checkbox-column-prevent-click.md index b44aae54b..a6fcdca7f 100644 --- a/knowledge-base/grid-checkbox-column-prevent-click.md +++ b/knowledge-base/grid-checkbox-column-prevent-click.md @@ -3,9 +3,9 @@ title: Grid CheckBox column prevent accidental click description: How to prevent an accidental click in the CheckBox column of the Grid type: how-to page_title: Grid CheckBox column prevent accidental click -slug: grid-checkbox-column-prevent-click +slug: grid-kb-checkbox-column-prevent-click position: -tags: +tags: grid, checkbox, prevent, click, deselection res_type: kb --- @@ -29,21 +29,24 @@ An accidental click outside the checkbox in the Grid's CheckBox column deselects You can prevent the click event of the cells in the CheckBox column of the Grid with JavaScript. -1. Handle the `OnRowClick` event of the Grid and call a JavaScript function in it. +1. Call a JavaScript function in the `OnAfterRenderAsync` method and in the `OnRead` event handler of the Grid. 2. In the function, attach a `click` event handler to the `td` Html elements in the CheckBox column and prevent the event conditionally ```razor +@using Telerik.DataSource.Extensions; @inject IJSRuntime JS; - + SelectedItems="@SelectedRows"> - - @@ -52,7 +55,6 @@ You can prevent the click event of the cells in the CheckBox column of the Grid setTimeout(function() { preventDeselection() }, 300) - } function preventClickHandler(event) { @@ -70,39 +72,57 @@ You can prevent the click event of the cells in the CheckBox column of the Grid @code { - private List GridData { get; set; } = new(); + private List GridData { get; set; } - private IEnumerable SelectedEmployees { get; set; } = Enumerable.Empty(); + private IEnumerable SelectedRows { get; set; } = Enumerable.Empty(); - private string SelectedItemsChangedLog { get; set; } = string.Empty; + private string LastOnRead { get; set; } - private async Task OnRowClickHandler(GridRowClickEventArgs args) + protected override void OnInitialized() { - // call a function that will prevent deselection of already selected rows - await JS.InvokeVoidAsync("handleDeselection"); - } + GenerateData(); + SelectedRows = new List() { GridData.ElementAt(2) }; + base.OnInitialized(); + } - protected override void OnInitialized() + protected override async Task OnAfterRenderAsync(bool firstRender) { - for (int i = 1; i <= 15; i++) + if (firstRender) { - GridData.Add(new Employee() - { - EmployeeId = i, - Name = $"Employee {i}", - Team = $"Team {i % 3 + 1}" - }); + await Task.Delay(1); + await JS.InvokeVoidAsync("handleDeselection"); } + await base.OnAfterRenderAsync(firstRender); + } + + private async Task OnGridRead(GridReadEventArgs args) + { + var result = GridData.ToDataSourceResult(args.Request); + args.Data = result.Data; + args.Total = result.Total; - SelectedEmployees = new List() { GridData.ElementAt(2) }; + var now = DateTime.Now; + LastOnRead = now.ToLongTimeString() + "." + now.Millisecond; + + await Task.Delay(1); + await JS.InvokeVoidAsync("handleDeselection"); + } + + private void GenerateData() + { + GridData = new List(); + + for (int i = 1; i <= 100; i++) + { + GridData.Add(new SampleModel() { Id = i, Text = $"Item{i}" }); + } } - public class Employee + public class SampleModel { - public int EmployeeId { get; set; } - public string Name { get; set; } = string.Empty; - public string Team { get; set; } = string.Empty; + public int Id { get; set; } + public string Text { get; set; } } } ```