Skip to content

Commit b45bcd3

Browse files
authored
Refine logic that determines if the DataGrid data needs to be reloaded (#3864)
1 parent 18144c1 commit b45bcd3

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,12 @@
12411241
If not specified, the default header template includes the <see cref="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.Title" /> along with any applicable sort indicators and options buttons.
12421242
</summary>
12431243
</member>
1244+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.HeaderCellTitleTemplate">
1245+
<summary>
1246+
Gets or sets a template for the title content of this column's header cell.
1247+
If not specified, the default header template includes the <see cref="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.Title" />.
1248+
</summary>
1249+
</member>
12441250
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.ColumnOptions">
12451251
<summary>
12461252
If specified, indicates that this column has this associated options UI. A button to display this
@@ -1350,6 +1356,14 @@
13501356
respecting that option.
13511357
</summary>
13521358
</member>
1359+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.HeaderTitleContent">
1360+
<summary>
1361+
Gets or sets a <see cref="T:Microsoft.AspNetCore.Components.RenderFragment" /> that will be rendered for this column's header title.
1362+
This allows derived components to change the header title output. However, derived components are then
1363+
responsible for using <see cref="P:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.HeaderCellTitleTemplate" /> within that new output if they want to continue
1364+
respecting that option.
1365+
</summary>
1366+
</member>
13531367
<member name="M:Microsoft.FluentUI.AspNetCore.Components.ColumnBase`1.IsSortableByDefault">
13541368
<summary>
13551369
Gets a value indicating whether this column should act as sortable if no value was set for the
@@ -2262,6 +2276,12 @@
22622276
</summary>
22632277
<returns></returns>
22642278
</member>
2279+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.ComputeItemsHash(System.Collections.Generic.IEnumerable{`0},System.Int32)">
2280+
<summary>
2281+
Computes a hash code for the given items.
2282+
To limit the effect on performance, only the given maximum number (default 250) of items will be considered.
2283+
</summary>
2284+
</member>
22652285
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGridCell`1.Item">
22662286
<summary>
22672287
Gets or sets the reference to the item that holds this cell's values.

examples/Demo/Shared/Pages/Menu/MenuPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<p>
4141
With version 4.9.4 of the library, we introduced the <code>FluentMenuProvider</code> component. The Menu component has been updated to use this provider.
4242
The <code>&lt;FluentMenuProvider /&gt;</code> needs to be placed at the <b>bottom</b> of your HTML page (just like the other <b>...Providers</b> components).
43-
It will renders all menus (and menu items) at the provider location in the HTML structure. This allows for menus to appear <b>on top</b> other components.
43+
It will render all menus (and menu items) at the provider location in the HTML structure. This allows for menus to appear <b>above</b> of other components.
4444
</p>
4545
<p>
4646
You can disable this feature by adding the <code>UseMenuService</code> parameter (with a value of "false") to you FluentMenu component. In this case, the menu will be rendered at the location it is placed at in the page.

src/Core/Components/DataGrid/FluentDataGrid.razor.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ public partial class FluentDataGrid<TGridItem> : FluentComponentBase, IHandleEve
374374
// things have changed, and to discard earlier load attempts that were superseded.
375375
private PaginationState? _lastRefreshedPaginationState;
376376
private IQueryable<TGridItem>? _lastAssignedItems;
377+
private int _lastAssignedItemsHashCode;
377378
private GridItemsProvider<TGridItem>? _lastAssignedItemsProvider;
378379
private CancellationTokenSource? _pendingDataLoadCancellationTokenSource;
379380

@@ -434,14 +435,18 @@ protected override Task OnParametersSetAsync()
434435
throw new InvalidOperationException($"FluentDataGrid cannot use both {nameof(Virtualize)} and {nameof(MultiLine)} at the same time.");
435436
}
436437

438+
var currentItemsHash = FluentDataGrid<TGridItem>.ComputeItemsHash(Items);
439+
var itemsChanged = currentItemsHash != _lastAssignedItemsHashCode;
440+
437441
// Perform a re-query only if the data source or something else has changed
438-
var dataSourceHasChanged = !Equals(Items, _lastAssignedItems) || !Equals(ItemsProvider, _lastAssignedItemsProvider);
442+
var dataSourceHasChanged = itemsChanged || !Equals(ItemsProvider, _lastAssignedItemsProvider);
439443
if (dataSourceHasChanged)
440444
{
441445
_scope?.Dispose();
442446
_scope = ScopeFactory.CreateAsyncScope();
443447
_lastAssignedItemsProvider = ItemsProvider;
444448
_lastAssignedItems = Items;
449+
_lastAssignedItemsHashCode = currentItemsHash;
445450
_asyncQueryExecutor = AsyncQueryExecutorSupplier.GetAsyncQueryExecutor(_scope.Value.ServiceProvider, Items);
446451
}
447452

@@ -1102,4 +1107,31 @@ public async Task ResetColumnWidthsAsync()
11021107
await Module.InvokeVoidAsync("resetColumnWidths", _gridReference);
11031108
}
11041109
}
1110+
1111+
/// <summary>
1112+
/// Computes a hash code for the given items.
1113+
/// To limit the effect on performance, only the given maximum number (default 250) of items will be considered.
1114+
/// </summary>
1115+
private static int ComputeItemsHash(IEnumerable<TGridItem>? items, int maxItems = 250)
1116+
{
1117+
if (items == null)
1118+
{
1119+
return 0;
1120+
}
1121+
unchecked
1122+
{
1123+
var hash = 19;
1124+
var count = 0;
1125+
foreach (var item in items)
1126+
{
1127+
if (++count > maxItems)
1128+
{
1129+
break;
1130+
}
1131+
hash = (hash * 31) + (item?.GetHashCode() ?? 0);
1132+
}
1133+
return hash;
1134+
}
1135+
}
11051136
}
1137+

0 commit comments

Comments
 (0)