Skip to content

Commit 76b3548

Browse files
docs(grid): improve incell editor template sample
1 parent 4495d7a commit 76b3548

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

components/grid/editing/incell.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
156156
*@
157157

158158
<TelerikGrid @ref="@Grid"
159-
Data=@MyData
160-
EditMode="@GridEditMode.Incell"
161-
Pageable="true"
162-
Height="500px"
163-
OnUpdate="@UpdateHandler" OnCreate="@CreateHandler"
164-
Navigable="true">
159+
Data=@MyData
160+
EditMode="@GridEditMode.Incell"
161+
Pageable="true"
162+
Height="500px"
163+
OnUpdate="@UpdateHandler" OnCreate="@CreateHandler"
164+
Navigable="true">
165165
<GridColumns>
166166
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="ID" />
167167
<GridColumn Field=@nameof(SampleData.Name) Title="Name">
@@ -176,8 +176,8 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
176176
<EditorTemplate>
177177
@{
178178
currentItem = context as SampleData;
179-
<TelerikNumericTextBox @bind-Value=@currentItem.Ranking OnChange="@CloseEditor"
180-
Width="100%" Max="10" Min="0" Step="1">
179+
<TelerikNumericTextBox @bind-Value=@currentItem.Ranking OnChange="@CloseEditor"
180+
Width="100%" Max="10" Min="0" Step="1">
181181
</TelerikNumericTextBox>
182182
}
183183
</EditorTemplate>
@@ -187,13 +187,13 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
187187
@{
188188
currentItem = context as SampleData;
189189
<TelerikDropDownList Data="@Roles" @bind-Value="@currentItem.Role" OnChange="@CloseEditor"
190-
Width="120px" PopupHeight="auto">
190+
Width="120px" PopupHeight="auto">
191191
</TelerikDropDownList>
192192
}
193193
</EditorTemplate>
194194
</GridColumn>
195195
<GridCommandColumn>
196-
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
196+
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Save</GridCommandButton>
197197
</GridCommandColumn>
198198
</GridColumns>
199199
<GridToolBar>
@@ -212,7 +212,7 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
212212

213213
if (currentItem.ID == 0 && state.InsertedItem != null)
214214
{
215-
//insert operation - the item is new
215+
// insert operation - the item is new
216216
await CreateHandler(new GridCommandEventArgs()
217217
{
218218
Item = state.InsertedItem
@@ -221,8 +221,7 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
221221
else
222222
if (currentItem.ID > 0 && state.EditItem != null)
223223
{
224-
//edit operation on an existing item
225-
224+
// edit operation on an existing item
226225
await UpdateHandler(new GridCommandEventArgs()
227226
{
228227
Item = state.EditItem,
@@ -232,6 +231,8 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
232231

233232
state.InsertedItem = state.OriginalEditItem = state.EditItem = default;
234233

234+
await Task.Delay(20); // let the grid re-render and close the cell if keyboard navigation is enabled
235+
235236
await Grid?.SetState(state);
236237
}
237238

@@ -244,10 +245,17 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
244245
var index = MyData.FindIndex(i => i.ID == item.ID);
245246
if (index != -1)
246247
{
247-
MyData[index] = item;
248+
// with keyboard navigation and Enter key press in the component
249+
// both the grid, and the OnChange handler will raise the update event
250+
// you may want to add an equality comparison for the item to only call the database once
251+
// when the item has changed, not both times
252+
if (!MyData[index].Equals(item))
253+
{
254+
MyData[index] = item;
255+
Console.WriteLine("update");
256+
//perform actual data source operations here
257+
}
248258
}
249-
250-
//perform actual data source operations here
251259
}
252260

253261
async Task CreateHandler(GridCommandEventArgs args)
@@ -257,6 +265,7 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
257265
item.ID = MyData.Count + 1;
258266
MyData.Insert(0, item);
259267

268+
Console.WriteLine("create");
260269
// perform actual data source operation here through your service
261270
}
262271

@@ -283,6 +292,16 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
283292
public string Name { get; set; }
284293
public string Role { get; set; }
285294
public int Ranking { get; set; }
295+
296+
public override bool Equals(object obj)
297+
{
298+
if (obj != null && obj is SampleData)
299+
{
300+
SampleData curr = obj as SampleData;
301+
return (ID == curr.ID) && (Name == curr.Name) && (Role == curr.Role) && (Ranking == curr.Ranking);
302+
}
303+
return false;
304+
}
286305
}
287306

288307
List<SampleData> MyData { get; set; }

0 commit comments

Comments
 (0)