From cf2dcd8030189e0e8ba51dd314c9965cb41b744a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tesa=C5=99?= <33880579+tesar-tech@users.noreply.github.com> Date: Wed, 26 Feb 2025 18:15:18 +0100 Subject: [PATCH] DataGrid: Removes new rows created when BatchEdit enabled and operation canceled (#5990) * Removes batchItem from filtered data on Cancel. * fixes EditingBatchEdit.razor to work also with canceling changes * Formating --------- Co-authored-by: Mladen Macanovic --- .../Tests/DataGrid/EditingBatchEdit.razor | 77 ++++++++++--------- .../Blazorise.DataGrid/DataGrid.razor.cs | 6 ++ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor index 425713ac0b..e788e1650d 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor @@ -31,32 +31,36 @@ 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> - - - - - - - - - - - + + + + + + + + + + + @context.Caption @@ -64,27 +68,26 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - @code { +@code { [Inject] EmployeeData EmployeeData { get; set; } - private int batchQuantity = 0; private DataGrid dataGridRef; private List inMemoryData; private Employee selectedEmployee; @@ -99,7 +102,6 @@ private Task OnBatchChange( DataGridBatchChangeEventArgs args ) { Console.WriteLine( "Batch Change" ); - batchQuantity = dataGridRef.BatchChanges.Count; return Task.CompletedTask; } @@ -112,7 +114,6 @@ private Task OnBatchSaved( DataGridBatchSavedEventArgs args ) { Console.WriteLine( "Batch Saved" ); - batchQuantity = 0; return Task.CompletedTask; } diff --git a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs index 704ee6fd83..7f19231e3d 100644 --- a/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs +++ b/Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs @@ -1405,6 +1405,12 @@ public async Task Cancel() { if ( BatchEdit ) { + if ( filteredData is ICollection data2 ) + { + foreach ( var newItem in batchChanges.Where( x => x.State == DataGridBatchEditItemState.New ) ) + data2.Remove( newItem.NewItem ); + } + batchChanges?.Clear(); }