Skip to content

Commit f32cb31

Browse files
Row selection in edit mode - Knowledge base article (#60)
* knowledge-base(grid): Row Selection in Edit Mode * knowledge-base(grid): Minor changes to article * chore(kb): fix grid select on incell edit kb * chore(docs): Updated Grid Selection Overview article * chore(docs): updated grid incell editing article * chore(docs): fixed typo in Grid Editing Overview article * chore(grid): fix wording Co-authored-by: Marin Bratanov <[email protected]>
1 parent 17dfaca commit f32cb31

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

components/grid/editing/incell.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
137137
## Notes
138138

139139
* When the InCell Edit Mode is enabled and you want to enable item selection a `<GridCheckboxColumn />` must be added to the `<Columns>` collection. More information on that can be read in the [Selection]({%slug components/grid/selection/overview%}#notes) article.
140+
141+
* To see how to select the row that is being edited in InCell edit mode without using a `<GridCheckboxColumn />` check out the [Row Selection in Edit with InCell EditMode]({%slug grid-kb-row-select-incell-edit%}) Knowledge Base article.
142+
140143
* It is up to the data access logic to save the data once it is changed in the data collection. The example above showcases when that happens and adds some code to provide a visual indication of the change. In a real application, the code for handling data updates may be entirely different.
141144

142145
## See Also

components/grid/editing/overview.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can initiate editing or inserting of an item from anywhere on the page (butt
4545

4646
The example below shows how you can handle the events the grid exposes, so you can Create, Update or Delete records in your data source and the view model.
4747

48-
>tip The grid events use `EventCallback` and can be syncrhonous or asynchronous. The example below shows async versions, and the signature for synchronous events is `void <MethodName>(GridCommandEventArgs args)`.
48+
>tip The grid events use `EventCallback` and can be synchronous or asynchronous. The example below shows async versions, and the signature for synchronous events is `void <MethodName>(GridCommandEventArgs args)`.
4949
5050
>caption Handling the CRUD events of the grid to save data to the actual data source
5151
@@ -201,4 +201,3 @@ There are a few considerations to keep in mind with the CUD operations of the gr
201201
* [Live Demo: Grid Custom Editor Template](https://demos.telerik.com/blazor-ui/grid/custom-editor)
202202
* [Live Demo: Grid Custom Edit Form](https://demos.telerik.com/blazor-ui/grid/editing-custom-form)
203203
* [Batch Editing Example](https://github.com/telerik/blazor-ui/tree/master/grid/batch-editing)
204-

components/grid/selection/overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ See how the row selection modes work
9595

9696
In the [Incell EditMode]({%slug components/grid/editing/incell%}) selection can be applied only via a checkbox column (`<GridCheckboxColumn />`). This is required due to the overlapping action that triggers selection and InCell editing (clicking in the row) - if row click selection was enabled with InCell editing, each attempt to select a row would put a cell in edit mode; and each attempt to edit a cell would select a new row. Such user experience is confusing, and so selection will only work through the row selection checkbox.
9797

98+
To see how to select the row that is being edited in InCell edit mode without using a `<GridCheckboxColumn />` check out the [Row Selection in Edit with InCell EditMode]({%slug grid-kb-row-select-incell-edit%}) Knowledge Base article.
99+
98100
#### Inline and PopUp Edit Modes
99101

100102
In [Inline EditMode]({%slug components/grid/editing/inline%}) and [PopUp EditMode]({%slug components/grid/editing/popup%}) selection can be done by clicking on the desired row or by using a `< GridCheckboxColumn />`.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: Row Selection in Edit with InCell EditMode
3+
description: How to Select a row that is being edited in InCell editing mode
4+
type: how-to
5+
page_title: Row Selection in Edit with InCell EditMode
6+
slug: grid-kb-row-select-incell-edit
7+
position:
8+
tags:
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>Grid for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
## Description
23+
24+
When using the [InCell]({%slug components/grid/editing/incell%}) Editing Mode, I want the row that is currently edited to be selected. I want the user to get the current row selected when they edit it.
25+
26+
By default, the click action opens a cell for editing and does not select a row to avoid an ambiguous action, and so rows can only be selected with the dedicated grid selection column.
27+
28+
29+
## Solution
30+
31+
Use the `OnEdit` and `OnUpdate` [Grid events]({%slug grid-events%}#cud-events):
32+
* In the handler for the `OnEdit` event add the currently edited item, passed to the method through the object of type `GridCommandEventArgs`, into the `SelectedItems` collection.
33+
* The item added to the collection is with the old value, before the editing.
34+
* In the handler for the `OnUpdate` event, update the `SelectedItems` collection with the new value of the edited item to ensure data integrity.
35+
36+
>caption How to Select the row that is being edited in InCell edit mode
37+
38+
````CSHTML
39+
@* You can create your own extension method to add an item into IEnumerable collection without the usage of a mediator one. *@
40+
41+
<TelerikGrid Data="@GridData"
42+
Height="400px"
43+
SelectionMode="GridSelectionMode.Multiple"
44+
@bind-SelectedItems="@SelectedItems"
45+
EditMode="GridEditMode.Incell"
46+
OnUpdate="@UpdateItem"
47+
OnEdit="@EditHandler">
48+
<GridColumns>
49+
<GridCheckboxColumn SelectAllMode="@GridSelectAllMode.All"></GridCheckboxColumn>
50+
<GridColumn Field=@nameof(Product.ProductId) Title="Product Id" Editable="false" />
51+
<GridColumn Field=@nameof(Product.ProductName) Title="Product Name" />
52+
<GridColumn Field=@nameof(Product.SupplierId) Title="Supplier Id" />
53+
<GridColumn Field=@nameof(Product.UnitPrice) Title="Unit Price" />
54+
<GridColumn Field=@nameof(Product.UnitsInStock) Title="Units in stock" />
55+
</GridColumns>
56+
</TelerikGrid>
57+
58+
@if (SelectedItems.Any())
59+
{
60+
<ul>
61+
@foreach (Product item in SelectedItems)
62+
{
63+
<li>@item.ProductName</li>
64+
}
65+
</ul>
66+
}
67+
68+
69+
@code {
70+
public void EditHandler(GridCommandEventArgs args)
71+
{
72+
var item = args.Item as Product;
73+
var foundItem = SelectedItems.Where(x => x.ProductId == item.ProductId).FirstOrDefault();
74+
75+
if (foundItem == null)
76+
{
77+
// add the currently edited row to the selected items
78+
var selItemsList = SelectedItems.ToList();
79+
selItemsList.Add(item);
80+
SelectedItems = new List<Product>(selItemsList);
81+
}
82+
}
83+
84+
public void UpdateItem(GridCommandEventArgs args)
85+
{
86+
var item = args.Item as Product;
87+
int index = GridData.FindIndex(x => item.ProductId == x.ProductId);
88+
var currentSelectedItems = new List<Product>(SelectedItems);
89+
int selectedItemIndex = currentSelectedItems.FindIndex(x => x.ProductId == item.ProductId);
90+
91+
if (index != -1)
92+
{
93+
// update the selected items collection
94+
currentSelectedItems[selectedItemIndex] = item;
95+
SelectedItems = currentSelectedItems;
96+
97+
// The actual Update operation for the view-model data. Add your actual data source operations here
98+
GridData[index] = item;
99+
}
100+
}
101+
102+
public List<Product> GridData { get; set; }
103+
public IEnumerable<Product> SelectedItems { get; set; } = new List<Product>();
104+
105+
protected override void OnInitialized()
106+
{
107+
GridData = GenerateProducts();
108+
}
109+
110+
private List<Product> GenerateProducts()
111+
{
112+
List<Product> products = new List<Product>();
113+
114+
for (int i = 1; i <= 100; i++)
115+
{
116+
var product = new Product()
117+
{
118+
ProductId = i,
119+
ProductName = "Product " + i.ToString(),
120+
SupplierId = i,
121+
UnitPrice = (decimal)(i * 3.14),
122+
UnitsInStock = (short)(i * 1),
123+
};
124+
products.Add(product);
125+
}
126+
return products;
127+
}
128+
129+
public class Product
130+
{
131+
public int ProductId { get; set; }
132+
public string ProductName { get; set; }
133+
public int SupplierId { get; set; }
134+
public decimal UnitPrice { get; set; }
135+
public short UnitsInStock { get; set; }
136+
}
137+
}
138+
````

0 commit comments

Comments
 (0)