Skip to content

Commit 9ebb34f

Browse files
IvanDanchevIvanDanchev
authored andcommitted
kb(Grid): add PR review suggestions
1 parent a7e7e9e commit 9ebb34f

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

knowledge-base/grid-checkbox-column-prevent-click.md

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: Grid CheckBox column prevent accidental click
33
description: How to prevent an accidental click in the CheckBox column of the Grid
44
type: how-to
55
page_title: Grid CheckBox column prevent accidental click
6-
slug: grid-checkbox-column-prevent-click
6+
slug: grid-kb-checkbox-column-prevent-click
77
position:
8-
tags:
8+
tags: grid, checkbox, prevent, click, deselection
99
res_type: kb
1010
---
1111

@@ -29,21 +29,24 @@ An accidental click outside the checkbox in the Grid's CheckBox column deselects
2929

3030
You can prevent the click event of the cells in the CheckBox column of the Grid with JavaScript.
3131

32-
1. Handle the `OnRowClick` event of the Grid and call a JavaScript function in it.
32+
1. Call a JavaScript function in the `OnAfterRenderAsync` method and in the `OnRead` event handler of the Grid.
3333
2. In the function, attach a `click` event handler to the `td` Html elements in the CheckBox column and prevent the event conditionally
3434

3535
```razor
36+
@using Telerik.DataSource.Extensions;
3637
@inject IJSRuntime JS;
3738
38-
<TelerikGrid Data="@GridData"
39+
<TelerikGrid TItem="@SampleModel"
40+
OnRead="@OnGridRead"
41+
AutoGenerateColumns="true"
42+
Sortable="true"
43+
Pageable="true"
44+
FilterMode="@GridFilterMode.FilterRow"
45+
Height="400px"
3946
SelectionMode="@GridSelectionMode.Multiple"
40-
SelectedItems="@SelectedEmployees"
41-
OnRowClick="@OnRowClickHandler"
42-
Pageable="true">
47+
SelectedItems="@SelectedRows">
4348
<GridColumns>
4449
<GridCheckboxColumn SelectAll="true" CheckBoxOnlySelection="false" />
45-
<GridColumn Field="@nameof(Employee.Name)" />
46-
<GridColumn Field="@nameof(Employee.Team)" />
4750
</GridColumns>
4851
</TelerikGrid>
4952
@@ -52,7 +55,6 @@ You can prevent the click event of the cells in the CheckBox column of the Grid
5255
setTimeout(function() {
5356
preventDeselection()
5457
}, 300)
55-
5658
}
5759
5860
function preventClickHandler(event) {
@@ -70,39 +72,57 @@ You can prevent the click event of the cells in the CheckBox column of the Grid
7072
</script>
7173
7274
@code {
73-
private List<Employee> GridData { get; set; } = new();
75+
private List<SampleModel> GridData { get; set; }
7476
75-
private IEnumerable<Employee> SelectedEmployees { get; set; } = Enumerable.Empty<Employee>();
77+
private IEnumerable<SampleModel> SelectedRows { get; set; } = Enumerable.Empty<SampleModel>();
7678
77-
private string SelectedItemsChangedLog { get; set; } = string.Empty;
79+
private string LastOnRead { get; set; }
7880
79-
private async Task OnRowClickHandler(GridRowClickEventArgs args)
81+
protected override void OnInitialized()
8082
{
81-
// call a function that will prevent deselection of already selected rows
82-
await JS.InvokeVoidAsync("handleDeselection");
83-
}
83+
GenerateData();
84+
SelectedRows = new List<SampleModel>() { GridData.ElementAt(2) };
8485
86+
base.OnInitialized();
87+
}
8588
86-
protected override void OnInitialized()
89+
protected override async Task OnAfterRenderAsync(bool firstRender)
8790
{
88-
for (int i = 1; i <= 15; i++)
91+
if (firstRender)
8992
{
90-
GridData.Add(new Employee()
91-
{
92-
EmployeeId = i,
93-
Name = $"Employee {i}",
94-
Team = $"Team {i % 3 + 1}"
95-
});
93+
await Task.Delay(1);
94+
await JS.InvokeVoidAsync("handleDeselection");
9695
}
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;
97104
98-
SelectedEmployees = new List<Employee>() { GridData.ElementAt(2) };
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+
}
99120
}
100121
101-
public class Employee
122+
public class SampleModel
102123
{
103-
public int EmployeeId { get; set; }
104-
public string Name { get; set; } = string.Empty;
105-
public string Team { get; set; } = string.Empty;
124+
public int Id { get; set; }
125+
public string Text { get; set; }
106126
}
107127
}
108128
```

0 commit comments

Comments
 (0)