Skip to content

Commit fcad6fc

Browse files
1011226: Need to create sample for blazor datagrid integration with MariaDB
1 parent eda8041 commit fcad6fc

File tree

64 files changed

+60759
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+60759
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<base href="/" />
8+
<link rel="stylesheet" href="@Assets["lib/bootstrap/dist/css/bootstrap.min.css"]" />
9+
<link rel="stylesheet" href="@Assets["app.css"]" />
10+
<link rel="stylesheet" href="@Assets["Grid_MariaDB.styles.css"]" />
11+
<link rel="stylesheet" href="/grid-column-template.css" />
12+
<link href="_content/Syncfusion.Blazor.Themes/tailwind3.css" rel="stylesheet" />
13+
<ImportMap />
14+
<link rel="icon" type="image/png" href="favicon.png" />
15+
<HeadOutlet />
16+
</head>
17+
18+
<body>
19+
<Routes />
20+
<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
21+
<script src="_framework/blazor.web.js"></script>
22+
</body>
23+
24+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@inherits LayoutComponentBase
2+
3+
<div class="page">
4+
<main>
5+
<article class="content px-4">
6+
@Body
7+
</article>
8+
</main>
9+
</div>
10+
11+
<div id="blazor-error-ui" data-nosnippet>
12+
An unhandled error has occurred.
13+
<a href="." class="reload">Reload</a>
14+
<span class="dismiss">🗙</span>
15+
</div>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row ::deep .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
text-decoration: none;
28+
}
29+
30+
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
31+
text-decoration: underline;
32+
}
33+
34+
.top-row ::deep a:first-child {
35+
overflow: hidden;
36+
text-overflow: ellipsis;
37+
}
38+
39+
@media (max-width: 640.98px) {
40+
.top-row {
41+
justify-content: space-between;
42+
}
43+
44+
.top-row ::deep a, .top-row ::deep .btn-link {
45+
margin-left: 0;
46+
}
47+
}
48+
49+
@media (min-width: 641px) {
50+
.page {
51+
flex-direction: row;
52+
}
53+
54+
.sidebar {
55+
width: 250px;
56+
height: 100vh;
57+
position: sticky;
58+
top: 0;
59+
}
60+
61+
.top-row {
62+
position: sticky;
63+
top: 0;
64+
z-index: 1;
65+
}
66+
67+
.top-row.auth ::deep a:first-child {
68+
flex: 1;
69+
text-align: right;
70+
width: 0;
71+
}
72+
73+
.top-row, article {
74+
padding-left: 2rem !important;
75+
padding-right: 1.5rem !important;
76+
}
77+
}
78+
79+
#blazor-error-ui {
80+
color-scheme: light only;
81+
background: lightyellow;
82+
bottom: 0;
83+
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
84+
box-sizing: border-box;
85+
display: none;
86+
left: 0;
87+
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
88+
position: fixed;
89+
width: 100%;
90+
z-index: 1000;
91+
}
92+
93+
#blazor-error-ui .dismiss {
94+
cursor: pointer;
95+
position: absolute;
96+
right: 0.75rem;
97+
top: 0.5rem;
98+
}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
@page "/"
2+
@rendermode InteractiveServer
3+
@using System.Collections
4+
@inject SubscriptionRepository SubscriptionService
5+
6+
<div class="container-fluid p-4">
7+
<h1 class="mb-4">Subscription Management System</h1>
8+
<p class="mb-3">Manage and view all subscription records from the MariaDB database.</p>
9+
10+
<SfGrid TValue="SubscriptionModel" AllowSorting="true" AllowFiltering="true" AllowGrouping="true" AllowPaging="true"
11+
Height="500px" Width="100%" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })">
12+
<SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager>
13+
<GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings>
14+
<GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
15+
<GridPageSettings PageSize="10"></GridPageSettings>
16+
<GridColumns>
17+
<GridColumn Field=@nameof(SubscriptionModel.Id) IsPrimaryKey="true" ShowInColumnChooser="false" ShowColumnMenu="false"></GridColumn>
18+
<GridColumn Field=@nameof(SubscriptionModel.PublicID) HeaderText="Public ID" Width="170" TextAlign="TextAlign.Left" AllowAdding="false" AllowEditing="false">
19+
<Template>
20+
@{
21+
var data = (SubscriptionModel)context;
22+
}
23+
<a class="status-text status-ticket-id">
24+
@data.PublicID
25+
</a>
26+
</Template>
27+
</GridColumn>
28+
<GridColumn Field=@nameof(SubscriptionModel.CustomerId) HeaderText="Customer ID" Width="140" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.NumericEdit" />
29+
<GridColumn Field=@nameof(SubscriptionModel.SubscriptionID) HeaderText="Subscription ID" Width="140" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.NumericEdit" />
30+
<GridColumn Field=@nameof(SubscriptionModel.InvoiceNumber) HeaderText="Invoice No" Width="130" EditType="EditType.DefaultEdit" />
31+
<GridColumn Field=@nameof(SubscriptionModel.Description) HeaderText="Description" Width="180" EditType="EditType.DefaultEdit" />
32+
<GridColumn Field=@nameof(SubscriptionModel.Amount) HeaderText="Amount" Width="130" ValidationRules="@(new ValidationRules { Required = true })" Format="N2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit">
33+
<Template>
34+
@{
35+
var amount = (context as SubscriptionModel)?.Amount ?? 0;
36+
var amountClass = amount < 0 ? "amnt-negative" : "amnt-positive";
37+
}
38+
<span class="@amountClass">@amount.ToString("N2")</span>
39+
</Template>
40+
</GridColumn>
41+
<GridColumn Field=@nameof(SubscriptionModel.CurrencyCode) HeaderText="Curr" Width="100" TextAlign="TextAlign.Center" EditType="EditType.DefaultEdit" />
42+
<GridColumn Field=@nameof(SubscriptionModel.SubscriptionType) HeaderText="Type" Width="100" EditType="EditType.DropDownEdit" EditorSettings="@TypeDropDownParams">
43+
<Template>
44+
@{
45+
var data = (SubscriptionModel)context;
46+
}
47+
<span class="chip @GetCategoryClass(data)">
48+
@data.SubscriptionType
49+
</span>
50+
</Template>
51+
</GridColumn>
52+
<GridColumn Field=@nameof(SubscriptionModel.PaymentGateway) HeaderText="Gateway" Width="130" EditType="EditType.DropDownEdit" EditorSettings="@GatewayDropDownParams" />
53+
<GridColumn Field=@nameof(SubscriptionModel.SubscriptionStartDate) HeaderText="Start Date" Width="170" Format="dd-MMM-yy hh:mm tt" TextAlign="TextAlign.Right" Type="ColumnType.DateTime" AllowEditing="false" />
54+
<GridColumn Field=@nameof(SubscriptionModel.SubscriptionEndDate) HeaderText="End Date" Width="170" Format="dd-MMM-yy hh:mm tt" TextAlign="TextAlign.Right" Type="ColumnType.DateTime" EditType="EditType.DateTimePickerEdit" />
55+
56+
<GridColumn Field=@nameof(SubscriptionModel.Status) HeaderText="Status" Width="110" ValidationRules="@(new ValidationRules { Required = true })" EditType="EditType.DropDownEdit" EditorSettings="@StatusDropDownParams">
57+
<Template>
58+
@{
59+
var status = (context as SubscriptionModel)?.Status;
60+
var badgeClass = status?.ToLower() switch
61+
{
62+
"active" => "e-badge e-badge-success",
63+
"expired" => "e-badge e-badge-danger",
64+
"pending" => "e-badge e-badge-warning",
65+
"paused" => "e-badge e-badge-info",
66+
_ => "e-badge"
67+
};
68+
}
69+
<span class="@badgeClass">@status</span>
70+
</Template>
71+
</GridColumn>
72+
</GridColumns>
73+
</SfGrid>
74+
</div>
75+
76+
@code {
77+
private CustomAdaptor? _customAdaptor;
78+
79+
protected override void OnInitialized()
80+
{
81+
// Initialize the CustomAdaptor with the injected SubscriptionRepository
82+
_customAdaptor = new CustomAdaptor { SubscriptionService = SubscriptionService };
83+
}
84+
85+
/// <summary>
86+
/// CustomAdaptor class to handle grid data operations with MariaDB using Entity Framework
87+
/// </summary>
88+
public class CustomAdaptor : DataAdaptor
89+
{
90+
public static SubscriptionRepository? _subscriptionService { get; set; }
91+
public SubscriptionRepository? SubscriptionService
92+
{
93+
get => _subscriptionService;
94+
set => _subscriptionService = value;
95+
}
96+
97+
public override async Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string? key = null)
98+
{
99+
IEnumerable dataSource = await _subscriptionService!.GetSubscriptionsAsync();
100+
101+
// Handling Search
102+
if (dataManagerRequest.Search != null && dataManagerRequest.Search.Count > 0)
103+
{
104+
dataSource = DataOperations.PerformSearching(dataSource, dataManagerRequest.Search);
105+
}
106+
107+
// Handling Filtering
108+
if (dataManagerRequest.Where != null && dataManagerRequest.Where.Count > 0)
109+
{
110+
dataSource = DataOperations.PerformFiltering(dataSource, dataManagerRequest.Where, dataManagerRequest.Where[0].Operator);
111+
}
112+
113+
// Handling Sorting
114+
if (dataManagerRequest.Sorted != null && dataManagerRequest.Sorted.Count > 0)
115+
{
116+
dataSource = DataOperations.PerformSorting(dataSource, dataManagerRequest.Sorted);
117+
}
118+
119+
int totalRecordsCount = dataSource.Cast<SubscriptionModel>().Count();
120+
// Handling Paging
121+
if (dataManagerRequest.Skip != 0)
122+
{
123+
dataSource = DataOperations.PerformSkip(dataSource, dataManagerRequest.Skip);
124+
}
125+
if (dataManagerRequest.Take != 0)
126+
{
127+
dataSource = DataOperations.PerformTake(dataSource, dataManagerRequest.Take);
128+
}
129+
130+
// Handling Group operation in CustomAdaptor.
131+
if (dataManagerRequest.Group != null)
132+
{
133+
foreach (var group in dataManagerRequest.Group)
134+
{
135+
dataSource = DataUtil.Group<SubscriptionModel>(dataSource, group, dataManagerRequest.Aggregates, 0, dataManagerRequest.GroupByFormatter);
136+
//Add custom logic here if needed and remove above method
137+
}
138+
}
139+
140+
return dataManagerRequest.RequiresCounts ? new DataResult() { Result = dataSource, Count = totalRecordsCount } : (object)dataSource;
141+
}
142+
143+
public override async Task<object> InsertAsync(DataManager dataManager, object value, string? key)
144+
{
145+
await _subscriptionService!.AddSubscriptionAsync(value as SubscriptionModel);
146+
return value;
147+
}
148+
149+
public override async Task<object> UpdateAsync(DataManager dataManager, object value, string? keyField, string key)
150+
{
151+
await _subscriptionService!.UpdateSubscriptionAsync(value as SubscriptionModel);
152+
return value;
153+
}
154+
155+
public override async Task<object> RemoveAsync(DataManager dataManager, object value, string? keyField, string key)
156+
{
157+
await _subscriptionService!.RemoveSubscriptionAsync(value as int?);
158+
return value;
159+
}
160+
161+
public override async Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string? keyField, string key, int? dropIndex)
162+
{
163+
if (changedRecords != null)
164+
{
165+
foreach (var record in (IEnumerable<SubscriptionModel>)changedRecords)
166+
{
167+
await _subscriptionService!.UpdateSubscriptionAsync(record as SubscriptionModel);
168+
}
169+
}
170+
if (addedRecords != null)
171+
{
172+
foreach (var record in (IEnumerable<SubscriptionModel>)addedRecords)
173+
{
174+
await _subscriptionService!.AddSubscriptionAsync(record as SubscriptionModel);
175+
}
176+
}
177+
if (deletedRecords != null)
178+
{
179+
foreach (var record in (IEnumerable<SubscriptionModel>)deletedRecords)
180+
{
181+
await _subscriptionService!.RemoveSubscriptionAsync((record as SubscriptionModel).Id);
182+
}
183+
}
184+
return key;
185+
}
186+
}
187+
188+
/// <summary>
189+
/// Provides a list of payment gateway options used as a data source for the Gateway dropdown editor in the grid.
190+
/// </summary>
191+
private static List<SubscriptionModel> CustomGateway = new List<SubscriptionModel> {
192+
new SubscriptionModel() { PaymentGateway = "Razorpay" },
193+
new SubscriptionModel() { PaymentGateway= "GooglePay" },
194+
new SubscriptionModel() { PaymentGateway= "Stripe" },
195+
new SubscriptionModel() { PaymentGateway= "PhonePe" },
196+
new SubscriptionModel() { PaymentGateway= "Paytm" },
197+
};
198+
199+
/// <summary>
200+
/// Provides a list of subscription types used as a data source for the Type dropdown editor in the grid.
201+
/// </summary>
202+
private static List<SubscriptionModel> CustomType = new List<SubscriptionModel> {
203+
new SubscriptionModel() { SubscriptionType = "ACTIVE" },
204+
new SubscriptionModel() { SubscriptionType = "PAUSED" },
205+
new SubscriptionModel() { SubscriptionType = "EXPIRED" },
206+
};
207+
208+
/// <summary>
209+
/// Provides a list of subscription statuses used as a data source for the Status dropdown editor in the grid.
210+
/// </summary>
211+
private static List<SubscriptionModel> CustomStatus = new List<SubscriptionModel> {
212+
new SubscriptionModel() { Status = "ACTIVE" },
213+
new SubscriptionModel() { Status = "PAUSED" },
214+
new SubscriptionModel() { Status = "PENDING" },
215+
new SubscriptionModel() { Status = "EXPIRED" },
216+
};
217+
218+
/// <summary>
219+
/// Dropdown editor settings configured with payment gateway options for the PaymentGateway column in grid edit mode.
220+
/// </summary>
221+
private IEditorSettings GatewayDropDownParams = new DropDownEditCellParams
222+
{
223+
Params = new DropDownListModel<object, object>() { DataSource = CustomGateway, Query = new Syncfusion.Blazor.Data.Query() },
224+
};
225+
226+
/// <summary>
227+
/// Dropdown editor settings configured with subscription type options for the SubscriptionType column in grid edit mode.
228+
/// </summary>
229+
private IEditorSettings TypeDropDownParams = new DropDownEditCellParams
230+
{
231+
Params = new DropDownListModel<object, object>() { DataSource = CustomType, Query = new Syncfusion.Blazor.Data.Query() },
232+
};
233+
234+
/// <summary>
235+
/// Dropdown editor settings configured with subscription status options for the Status column in grid edit mode.
236+
/// </summary>
237+
private IEditorSettings StatusDropDownParams = new DropDownEditCellParams
238+
{
239+
Params = new DropDownListModel<object, object>() { DataSource = CustomStatus, Query = new Syncfusion.Blazor.Data.Query() },
240+
};
241+
242+
/// <summary>
243+
/// Returns a CSS class name based on the subscription type to apply visual styling in the grid template.
244+
/// </summary>
245+
private string GetCategoryClass(SubscriptionModel data)
246+
{
247+
return data.SubscriptionType?.ToLower() switch
248+
{
249+
"active" => "category-active",
250+
"paused" => "category-paused",
251+
"expired" => "category-expired",
252+
_ => ""
253+
};
254+
}
255+
}

0 commit comments

Comments
 (0)