Skip to content

Commit

Permalink
DataGrid: Removes new rows created when BatchEdit enabled and operati…
Browse files Browse the repository at this point in the history
…on canceled (#5990)

* Removes batchItem from filtered data on Cancel.

* fixes EditingBatchEdit.razor to work also with canceling changes

* Formating

---------

Co-authored-by: Mladen Macanovic <[email protected]>
  • Loading branch information
tesar-tech and stsrki authored Feb 26, 2025
1 parent ae0cd5d commit cf2dcd8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
77 changes: 39 additions & 38 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,63 @@
Responsive
ShowPager
ShowPageSizes
@bind-SelectedRow="@selectedEmployee"
@bind-SelectedRow="@selectedEmployee"
Editable
EditMode="@editMode"
EditMode="@editMode"
BatchEdit
BatchChange="OnBatchChange"
BatchSaving="OnBatchSaving"
BatchSaved="OnBatchSaved"
BatchEditCellStyling="OnCellBatchEditStyling"
RowBatchEditStyling="(x, y ) => OnRowBatchEditStyling(x,y)"
BatchChange="OnBatchChange"
BatchSaving="OnBatchSaving"
BatchSaved="OnBatchSaved"
BatchEditCellStyling="OnCellBatchEditStyling"
RowBatchEditStyling="( x, y ) => OnRowBatchEditStyling( x, y )"
UseValidation
ValidationsSummaryLabel="The following validation errors have occurred..."
ValidationsSummaryLabel="The following validation errors have occurred..."
ShowValidationsSummary>
<DataGridColumns>
<DataGridCommandColumn>
<SaveBatchCommandTemplate>
<Button Background=Background.Info Size=Size.Small Clicked="@context.Clicked">Save Changes (@batchQuantity.ToString())</Button>
</SaveBatchCommandTemplate>
<CancelBatchCommandTemplate>
<Button Background=Background.Light Size=Size.Small Clicked="@context.Clicked">Cancel Changes</Button>
</CancelBatchCommandTemplate>
</DataGridCommandColumn>
<DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City" Editable>
<Button Background=Background.Info Size=Size.Small Clicked="@context.Clicked">
Save Changes (@(dataGridRef?.BatchChanges?.Count ?? 0))
</Button>
</SaveBatchCommandTemplate>
<CancelBatchCommandTemplate>
<Button Background=Background.Light Size=Size.Small Clicked="@context.Clicked">
Cancel Changes
</Button>
</CancelBatchCommandTemplate>
</DataGridCommandColumn>
<DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City" Editable>
<CaptionTemplate>
<Icon Name="IconName.City" /> @context.Caption
</CaptionTemplate>
</DataGridColumn>
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Zip )" Caption="Zip">
</DataGridColumn>
<DataGridDateColumn TItem="Employee" Field="@nameof( Employee.DateOfBirth )" DisplayFormat="{0:dd.MM.yyyy}" Caption="Date Of Birth" Editable />
<DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" />
<DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable Data="EmployeeData.Genders" ValueField="(x) => ((Gender)x).Code" TextField="(x) => ((Gender)x).Description" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End">
</DataGridColumn>
<DataGridCheckColumn TItem="Employee" Field="@nameof(Employee.IsActive)" Caption="Active" Editable Filterable="false">
<DisplayTemplate>
<Check TValue="bool" Checked="context.IsActive" Disabled ReadOnly />
</DisplayTemplate>
</DataGridCheckColumn>
</DataGridColumns>
</DataGrid>
</CardBody>
</Card>
</Column>
</Row>
<DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" />
<DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable Data="EmployeeData.Genders" ValueField="( x ) => ( (Gender)x ).Code" TextField="( x ) => ( (Gender)x ).Description" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo( "fr-FR" )" TextAlignment="TextAlignment.End">
</DataGridColumn>
<DataGridCheckColumn TItem="Employee" Field="@nameof( Employee.IsActive )" Caption="Active" Editable Filterable="false">
<DisplayTemplate>
<Check TValue="bool" Checked="context.IsActive" Disabled ReadOnly />
</DisplayTemplate>
</DataGridCheckColumn>
</DataGridColumns>
</DataGrid>
</CardBody>
</Card>
</Column>
</Row>

@code {
@code {

[Inject] EmployeeData EmployeeData { get; set; }

private int batchQuantity = 0;
private DataGrid<Employee> dataGridRef;
private List<Employee> inMemoryData;
private Employee selectedEmployee;
Expand All @@ -99,7 +102,6 @@
private Task OnBatchChange( DataGridBatchChangeEventArgs<Employee> args )
{
Console.WriteLine( "Batch Change" );
batchQuantity = dataGridRef.BatchChanges.Count;
return Task.CompletedTask;
}

Expand All @@ -112,7 +114,6 @@
private Task OnBatchSaved( DataGridBatchSavedEventArgs<Employee> args )
{
Console.WriteLine( "Batch Saved" );
batchQuantity = 0;
return Task.CompletedTask;
}

Expand Down
6 changes: 6 additions & 0 deletions Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,12 @@ public async Task Cancel()
{
if ( BatchEdit )
{
if ( filteredData is ICollection<TItem> data2 )
{
foreach ( var newItem in batchChanges.Where( x => x.State == DataGridBatchEditItemState.New ) )
data2.Remove( newItem.NewItem );
}

batchChanges?.Clear();
}

Expand Down

0 comments on commit cf2dcd8

Please sign in to comment.