fix(module: table): supports datasource of abstract classes (#3475)

This commit is contained in:
James Yeung 2023-10-29 18:40:07 +08:00 committed by GitHub
parent 54d821e6ec
commit e1f3be5874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 33 deletions

View File

@ -7,7 +7,7 @@
@typeparam TItem
<div class="@_wrapperClassMapper.Class" @ref="Ref">
<Spin Spinning="Loading">
<Spin Spinning="Loading || (_fieldModel is null)">
@if (!HidePagination && PaginationPosition.Contains("top"))
{
if (PaginationTemplate == null)
@ -29,7 +29,10 @@
<CascadingValue Value=@typeof(TItem) Name="ItemType" IsFixed>
<CascadingValue Value="@ColumnContext" IsFixed>
<CascadingValue Name="IsInitialize" Value="true" IsFixed>
@ChildContent(_fieldModel)
@if (_fieldModel is not null)
{
@ChildContent(_fieldModel)
}
</CascadingValue>
<div class="@ClassMapper.Class">
@if (TitleTemplate != null || Title != null)
@ -53,7 +56,10 @@
<tbody class="ant-table-tbody">
<tr aria-hidden="true" class="ant-table-measure-row" style="height: 0px; font-size: 0px;">
<CascadingValue Name="IsMeasure" Value="true" IsFixed>
@ChildContent(_fieldModel)
@if (_fieldModel is not null)
{
@ChildContent(_fieldModel)
}
</CascadingValue>
</tr>
@body(this, _showItems, 0, _rootRowDataCache)
@ -71,7 +77,10 @@
<tbody class="ant-table-tbody">
<tr aria-hidden="true" class="ant-table-measure-row" style="height: 0px; font-size: 0px;">
<CascadingValue Name="IsMeasure" Value="true" IsFixed>
@ChildContent(_fieldModel)
@if (_fieldModel is not null)
{
@ChildContent(_fieldModel)
}
</CascadingValue>
</tr>
@body(this, _showItems, 0, _rootRowDataCache)
@ -135,15 +144,18 @@
<CascadingValue Value="headerRowAttributes" Name="AntDesign.TableRow.RowAttributes">
<CascadingValue Value="table._hasFixRight" Name="AntDesign.TableRow.HasFixRight">
<CascadingValue Value="table.ScrollY" Name="AntDesign.TableRow.ScrollY">
@if (table.HeaderTemplate != null)
@if (_fieldModel is not null)
{
@table.HeaderTemplate(_fieldModel)
}
else
{
<TableRow>
@table.ChildContent(_fieldModel)
</TableRow>
@if (table.HeaderTemplate != null)
{
@table.HeaderTemplate(_fieldModel)
}
else
{
<TableRow>
@table.ChildContent(_fieldModel)
</TableRow>
}
}
</CascadingValue>
</CascadingValue>
@ -160,7 +172,10 @@ RenderFragment<(Table<TItem> table, bool header)> colGroup = ctx =>
}
<colgroup>
<CascadingValue Name="IsColGroup" Value="true" IsFixed>
@if (_fieldModel is not null)
{
@table.ChildContent(_fieldModel)
}
</CascadingValue>
@if (table.ScrollY != null && header)
{

View File

@ -28,7 +28,10 @@ namespace AntDesign
public partial class Table<TItem> : AntDomComponentBase, ITable, IEqualityComparer<TItem>, IAsyncDisposable
{
private static readonly TItem _fieldModel = typeof(TItem).IsInterface ? DispatchProxy.Create<TItem, TItemProxy>() : (TItem)RuntimeHelpers.GetUninitializedObject(typeof(TItem));
private static TItem _fieldModel = typeof(TItem).IsInterface ? DispatchProxy.Create<TItem, TItemProxy>()
: !typeof(TItem).IsAbstract ? (TItem)RuntimeHelpers.GetUninitializedObject(typeof(TItem))
: default;
private static readonly EventCallbackFactory _callbackFactory = new EventCallbackFactory();
private bool _preventRender = false;
@ -47,6 +50,7 @@ namespace AntDesign
_waitingDataSourceReload = true;
_dataSourceCount = value is IQueryable<TItem> ? 0 : value?.Count() ?? 0;
_dataSource = value ?? Enumerable.Empty<TItem>();
_fieldModel ??= _dataSource.FirstOrDefault();
}
}
@ -173,7 +177,6 @@ namespace AntDesign
[Parameter] public Func<TItem, object> RowKey { get; set; } = default!;
/// <summary>
/// Enable resizable column
/// </summary>
@ -816,4 +819,4 @@ namespace AntDesign
return RowKey(obj).GetHashCode();
}
}
}
}

View File

@ -27,13 +27,5 @@
<ItemGroup>
<ProjectReference Include="..\..\components\AntDesign.csproj" />
</ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Demos\Components\Table\demo\CustomFieldFilter.razor" />
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="Demos\Components\Table\demo\CustomFieldFilter.razor" />
</ItemGroup>
</Project>

View File

@ -16,45 +16,59 @@
</ActionColumn>
</ColumnDefinitions>
<ExpandTemplate>
@context.Data.Description
@if (context.Data is Student student)
{
<p>Student</p>
<p>Grade: @student.Grade</p>
}
else if (context.Data is Teacher teacher)
{
<p>Teacher</p>
<p>Subject: @teacher.Subject</p>
}
<p>Description: @context.Data.Description</p>
</ExpandTemplate>
</Table>
@code {
Data[] data =
Contact[] data =
{
new() {
new Student() {
Key = "1",
Name = "John Brown",
Age = 32,
Address = "New York No. 1 Lake Park",
Grade = "1",
Description = "My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park."
},
new() {
new Student() {
Key = "2",
Name = "Jim Green",
Age = 42,
Address = "London No. 1 Lake Park",
Grade = "2",
Description = "My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park."
},
new() {
new Teacher() {
Key = "3",
Name = "Not Expandable",
Age = 29,
Address = "Jiangsu No. 1 Lake Park",
Description = "This not expandable"
Description = "This not expandable",
Subject = "Chemistry"
},
new() {
new Teacher() {
Key = "4",
Name = "Joe Black",
Age = 32,
Address = "Sidney No. 1 Lake Park",
Description = "My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park."
Description = "My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.",
Subject = "Math"
}
};
public class Data
public abstract class Contact
{
[DisplayName("Key")]
public string Key { get; set; }
@ -71,4 +85,16 @@
[DisplayName("Description")]
public string Description { get; set; }
}
public class Student : Contact
{
[DisplayName("Grade")]
public string Grade { get; set; }
}
public class Teacher : Contact
{
[DisplayName("Subject")]
public string Subject { get; set; }
}
}

View File

@ -7,9 +7,10 @@ title:
## zh-CN
当表格内容较多不能一次性完全展示时。
当表格内容较多不能一次性完全展示时。(本例中用于抽象类的不同实现类的展示)
## en-US
When there's too much information to show and the table can't display all at once.
(Different implementation classes used for abstract classes in this example.)