!2291 fix(#I4PPY4): throw exception when datasource collection is empty on Table component

* chore: bump version 6.2.1
* doc: 更新示例
* fix: 修复集合为空时聚合函数报错
* feat: 更新 Empty 内部结构
This commit is contained in:
Argo 2022-01-06 12:04:45 +00:00
parent b0cbe1da84
commit ff9a0cb50e
6 changed files with 49 additions and 19 deletions

View File

@ -60,11 +60,11 @@
<td>
<div class="footer-customer">
<div>
Average: @context.Average(i => i.Count)
Average: @GetAverage(context)
</div>
<hr />
<div>
Sum: @context.Sum(i => i.Count)
Sum: @GetSum(context)
</div>
</div>
</td>

View File

@ -73,4 +73,8 @@ public sealed partial class TablesFooter
IsSearch = true
});
}
private static double GetAverage(IEnumerable<Foo> items) => items.Any() ? items.Average(i => i.Count) : 0;
private static int GetSum(IEnumerable<Foo> items) => items.Any() ? items.Sum(i => i.Count) : 0;
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<Version>6.2.0</Version>
<Version>6.2.1</Version>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">

View File

@ -2,23 +2,26 @@
@inherits BootstrapComponentBase
<div @attributes="AdditionalAttributes" class="@ClassString">
@if (!string.IsNullOrEmpty(Image))
{
<div class="empty-image">
<img src="@Image">
</div>
}
@if (!string.IsNullOrEmpty(Text))
{
<div class="empty-text">
<span>@Text</span>
</div>
}
@if (Template != null)
{
<div class="empty-telemplate">
@Template
</div>
}
@ChildContent
else
{
@if (!string.IsNullOrEmpty(Image))
{
<div class="empty-image">
<img src="@Image">
</div>
}
@if (!string.IsNullOrEmpty(Text))
{
<div class="empty-text">
<span>@Text</span>
</div>
}
@ChildContent
}
</div>

View File

@ -335,7 +335,7 @@
{
<tr>
<td colspan="@GetColumnCount()">
<Empty Text="@EmptyText" Image="@EmptyImage" Template="@EmptyTemplate">
<Empty Text="@EmptyText" Image="@EmptyImage" Template="EmptyTemplate!">
</Empty>
</td>
</tr>

View File

@ -59,7 +59,7 @@ public partial class TableFooterCell
[CascadingParameter(Name = "TableFooterContext")]
private object? DataSource { get; set; }
private string? GetText() => Text ?? GetCountValue() ?? GetAggegateValue();
private string? GetText() => Text ?? (GetCount(DataSource) == 0 ? "0" : (GetCountValue() ?? GetAggegateValue()));
/// <summary>
/// 解析 Count Aggregate
@ -209,5 +209,28 @@ public partial class TableFooterCell
return mi?.MakeGenericMethod(modelType);
}
private static int CreateCountMethod<TSource>(IEnumerable<TSource> source) => Enumerable.Count(source);
private static int CreateCountMethod<TSource>(IEnumerable<TSource> source) => source.Count();
private static int GetCount(object? source)
{
var ret = 0;
if (source != null)
{
// 绑定数据源类型
var type = source.GetType();
// 数据源泛型 TModel 类型
var modelType = type.GenericTypeArguments[0];
var mi = typeof(TableFooterCell).GetMethod(nameof(CreateCountMethod), BindingFlags.NonPublic | BindingFlags.Static)
?.MakeGenericMethod(modelType);
if (mi != null)
{
var v = mi.Invoke(null, new object[] { source })?.ToString();
_ = int.TryParse(v, out ret);
}
}
return ret;
}
}