Skip to content

Commit d9260d2

Browse files
authored
DataGrid: Data list optimization checks (#6144)
* Optimize AreEqual with ReferenceEquals check * Make SetDirty protected * Add lastKnownDataCount optimization
1 parent 8dce7ce commit d9260d2

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Source/Blazorise/Extensions/ArrayExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public static class ArrayExtensions
1919
/// <returns>True if all elements are equal.</returns>
2020
public static bool AreEqual<T>( this IEnumerable<T> array1, IEnumerable<T> array2 )
2121
{
22-
if ( array1 is null && array2 is null )
22+
if ( ReferenceEquals( array1, array2 ) )
2323
return true;
2424

25-
if ( ( array1 is not null && array2 is null ) || ( array2 is not null && array1 is null ) )
25+
if ( array1 is null || array2 is null )
2626
return false;
2727

2828
return array1.SequenceEqual( array2 );

Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public partial class DataGrid<TItem> : BaseDataGridComponent
4949
/// </summary>
5050
private List<TItem> filteredData = new();
5151

52+
/// <summary>
53+
/// Represents the last known count of data items processed or retrieved.
54+
/// </summary>
55+
private int lastKnownDataCount;
56+
5257
/// <summary>
5358
/// Holds the filtered data to display based on the current page.
5459
/// </summary>
@@ -468,8 +473,17 @@ public override async Task SetParametersAsync( ParameterView parameters )
468473
{
469474
await CheckMultipleSelectionSetEmpty( parameters );
470475

471-
if ( parameters.TryGetValue<IEnumerable<TItem>>( nameof( Data ), out var paramData ) && !Data.AreEqual( paramData ) )
472-
SetDirty();
476+
if ( parameters.TryGetValue<IEnumerable<TItem>>( nameof( Data ), out var paramData ) )
477+
{
478+
var newCount = paramData?.Count() ?? 0;
479+
480+
if ( lastKnownDataCount != newCount || !Data.AreEqual( paramData ) )
481+
{
482+
SetDirty();
483+
}
484+
485+
lastKnownDataCount = newCount;
486+
}
473487

474488
if ( parameters.TryGetValue<DataGridSelectionMode>( nameof( SelectionMode ), out var paramSelectionMode ) && SelectionMode != paramSelectionMode )
475489
ExecuteAfterRender( HandleSelectionModeChanged );
@@ -1204,8 +1218,12 @@ public async Task Delete( TItem item )
12041218
if ( UseInternalEditing )
12051219
{
12061220
if ( data.Contains( item ) )
1221+
{
12071222
data.Remove( item );
12081223

1224+
lastKnownDataCount = Data?.Count() ?? 0;
1225+
}
1226+
12091227
if ( itemIsSelected )
12101228
{
12111229
SelectedRow = default;
@@ -1315,6 +1333,8 @@ protected internal async Task SaveBatch()
13151333
break;
13161334
}
13171335
}
1336+
1337+
lastKnownDataCount = Data?.Count() ?? 0;
13181338
}
13191339

13201340
await BatchSaved.InvokeAsync( new DataGridBatchSavedEventArgs<TItem>( batchChanges ) );
@@ -1439,6 +1459,8 @@ protected internal async Task SaveItem()
14391459
if ( UseInternalEditing && editState == DataGridEditState.New && CanInsertNewItem && Data is ICollection<TItem> data )
14401460
{
14411461
data.Add( editItem );
1462+
1463+
lastKnownDataCount = Data?.Count() ?? 0;
14421464
}
14431465

14441466
if ( UseInternalEditing || editState == DataGridEditState.New )
@@ -1494,6 +1516,8 @@ public async Task Cancel()
14941516
{
14951517
foreach ( var newItem in batchChanges.Where( x => x.State == DataGridBatchEditItemState.New ) )
14961518
data2.Remove( newItem.NewItem );
1519+
1520+
lastKnownDataCount = Data?.Count() ?? 0;
14971521
}
14981522

14991523
batchChanges?.Clear();
@@ -2173,7 +2197,10 @@ internal async Task<bool> IsSafeToProceed( EventCallback<DataGridBatchSavingEven
21732197

21742198
#region Filtering
21752199

2176-
private void SetDirty()
2200+
/// <summary>
2201+
/// Marks the current filter and view as dirty, indicating that they require updating.
2202+
/// </summary>
2203+
protected void SetDirty()
21772204
{
21782205
dirtyFilter = dirtyView = true;
21792206
}

0 commit comments

Comments
 (0)