Skip to content

Commit

Permalink
Merge pull request #2 from SyncfusionExamples/BLAZ-932495-sample-update
Browse files Browse the repository at this point in the history
932495: Updated code example of GitHub sample
  • Loading branch information
VigneshNatarajan27 authored Jan 15, 2025
2 parents c96065a + 0985311 commit 52c03c8
Showing 1 changed file with 32 additions and 44 deletions.
76 changes: 32 additions & 44 deletions Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
@using Syncfusion.Blazor.Inputs
@using System.IO

<SfGrid AllowPaging="true" @ref="MyGrid" DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
<GridEvents OnActionBegin="BeginHandler" OnActionComplete="ActionComplete" TValue="Order"></GridEvents>
<SfGrid AllowPaging="true" @ref="Grid" DataSource="@EmployeeData" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
<GridEvents TValue="@EmployeeDetails" RowEditing="RowEditingHandler" RowCreating="RowAddingHandler" RowUpdating="RowUpdatingHandler"></GridEvents>
<GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="EditMode.Dialog">
<Template>
@{
var Order = (context as Order);
var Employee = (context as EmployeeDetails);
}
<div>
<table>
<tbody>
<tr>
<td>
<span>Employee Name</span>
<SfNumericTextBox ID="EmployeeID" @bind-Value="@(Employee.EmployeeID)" Enabled="@((Employee.EmployeeID == 0) ? true : false)" FloatLabelType="FloatLabelType.Always" Placeholder="Employee ID"></SfNumericTextBox>
</td>
</tr>
<tr>
<td>
<b style="margin-left: -50px;">@Order.CustomerID</b><br>
<SfTextBox ID="EmployeeName" @bind-Value="@(Employee.EmployeeName)" TValue="string" FloatLabelType="FloatLabelType.Always" Placeholder="Employee Name">
</SfTextBox>
</td>
</tr>
<tr>
<td>
<span>Employee Image</span>
</td>
<td>
<div class="image"><img class="upload-image" style="margin-top: 10px;margin-left: -50px;" src="@Order.Imagesrc" /></div>
<div class="image"><img class="upload-image" style="margin-top: 10px;margin-left: -50px;" src="@Employee.ImageUrl" /></div>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -59,12 +62,12 @@
</Template>
</GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Employee ID" IsPrimaryKey="true" TextAlign="@TextAlign.Center" Width="140"></GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Employee Name" Width="140"></GridColumn>
<GridColumn Field="Imagesrc" HeaderText="Employee Image" Width="200">
<GridColumn Field=@nameof(EmployeeDetails.EmployeeID) HeaderText="Employee ID" IsPrimaryKey="true" ValidationRules="@(new ValidationRules{ Required=true})" TextAlign="@TextAlign.Right" Width="140"></GridColumn>
<GridColumn Field=@nameof(EmployeeDetails.EmployeeName) HeaderText="Employee Name" Width="140"></GridColumn>
<GridColumn Field="ImageUrl" HeaderText="Employee Image" Width="200">
<Template>
@{
var imageUrl = (context as Order).Imagesrc;
var imageUrl = (context as EmployeeDetails).ImageUrl;
<div class="image">
<img src="@imageUrl" />
</div>
Expand All @@ -85,16 +88,21 @@
@code {

public List<fileInfo> files = new List<fileInfo>();
public SfGrid<Order> MyGrid { get; set; }
public SfGrid<EmployeeDetails> Grid { get; set; }
public string UploadedFile { get; set; }
public List<Order> Orders { get; set; }

public void ActionComplete(ActionEventArgs<Order> args)
public List<EmployeeDetails> EmployeeData { get; set; }
public void RowAddingHandler(RowCreatingEventArgs<EmployeeDetails> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add) || args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.BeginEdit))
{
MyGrid.PreventRender(false);
}
Grid.PreventRender(false);
}
public void RowEditingHandler(RowEditingEventArgs<EmployeeDetails> args)
{
Grid.PreventRender(false);
}
public void RowUpdatingHandler(RowUpdatingEventArgs<EmployeeDetails> args)
{
args.Data.ImageUrl = "scripts/Images/Employees/" + UploadedFile;
}

public void OnChange(UploadChangeEventArgs args)
Expand All @@ -111,46 +119,26 @@
}
}

public void BeginHandler(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Add")
{
Args.Data.Imagesrc = "scripts/Images/Employees/" + UploadedFile;
}
else if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Edit")
{
Args.Data.Imagesrc = "scripts/Images/Employees/" + UploadedFile;
}

}

public void Selected(SelectedEventArgs Args)
{
UploadedFile = Args.FilesData[0].Name;

}

protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 9).Select(x => new Order()
EmployeeData = Enumerable.Range(1, 9).Select(x => new EmployeeDetails()
{
OrderID = 1000 + x,
EmployeeID = x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Imagesrc = "scripts/Images/Employees/" + x + ".png",
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
EmployeeName = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
ImageUrl = "scripts/Images/Employees/" + x + ".png",
}).ToList();
}

public class Order
public class EmployeeDetails
{
public int? OrderID { get; set; }
public int EmployeeID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public string Imagesrc { get; set; }
public double? Freight { get; set; }
public string EmployeeName { get; set; }
public string ImageUrl { get; set; }
}

public class fileInfo
Expand Down

0 comments on commit 52c03c8

Please sign in to comment.