Skip to content

Commit 3efb3ef

Browse files
Better CRUD service mimic classes and usage (#164)
* chore(grid): better CRUD service mimic on editign overview page * chore(grid): inline editing better crud mimic * chore(grid): popup editing better crud mimic * chore(grid): incell editing better crud mimic * chore(grid): autogetn columns editing better crud mimic * chore(grid): insertion to generate ID * chore(grid): command column editing better crud mimic * chore(grid): state dics editing better crud mimic * chore(grid): editor template better crud mimic * chore(grid): toolbar better CRUD mimic * chore(listview): better CRUD mimic * chore(scheduler): better CRUD service mimic * docs(grid): clarify why async Task and not async void * chore(treelist): autogen columns better CRUD mimic * chore(treeList): command column better CRUD mimic * chore(treeList): incell edit better CRUD mimic * chore(treeList): inline edit better CRUD mimic * chore(treeList): popup editing better CRUD mimic * chore(treeList): editing overview better CRUD mimic * docs(treelist): improvements on editing notes * chore(treeList): state sampel better CRUD mimic * chore(treeList): editor template better CRUD mimic
1 parent b82e832 commit 3efb3ef

File tree

21 files changed

+1744
-1813
lines changed

21 files changed

+1744
-1813
lines changed

components/grid/columns/auto-generated.md

Lines changed: 76 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -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

components/grid/columns/command.md

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ The following code example demonstrates declarations and handling.
9999
public DateTime HireDate { get; set; }
100100
}
101101
102-
List<SampleData> GridData = Enumerable.Range(1, 50).Select(x => new SampleData
103-
{
104-
ID = x,
105-
Name = "name " + x,
106-
HireDate = DateTime.Now.AddDays(-x)
107-
}).ToList();
102+
List<SampleData> GridData { get; set; }
108103
109104
// sample custom commands handling
110105
@@ -129,36 +124,61 @@ The following code example demonstrates declarations and handling.
129124
130125
}
131126
132-
// sample CUD operations
127+
// sample CRUD operations
133128
134129
private async Task MyUpdateHandler(GridCommandEventArgs args)
135130
{
136131
SampleData theUpdatedItem = args.Item as SampleData;
137-
SampleData updatedItem = await ServiceMimicUpdate(theUpdatedItem);
138132
139-
// update the local view-model data
140-
var index = GridData.FindIndex(i => i.ID == updatedItem.ID);
141-
if (index != -1)
142-
{
143-
GridData[index] = updatedItem;
144-
}
133+
// perform actual data source operations here through your service
134+
await MyService.Update(theUpdatedItem);
135+
136+
// update the local view-model data with the service data
137+
await GetGridData();
138+
}
139+
140+
async Task GetGridData()
141+
{
142+
GridData = await MyService.Read();
145143
}
146144
147-
// the following method mimics an actual data service that handles the actual data source
148-
// you can see about implement error and exception handling, determining suitable return types as per your needs
149-
// an example is available here: https://github.com/telerik/blazor-ui/tree/master/grid/remote-validation
145+
protected override async Task OnInitializedAsync()
146+
{
147+
await GetGridData();
148+
}
150149
151-
async Task<SampleData> ServiceMimicUpdate(SampleData itemToUpdate)
150+
// the following static class mimics an actual data service that handles the actual data source
151+
// replace it with your actual service through the DI, this only mimics how the API can look like and works for this standalone page
152+
public static class MyService
152153
{
153-
// in this example, we just populate the fields, you project may use
154-
// something else or generate the updated item differently
155-
SampleData updatedItem = new SampleData()
154+
private static List<SampleData> _data { get; set; } = new List<SampleData>();
155+
156+
public static async Task<List<SampleData>> Read()
156157
{
157-
ID = itemToUpdate.ID,
158-
HireDate = itemToUpdate.HireDate,
159-
Name = itemToUpdate.Name
160-
};
161-
return await Task.FromResult(updatedItem);
158+
if (_data.Count < 1)
159+
{
160+
for (int i = 1; i < 50; i++)
161+
{
162+
_data.Add(new SampleData()
163+
{
164+
ID = i,
165+
Name = "name " + i,
166+
HireDate = DateTime.Now.AddDays(-i)
167+
});
168+
}
169+
}
170+
171+
return await Task.FromResult(_data);
172+
}
173+
174+
public static async Task Update(SampleData itemToUpdate)
175+
{
176+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
177+
if (index != -1)
178+
{
179+
_data[index] = itemToUpdate;
180+
}
181+
}
162182
}
163183
}
164184
````

0 commit comments

Comments
 (0)