Browse Source

Merge pull request #26330 from abpframework/auto-merge-forward/rel-10.7-to-dev-102

Auto-merge forward rel-10.7 → dev
pull/26331/head
Volosoft Agent 1 month ago
committed by GitHub
parent
commit
5dc6dbbceb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 14
      framework/src/Volo.Abp.MudBlazorUI/AbpMudCrudPageBase.cs
  2. 6
      framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor
  3. 46
      framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs

14
framework/src/Volo.Abp.MudBlazorUI/AbpMudCrudPageBase.cs

@ -180,6 +180,7 @@ public abstract class AbpMudCrudPageBase<
protected virtual int PageSize { get; set; } = LimitedResultRequestDto.DefaultMaxResultCount; protected virtual int PageSize { get; set; } = LimitedResultRequestDto.DefaultMaxResultCount;
protected IReadOnlyList<TListViewModel> Entities = Array.Empty<TListViewModel>();
protected int CurrentPage = 1; protected int CurrentPage = 1;
protected string CurrentSorting = default!; protected string CurrentSorting = default!;
protected TGetListInput GetListInput = new TGetListInput(); protected TGetListInput GetListInput = new TGetListInput();
@ -349,6 +350,7 @@ public abstract class AbpMudCrudPageBase<
await UpdateGetListInputAsync(); await UpdateGetListInputAsync();
var result = await AppService.GetListAsync(GetListInput); var result = await AppService.GetListAsync(GetListInput);
var items = MapToListViewModel(result.Items); var items = MapToListViewModel(result.Items);
Entities = items;
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
@ -599,7 +601,17 @@ public abstract class AbpMudCrudPageBase<
protected virtual async Task OnDeletedEntityAsync() protected virtual async Task OnDeletedEntityAsync()
{ {
if (_dataGrid != null) if (Entities.Count == 1 && CurrentPage > 1)
{
// The data grid loads the previous page by itself when it receives the new CurrentPage
CurrentPage -= 1;
await InvokeAsync(StateHasChanged);
if (_dataGrid != null)
{
await InvokeAsync(() => _dataGrid.WaitForServerDataAsync());
}
}
else if (_dataGrid != null)
{ {
await InvokeAsync(() => _dataGrid.ReloadServerDataAsync()); await InvokeAsync(() => _dataGrid.ReloadServerDataAsync());
} }

6
framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor

@ -2,7 +2,7 @@
@using MudBlazor @using MudBlazor
@using Volo.Abp.Data @using Volo.Abp.Data
<MudDataGrid @ref="_dataGrid" T="TItem" ServerData="@ServerData" Loading="@Loading" Dense="true" Striped="true" <MudDataGrid @ref="_dataGrid" T="TItem" ServerData="@GetServerDataFunc()" Loading="@Loading" Dense="true" Striped="true"
Bordered="false" Hover="true" Class="@Class" RowsPerPage="@PageSize" CurrentPage="@ZeroBasedCurrentPage"> Bordered="false" Hover="true" Class="@Class" RowsPerPage="@PageSize" CurrentPage="@ZeroBasedCurrentPage">
<LoadingContent> <LoadingContent>
<MudProgressCircular Color="Color.Primary" Indeterminate="true" /> <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
@ -71,12 +71,12 @@
@if (column.ValueConverter == null) @if (column.ValueConverter == null)
{ {
<PropertyColumn T="TItem" TProperty="object" Property="@(x => GetPropertyValue(x, column.Data))" <PropertyColumn T="TItem" TProperty="object" Property="@(x => GetPropertyValue(x, column.Data))"
Title="@column.Title" Sortable="@column.Sortable" Hidden="@(!column.Visible)" Title="@column.Title" Sortable="@column.Sortable" SortBy="@GetSortByFunc(column)" Hidden="@(!column.Visible)"
CellStyle="@GetColumnStyle(column)" /> CellStyle="@GetColumnStyle(column)" />
} }
else else
{ {
<TemplateColumn T="TItem" Title="@column.Title" Sortable="@column.Sortable" Hidden="@(!column.Visible)" <TemplateColumn T="TItem" Title="@column.Title" Sortable="@column.Sortable" SortBy="@GetSortByFunc(column)" Hidden="@(!column.Visible)"
CellStyle="@GetColumnStyle(column)"> CellStyle="@GetColumnStyle(column)">
<CellTemplate> <CellTemplate>
@((MarkupString)GetConvertedFieldValue(context.Item, column)) @((MarkupString)GetConvertedFieldValue(context.Item, column))

46
framework/src/Volo.Abp.MudBlazorUI/Components/AbpMudExtensibleDataGrid.razor.cs

@ -31,6 +31,14 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
/// </summary> /// </summary>
private readonly Dictionary<Func<TItem, object?>, string> _sortFuncToPropertyMap = new(); private readonly Dictionary<Func<TItem, object?>, string> _sortFuncToPropertyMap = new();
/// <summary>
/// Keeps one sort function per sort path, so re-renders reuse it instead of growing <see cref="_sortFuncToPropertyMap"/>.
/// Keyed by path rather than by column, because a column's data can change between renders.
/// </summary>
private readonly Dictionary<string, Func<TItem, object?>> _sortFuncsByPath = new();
private Task? _serverDataTask;
[Parameter] public Func<GridState<TItem>, CancellationToken, Task<GridData<TItem>>>? ServerData { get; set; } [Parameter] public Func<GridState<TItem>, CancellationToken, Task<GridData<TItem>>>? ServerData { get; set; }
[Parameter] public bool Loading { get; set; } [Parameter] public bool Loading { get; set; }
@ -66,6 +74,30 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
} }
} }
/// <summary>
/// Waits for the server data load that is in progress, such as the one the data grid starts by itself
/// when <see cref="CurrentPage"/> changes. Completes immediately if there is no load in progress.
/// </summary>
public virtual async Task WaitForServerDataAsync()
{
if (_serverDataTask != null)
{
await _serverDataTask;
}
}
protected virtual Func<GridState<TItem>, CancellationToken, Task<GridData<TItem>>>? GetServerDataFunc()
{
return ServerData == null ? null : LoadServerDataAsync;
}
private Task<GridData<TItem>> LoadServerDataAsync(GridState<TItem> state, CancellationToken cancellationToken)
{
var task = ServerData!(state, cancellationToken);
_serverDataTask = task;
return task;
}
/// <summary> /// <summary>
/// Resolves the property name for a given SortDefinition by matching its SortFunc reference. /// Resolves the property name for a given SortDefinition by matching its SortFunc reference.
/// When MudBlazor uses Func for SortBy on TemplateColumn, it returns an internal GUID identifier /// When MudBlazor uses Func for SortBy on TemplateColumn, it returns an internal GUID identifier
@ -216,6 +248,12 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
return null; return null;
} }
var cacheKey = "property:" + propertyPath;
if (_sortFuncsByPath.TryGetValue(cacheKey, out var cachedSortFunc))
{
return cachedSortFunc;
}
var properties = propertyPath.Split('.'); var properties = propertyPath.Split('.');
// Build a lambda expression that directly accesses the property // Build a lambda expression that directly accesses the property
@ -244,6 +282,7 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
// Register the mapping so we can resolve property name from SortDefinition.SortFunc later // Register the mapping so we can resolve property name from SortDefinition.SortFunc later
_sortFuncToPropertyMap[sortFunc] = propertyPath; _sortFuncToPropertyMap[sortFunc] = propertyPath;
_sortFuncsByPath[cacheKey] = sortFunc;
return sortFunc; return sortFunc;
} }
@ -262,6 +301,12 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
} }
var propertyName = match.Groups[1].Value; var propertyName = match.Groups[1].Value;
var cacheKey = "extension:" + propertyName;
if (_sortFuncsByPath.TryGetValue(cacheKey, out var cachedSortFunc))
{
return cachedSortFunc;
}
Func<TItem, object?> sortFunc = item => Func<TItem, object?> sortFunc = item =>
{ {
var entity = item as IHasExtraProperties; var entity = item as IHasExtraProperties;
@ -270,6 +315,7 @@ public partial class AbpMudExtensibleDataGrid<TItem> : ComponentBase
// Register the mapping so we can resolve property name from SortDefinition.SortFunc later // Register the mapping so we can resolve property name from SortDefinition.SortFunc later
_sortFuncToPropertyMap[sortFunc] = propertyName; _sortFuncToPropertyMap[sortFunc] = propertyName;
_sortFuncsByPath[cacheKey] = sortFunc;
return sortFunc; return sortFunc;
} }

Loading…
Cancel
Save