-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHome.razor
More file actions
106 lines (87 loc) · 4.52 KB
/
Home.razor
File metadata and controls
106 lines (87 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
@page "/"
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Calendars
<SfGrid TValue="OrderData" @ref="Grid" Height="500px" AllowPaging="true" AllowFiltering="true" AllowSorting="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })">
<SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager>
<GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="EditMode.Normal"></GridEditSettings>
<GridColumns>
<GridColumn Field="@nameof(OrderData.OrderID)" HeaderText="Order ID" IsPrimaryKey="true" Width="140" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })"></GridColumn>
<GridColumn Field="@nameof(OrderData.CustomerID)" HeaderText="Customer ID" Width="140" ValidationRules="@(new ValidationRules { Required = true })"></GridColumn>
<GridColumn Field="@nameof(OrderData.EmployeeID)" HeaderText="Employee ID" Width="120" TextAlign="TextAlign.Right"></GridColumn>
<GridColumn Field="@nameof(OrderData.ShipCountry)" HeaderText="Ship Country" Width="150"></GridColumn>
</GridColumns>
</SfGrid>
@code {
private SfGrid<OrderData> Grid;
public List<OrderData> Orders { get; set; }
protected override void OnInitialized()
{
Orders = OrderData.GetAllRecords();
}
public class CustomAdaptor : DataAdaptor
{
public override object Read(DataManagerRequest dm, string key = null)
{
IEnumerable<OrderData> DataSource = OrderData.GetAllRecords();
if (dm.Search != null && dm.Search.Count > 0)
DataSource = DataOperations.PerformSearching(DataSource, dm.Search);
if (dm.Sorted != null && dm.Sorted.Count > 0)
DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
if (dm.Where != null && dm.Where.Count > 0)
DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
int count = DataSource.Cast<OrderData>().Count();
if (dm.Skip != 0)
DataSource = DataOperations.PerformSkip(DataSource, dm.Skip);
if (dm.Take != 0)
DataSource = DataOperations.PerformTake(DataSource, dm.Take);
return dm.RequiresCounts
? new DataResult() { Result = DataSource, Count = count }
: (object)DataSource;
}
public override object Insert(DataManager dataManager, object record, string additionalParam)
{
if(record == null){
return "no record";
}
// Check if the record is null before inserting
if (record is OrderData orderData)
{
OrderData.GetAllRecords().Insert(0, orderData);
}
return record;
}
public override object Update(DataManager dataManager, object updateRecord, string primaryColumnName, string additionalParam)
{
if (updateRecord is OrderData updatedOrder)
{
// Retrieve the existing order based on the primary key
var existingOrder = OrderData.GetAllRecords()
.FirstOrDefault(order => order.OrderID == updatedOrder.OrderID);
if (existingOrder != null)
{
// Update properties if they are not null or default
existingOrder.CustomerID = updatedOrder.CustomerID;
existingOrder.EmployeeID = updatedOrder.EmployeeID;
existingOrder.ShipCountry = updatedOrder.ShipCountry;
existingOrder.Freight = updatedOrder.Freight;
existingOrder.Verified = updatedOrder.Verified;
}
}
// Call the base method to complete the update process
return (updateRecord);
}
public override object Remove(DataManager dataManager, object primaryColumnValue, string primaryColumnName, string additionalParam)
{
int orderId = int.Parse(primaryColumnValue.ToString());
var data = OrderData.GetAllRecords().FirstOrDefault(orderData => orderData.OrderID == orderId);
if (data != null)
{
// Remove the record from the data collection
OrderData.GetAllRecords().Remove(data);
}
return (primaryColumnValue);
}
}
}