Skip to content

Commit a9dcde8

Browse files
chore(grid):added example project for saving state in wasm (#111)
* chore(grid):added example project for saving state in wasm * Update grid/save-state-in-wasm-through-controller/GridSavingStateInController/Client/Shared/TelerikLayout.razor Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com> * Update grid/save-state-in-wasm-through-controller/GridSavingStateInController/Client/Pages/Index.razor Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com> * chore(grid):changed service name * chore(grid):add fixes as per comment * chore(grid):added sample for saving the state with localstorage service * chore(grid):improved examples Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com>
1 parent 5164f1d commit a9dcde8

36 files changed

+1759
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31410.357
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GridSavingStateInController.Server", "GridSavingStateInController\Server\GridSavingStateInController.Server.csproj", "{6B6C895B-1933-481C-BBC0-8ABCAE652EFA}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GridSavingStateInController.Client", "GridSavingStateInController\Client\GridSavingStateInController.Client.csproj", "{A6ED9595-2334-4B2D-8E53-473A085F292E}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GridSavingStateInController.Shared", "GridSavingStateInController\Shared\GridSavingStateInController.Shared.csproj", "{5ECE9AE2-39A8-49FD-B636-843F55BAEB08}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{6B6C895B-1933-481C-BBC0-8ABCAE652EFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{6B6C895B-1933-481C-BBC0-8ABCAE652EFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{6B6C895B-1933-481C-BBC0-8ABCAE652EFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{6B6C895B-1933-481C-BBC0-8ABCAE652EFA}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{A6ED9595-2334-4B2D-8E53-473A085F292E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{A6ED9595-2334-4B2D-8E53-473A085F292E}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{A6ED9595-2334-4B2D-8E53-473A085F292E}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{A6ED9595-2334-4B2D-8E53-473A085F292E}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{5ECE9AE2-39A8-49FD-B636-843F55BAEB08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{5ECE9AE2-39A8-49FD-B636-843F55BAEB08}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{5ECE9AE2-39A8-49FD-B636-843F55BAEB08}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{5ECE9AE2-39A8-49FD-B636-843F55BAEB08}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {D46015CA-C2ED-4A6A-B9BC-E77B52B62A9D}
36+
EndGlobalSection
37+
EndGlobal
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Router AppAssembly="@typeof(Program).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
</Found>
5+
<NotFound>
6+
<LayoutView Layout="@typeof(MainLayout)">
7+
<p>Sorry, there's nothing at this address.</p>
8+
</LayoutView>
9+
</NotFound>
10+
</Router>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0" />
8+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0" PrivateAssets="all" />
9+
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\Shared\GridSavingStateInController.Shared.csproj" />
13+
</ItemGroup>
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
@page "/"
2+
@page "/save-state-controller"
3+
4+
@using GridSavingStateInController.Shared
5+
6+
@inject HttpClient Http
7+
@inject IJSRuntime JsInterop
8+
9+
<p><strong>Change something in the grid (like sort, filter, select, page, resize columns, etc.), then click "SaveState". After that, change again something and click "Apply last saved State".</strong></p>
10+
<p>
11+
<TelerikButton OnClick="@SaveState">Save State</TelerikButton>
12+
<TelerikButton OnClick="@ApplyState">Apply last saved State</TelerikButton>
13+
</p>
14+
15+
<TelerikGrid Data="@GridData" Height="500px" @ref="@Grid"
16+
Groupable="true"
17+
Pageable="true"
18+
Sortable="true"
19+
FilterMode="@GridFilterMode.FilterRow"
20+
Reorderable="true"
21+
Resizable="true"
22+
SelectionMode="GridSelectionMode.Multiple" @bind-SelectedItems="@SelectedItems"
23+
OnUpdate=@UpdateItem OnDelete=@DeleteItem OnCreate=@CreateItem EditMode="@GridEditMode.Inline">
24+
<GridColumns>
25+
<GridColumn Field="@(nameof(SampleData.Id))" Editable="false" />
26+
<GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
27+
<GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
28+
<GridCommandColumn>
29+
<GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
30+
<GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
31+
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Save</GridCommandButton>
32+
<GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
33+
</GridCommandColumn>
34+
</GridColumns>
35+
<GridToolBar>
36+
<GridCommandButton Command="Add" Icon="add">Add Employee</GridCommandButton>
37+
</GridToolBar>
38+
</TelerikGrid>
39+
40+
@if (SelectedItems != null)
41+
{
42+
<ul>
43+
@foreach (SampleData employee in SelectedItems)
44+
{
45+
<li>
46+
@employee.Id
47+
</li>
48+
}
49+
</ul>
50+
}
51+
52+
@code {
53+
TelerikGrid<SampleData> Grid { get; set; }
54+
IEnumerable<SampleData> SelectedItems { get; set; } = Enumerable.Empty<SampleData>();
55+
List<SampleData> GridData { get; set; }
56+
57+
async Task SaveState()
58+
{
59+
var state = await Http.PostAsJsonAsync("setstate", Grid.GetState());
60+
}
61+
62+
async Task ApplyState()
63+
{
64+
var state = await Http.GetFromJsonAsync<GridState<SampleData>>("getstate");
65+
66+
await Grid.SetState(state);
67+
}
68+
69+
async Task UpdateItem(GridCommandEventArgs args)
70+
{
71+
SampleData item = (SampleData)args.Item;
72+
73+
await GridCRUDService.Update(item);
74+
75+
await GetGridData();
76+
}
77+
78+
async Task DeleteItem(GridCommandEventArgs args)
79+
{
80+
SampleData item = (SampleData)args.Item;
81+
82+
await GridCRUDService.Delete(item);
83+
84+
await GetGridData();
85+
}
86+
87+
async Task CreateItem(GridCommandEventArgs args)
88+
{
89+
SampleData item = (SampleData)args.Item;
90+
91+
await GridCRUDService.Create(item);
92+
93+
await GetGridData();
94+
}
95+
96+
async Task GetGridData()
97+
{
98+
GridData = await GridCRUDService.Read();
99+
}
100+
101+
protected override async Task OnInitializedAsync()
102+
{
103+
await GetGridData();
104+
}
105+
106+
public static class GridCRUDService
107+
{
108+
private static List<SampleData> _data { get; set; } = new List<SampleData>();
109+
110+
public static async Task Create(SampleData itemToInsert)
111+
{
112+
itemToInsert.Id = _data.Count + 1;
113+
_data.Insert(0, itemToInsert);
114+
}
115+
116+
public static async Task<List<SampleData>> Read()
117+
{
118+
if (_data.Count < 1)
119+
{
120+
for (int i = 1; i < 50; i++)
121+
{
122+
_data.Add(new SampleData()
123+
{
124+
Id = i,
125+
Name = "name " + i,
126+
Team = "team " + i % 5
127+
});
128+
}
129+
}
130+
131+
return await Task.FromResult(_data);
132+
}
133+
134+
public static async Task Update(SampleData itemToUpdate)
135+
{
136+
var index = _data.FindIndex(i => i.Id == itemToUpdate.Id);
137+
if (index != -1)
138+
{
139+
_data[index] = itemToUpdate;
140+
}
141+
}
142+
143+
public static async Task Delete(SampleData itemToDelete)
144+
{
145+
_data.Remove(itemToDelete);
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)