Skip to content

Commit 4495d7a

Browse files
docs(grid): incell editing and editor templates - when to close
1 parent 5c9396c commit 4495d7a

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

components/grid/editing/incell.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,154 @@ Click a cell, edit it and click outside of the cell to see the change.<br />
142142

143143
* 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.
144144

145+
* When using an [editor template]({%slug components/grid/features/templates%}#edit-template), the grid cannot always know what the custom editor needs to do, and when it needs to close the cell and update the data, because this is up to the editor. Thus, you can use the grid [state]({%slug grid-state%}) to close the cell and invoke the desired operations on the data according to your business logic. For example, a suitable event the Telerik input components provide is `OnChange`.
146+
* When keyboard navigation is enabled in the grid (`Navigable=true`), the grid will capture `Enter` keypresses when the cell is focused, and will close the cell with the corresponding update. You can either use that (e.g., a simple input will let the keypress event propagate to the grid cell), or you can prevent the event propagation and use only your business logic.
147+
148+
The example below shows how you can use both a navigable grid and events on the custom editor templates to close the cells when Enter is pressed or when they lose focus, much like an Excel spreadsheet behaves.
149+
150+
**.razor**
151+
152+
@* The Telerik-specific OnChange event is used to achieve this in this example.
153+
You can implement a similar event in your components/editors, or use a completely different event and logic to instruct the grid to update a cell. In this example, the two-way binding
154+
provides the editor value to the model immediately, so it becomes avaialable in the OnChange
155+
handler. In your case you might need to handle this differently, depending on the logic you need.
156+
*@
157+
158+
<TelerikGrid @ref="@Grid"
159+
Data=@MyData
160+
EditMode="@GridEditMode.Incell"
161+
Pageable="true"
162+
Height="500px"
163+
OnUpdate="@UpdateHandler" OnCreate="@CreateHandler"
164+
Navigable="true">
165+
<GridColumns>
166+
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="ID" />
167+
<GridColumn Field=@nameof(SampleData.Name) Title="Name">
168+
<EditorTemplate>
169+
@{
170+
currentItem = context as SampleData;
171+
<TelerikTextBox @bind-Value=@currentItem.Name OnChange="@CloseEditor" Width="100%" />
172+
}
173+
</EditorTemplate>
174+
</GridColumn>
175+
<GridColumn Field=@nameof(SampleData.Ranking) Title="Ranking" Width="120px">
176+
<EditorTemplate>
177+
@{
178+
currentItem = context as SampleData;
179+
<TelerikNumericTextBox @bind-Value=@currentItem.Ranking OnChange="@CloseEditor"
180+
Width="100%" Max="10" Min="0" Step="1">
181+
</TelerikNumericTextBox>
182+
}
183+
</EditorTemplate>
184+
</GridColumn>
185+
<GridColumn Field=@nameof(SampleData.Role) Title="Position" Width="200px">
186+
<EditorTemplate>
187+
@{
188+
currentItem = context as SampleData;
189+
<TelerikDropDownList Data="@Roles" @bind-Value="@currentItem.Role" OnChange="@CloseEditor"
190+
Width="120px" PopupHeight="auto">
191+
</TelerikDropDownList>
192+
}
193+
</EditorTemplate>
194+
</GridColumn>
195+
<GridCommandColumn>
196+
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
197+
</GridCommandColumn>
198+
</GridColumns>
199+
<GridToolBar>
200+
<GridCommandButton Command="Add" Icon="add">Add Employee</GridCommandButton>
201+
</GridToolBar>
202+
</TelerikGrid>
203+
204+
@code {
205+
// handling the custom editor template for InCell editing
206+
public TelerikGrid<SampleData> Grid { get; set; }
207+
public SampleData currentItem { get; set; }
208+
209+
async Task CloseEditor()
210+
{
211+
var state = Grid?.GetState();
212+
213+
if (currentItem.ID == 0 && state.InsertedItem != null)
214+
{
215+
//insert operation - the item is new
216+
await CreateHandler(new GridCommandEventArgs()
217+
{
218+
Item = state.InsertedItem
219+
});
220+
}
221+
else
222+
if (currentItem.ID > 0 && state.EditItem != null)
223+
{
224+
//edit operation on an existing item
225+
226+
await UpdateHandler(new GridCommandEventArgs()
227+
{
228+
Item = state.EditItem,
229+
Field = state.EditField
230+
});
231+
}
232+
233+
state.InsertedItem = state.OriginalEditItem = state.EditItem = default;
234+
235+
await Grid?.SetState(state);
236+
}
237+
238+
//Create and Update operations
239+
240+
async Task UpdateHandler(GridCommandEventArgs args)
241+
{
242+
SampleData item = (SampleData)args.Item;
243+
244+
var index = MyData.FindIndex(i => i.ID == item.ID);
245+
if (index != -1)
246+
{
247+
MyData[index] = item;
248+
}
249+
250+
//perform actual data source operations here
251+
}
252+
253+
async Task CreateHandler(GridCommandEventArgs args)
254+
{
255+
SampleData item = (SampleData)args.Item;
256+
257+
item.ID = MyData.Count + 1;
258+
MyData.Insert(0, item);
259+
260+
// perform actual data source operation here through your service
261+
}
262+
263+
// data sources
264+
265+
protected override void OnInitialized()
266+
{
267+
MyData = new List<SampleData>();
268+
269+
for (int i = 1; i < 50; i++)
270+
{
271+
MyData.Add(new SampleData()
272+
{
273+
ID = i,
274+
Name = "name " + i,
275+
Ranking = i % 10
276+
});
277+
}
278+
}
279+
280+
public class SampleData
281+
{
282+
public int ID { get; set; }
283+
public string Name { get; set; }
284+
public string Role { get; set; }
285+
public int Ranking { get; set; }
286+
}
287+
288+
List<SampleData> MyData { get; set; }
289+
static List<string> Roles = new List<string> { "Manager", "Employee", "Contractor" };
290+
}
291+
292+
145293
## See Also
146294

147295
* [Live Demo: Grid InCell Editing](https://demos.telerik.com/blazor-ui/grid/editing-incell)

0 commit comments

Comments
 (0)