@@ -199,33 +199,30 @@ This example shows how to:
199199@using System.Collections.ObjectModel
200200
201201<div style="max-width: 800px;">
202- <EditForm Model="@DataModel">
203- <DataAnnotationsValidator />
204- <ValidationSummary />
205- <TelerikGrid Data="@GridData"
206- AutoGenerateColumns="true"
207- EditMode="GridEditMode.Popup"
208- Pageable="true"
209- SelectionMode="GridSelectionMode.Multiple"
210- @bind-SelectedItems="@SelectedUsers"
211- PageSize="@PageSize"
212- OnCreate="@CreateItem"
213- OnUpdate="@UpdateItem"
214- OnDelete="@DeleteItem">
215- <GridToolBar>
216- <GridCommandButton Command="Add" Icon="add">Add</GridCommandButton>
217- </GridToolBar>
218- <GridColumns>
219- <GridCheckboxColumn />
220- <GridAutoGeneratedColumns ColumnWidth="200px" />
221- <GridColumn Field="@nameof(GridDataModel.RegistrationDate)" Title="Registration Date" Width="200px" />
222- <GridCommandColumn Width="250px">
223- <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
224- <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
225- </GridCommandColumn>
226- </GridColumns>
227- </TelerikGrid>
228- </EditForm>
202+
203+ <TelerikGrid Data="@GridData"
204+ AutoGenerateColumns="true"
205+ EditMode="GridEditMode.Popup"
206+ Pageable="true"
207+ SelectionMode="GridSelectionMode.Multiple"
208+ @bind-SelectedItems="@SelectedUsers"
209+ PageSize="@PageSize"
210+ OnCreate="@CreateItem"
211+ OnUpdate="@UpdateItem"
212+ OnDelete="@DeleteItem">
213+ <GridToolBar>
214+ <GridCommandButton Command="Add" Icon="add">Add</GridCommandButton>
215+ </GridToolBar>
216+ <GridColumns>
217+ <GridCheckboxColumn />
218+ <GridAutoGeneratedColumns ColumnWidth="200px" />
219+ <GridColumn Field="@nameof(GridDataModel.RegistrationDate)" Title="Registration Date" Width="200px" />
220+ <GridCommandColumn Width="250px">
221+ <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
222+ <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
223+ </GridCommandColumn>
224+ </GridColumns>
225+ </TelerikGrid>
229226
230227
231228 @if (SelectedUsers.Any())
@@ -253,7 +250,6 @@ This example shows how to:
253250 public List<GridDataModel> GridData { get; set; }
254251 public IEnumerable<GridDataModel> SelectedUsers { get; set; } = new ObservableCollection<GridDataModel>();
255252 public int PageSize { get; set; } = 3;
256- GridDataModel DataModel = new GridDataModel();
257253
258254 #region data model with annotations
259255 public class GridDataModel
@@ -290,106 +286,92 @@ This example shows how to:
290286 GridDataModel item = (GridDataModel)args.Item;
291287
292288 // perform actual data source operations here through your service
293- GridDataModel updatedItem = await ServiceMimicUpdate (item);
289+ await MyService.Update (item);
294290
295- // update the local view-model data
296- var index = GridData.FindIndex(i => i.Id == updatedItem.Id);
297- if (index != -1)
298- {
299- GridData[index] = updatedItem;
300- }
291+ // update the local view-model data with the service data
292+ await GetGridData();
301293 }
302294
303295 async Task DeleteItem(GridCommandEventArgs args)
304296 {
305297 GridDataModel item = (GridDataModel)args.Item;
306298
307299 // perform actual data source operation here through your service
308- bool isDeleted = await ServiceMimicDelete (item);
300+ await MyService.Delete (item);
309301
310- if (isDeleted)
311- {
312- // update the local view-model data
313- GridData.Remove(item);
314- }
302+ // update the local view-model data with the service data
303+ await GetGridData();
315304 }
316305
317306 async Task CreateItem(GridCommandEventArgs args)
318307 {
319308 GridDataModel item = (GridDataModel)args.Item;
320309
321310 // perform actual data source operation here through your service
322- GridDataModel insertedItem = await ServiceMimicInsert (item);
311+ await MyService.Create (item);
323312
324- // update the local view-model data
325- GridData.Insert(0, insertedItem );
313+ // update the local view-model data with the service data
314+ await GetGridData( );
326315 }
327316
317+ #endregion
328318
329- // the following three methods mimic an actual data service that handles the actual data source
330- // you can see about implement error and exception handling, determining suitable return types as per your needs
331- // an example is available here: https://github.com/telerik/blazor-ui/tree/master/grid/remote-validation
332-
333- async Task<GridDataModel> ServiceMimicInsert(GridDataModel itemToInsert)
319+ async Task GetGridData()
334320 {
335- // in this example, we just populate the fields, you project may use
336- // something else or generate the updated item differently
337- GridDataModel updatedItem = new GridDataModel()
338- {
339- // the service assigns an ID, in this sample we use only the view-model data for simplicity,
340- // you should use the actual data and set the properties as necessary (e.g., generate nested fields data and so on)
341- Id = GridData.Count + 1,
342- Username = itemToInsert.Username,
343- EmailAddress = itemToInsert.EmailAddress,
344- RegistrationDate = itemToInsert.RegistrationDate,
345- LocalTime = itemToInsert.LocalTime,
346- BoughtBooks = itemToInsert.BoughtBooks
347- };
348- return await Task.FromResult(updatedItem);
321+ GridData = await MyService.Read();
349322 }
350323
351- async Task<GridDataModel> ServiceMimicUpdate(GridDataModel itemToUpdate )
324+ protected override async Task OnInitializedAsync( )
352325 {
353- // in this example, we just populate the fields, you project may use
354- // something else or generate the updated item differently
355- GridDataModel updatedItem = new GridDataModel()
356- {
357- Id = itemToUpdate.Id,
358- Username = itemToUpdate.Username,
359- EmailAddress = itemToUpdate.EmailAddress,
360- RegistrationDate = itemToUpdate.RegistrationDate,
361- LocalTime = itemToUpdate.LocalTime,
362- BoughtBooks = itemToUpdate.BoughtBooks
363- };
364- return await Task.FromResult(updatedItem);
326+ await GetGridData();
365327 }
366328
367- async Task<bool> ServiceMimicDelete(GridDataModel itemToDelete)
329+ // the following static class mimics an actual data service that handles the actual data source
330+ // replace it with your actual service through the DI, this only mimics how the API can look like and works for this standalone page
331+ public static class MyService
368332 {
369- return await Task.FromResult(true);//always successful
370- }
333+ private static List<GridDataModel> _data { get; set; } = new List<GridDataModel>();
371334
372- #endregion
335+ public static async Task Create(GridDataModel itemToInsert)
336+ {
337+ itemToInsert.Id = _data.Count + 1;
338+ _data.Insert(0, itemToInsert);
339+ }
373340
374- #region data generation
375- protected override void OnInitialized()
376- {
377- GridData = new List<GridDataModel>();
378- for (int i = 0; i < 45; i++)
341+ public static async Task<List<GridDataModel>> Read()
342+ {
343+ if (_data.Count < 1)
344+ {
345+ for (int i = 0; i < 45; i++)
346+ {
347+ _data.Add(new GridDataModel()
348+ {
349+ Id = i,
350+ Username = $"Username {i}",
351+ EmailAddress = $"user{i}@mail.com",
352+ RegistrationDate = DateTime.Now.AddDays(-2),
353+ LocalTime = DateTime.Now
354+ });
355+ }
356+ }
357+
358+ return await Task.FromResult(_data);
359+ }
360+
361+ public static async Task Update(GridDataModel itemToUpdate)
379362 {
380- GridData.Add(new GridDataModel()
363+ var index = _data.FindIndex(i => i.Id == itemToUpdate.Id);
364+ if (index != -1)
381365 {
382- Id = i,
383- Username = $"Username {i}",
384- EmailAddress = $"user{i}@mail.com",
385- RegistrationDate = DateTime.Now.AddDays(-2),
386- LocalTime = DateTime.Now
387- });
366+ _data[index] = itemToUpdate;
367+ }
388368 }
389369
390- base.OnInitialized();
370+ public static async Task Delete(GridDataModel itemToDelete)
371+ {
372+ _data.Remove(itemToDelete);
373+ }
391374 }
392- #endregion
393375}
394376````
395377> caption The result from the code snippet above
0 commit comments