Skip to content

Commit 4201004

Browse files
authored
doc(IDynamicObject): update table dynamic data sample (#7289)
1 parent c941dc8 commit 4201004

3 files changed

Lines changed: 62 additions & 27 deletions

File tree

src/BootstrapBlazor.Server/Components/Samples/Table/TablesDynamicObject.razor

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/table/dynamic-object"
1+
@page "/table/dynamic-object"
22
@inject IStringLocalizer<NavMenu> NavMenuLocalizer
33
@inject IStringLocalizer<TablesDynamicObject> Localizer
44

@@ -12,13 +12,9 @@
1212
IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
1313
<TableColumns>
1414
<TableColumn @bind-Field="@context.Fix" Sortable="true" Filterable="true" />
15-
@foreach (var element in DynamicColumnList)
15+
@foreach (var element in _dynamicColumnList)
1616
{
17-
<TableColumn Field="@element.ToString()" FieldName="@element.ToString()" Text="@element" Sortable="true" Filterable="true">
18-
<Template Context="v">
19-
<div>Template - @v.Value</div>
20-
</Template>
21-
</TableColumn>
17+
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true"></TableColumn>
2218
}
2319
</TableColumns>
2420
</Table>
@@ -27,17 +23,13 @@
2723
<DemoBlock Title="@Localizer["TablesDynamicObjectIDynamicObjectTitle"]"
2824
Introduction="@Localizer["TablesDynamicObjectIDynamicObjectIntro"]"
2925
Name="IDynamicObject">
30-
<Table TItem="CustomDynamicColumnsObjectData" Items="CustomDynamicItems"
26+
<Table TItem="CustomDynamicColumnsObjectData" Items="_customDynamicItems"
3127
IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
3228
<TableColumns>
3329
<TableColumn @bind-Field="@context.Fix" Sortable="true" Filterable="true" />
3430
@foreach (var element in StaticColumnList)
3531
{
36-
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true">
37-
<Template Context="v">
38-
<div>Template - @v.Value</div>
39-
</Template>
40-
</TableColumn>
32+
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true"></TableColumn>
4133
}
4234
</TableColumns>
4335
</Table>
Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -10,8 +10,8 @@ namespace BootstrapBlazor.Server.Components.Samples.Table;
1010
/// </summary>
1111
public partial class TablesDynamicObject
1212
{
13-
[NotNull]
14-
private IEnumerable<CustomDynamicColumnsObjectData>? CustomDynamicItems { get; set; }
13+
private IEnumerable<CustomDynamicColumnsObjectData> _customDynamicItems = [];
14+
1515
private static List<string> StaticColumnList =>
1616
[
1717
"A",
@@ -20,28 +20,71 @@ public partial class TablesDynamicObject
2020
"Z"
2121
];
2222

23-
[NotNull]
24-
private List<string>? DynamicColumnList { get; set; }
23+
private List<string> _dynamicColumnList = [];
2524

2625
/// <summary>
27-
/// OnInitialized
26+
/// <inheritdoc/>
2827
/// </summary>
2928
protected override void OnInitialized()
3029
{
3130
base.OnInitialized();
31+
3232
// 构造动态列
3333
var now = DateTime.Now;
34-
DynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
35-
CustomDynamicItems = Enumerable.Range(1, 10).Select(index => new CustomDynamicColumnsObjectData(index.ToString(), StaticColumnList.ToDictionary(d => d, d => (object?)$"{d}{index}")));
34+
_dynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
35+
_customDynamicItems = GenerateDynamicColumnsObjectData();
36+
}
37+
38+
private static IEnumerable<CustomDynamicColumnsObjectData> GenerateDynamicColumnsObjectData() => Enumerable.Range(1, 10)
39+
.Select(index => new CustomDynamicColumnsObjectData(index.ToString(), GenerateRowData(index)));
40+
41+
private static Dictionary<string, object?> GenerateRowData(int index)
42+
{
43+
var ret = new Dictionary<string, object?>();
44+
for (int i = 0; i < StaticColumnList.Count; i++)
45+
{
46+
var columnName = StaticColumnList[i];
47+
object? value = null;
48+
if (columnName == "A")
49+
{
50+
value = $"Template - A{index}";
51+
}
52+
else if (columnName == "B")
53+
{
54+
value = index * 100;
55+
}
56+
else if (columnName == "C")
57+
{
58+
value = DateTime.Now.AddDays(index);
59+
}
60+
else if (columnName == "Z")
61+
{
62+
value = i % 2 == 0;
63+
}
64+
ret.Add(columnName, value);
65+
}
66+
return ret;
3667
}
3768

3869
private readonly static Random random = new();
3970

4071
private Task<QueryData<CustomDynamicData>> OnQueryAsync(QueryPageOptions options)
4172
{
42-
var items = Enumerable.Range(1, 10).Select(index => new CustomDynamicData(index.ToString(), DynamicColumnList.ToDictionary(d => d.ToString(), d => $"{random.Next(1000, 9999)}")));
73+
var items = Enumerable.Range(1, 10).Select(index => new CustomDynamicData(index.ToString(), GenerateDynamicRowData(index)));
4374
// sort logic ...
4475
// filter logic ...
4576
return Task.FromResult(new QueryData<CustomDynamicData>() { Items = items, TotalCount = 10, IsSorted = true, IsFiltered = true });
4677
}
78+
79+
private Dictionary<string, object?> GenerateDynamicRowData(int index)
80+
{
81+
var ret = new Dictionary<string, object?>();
82+
for (int i = 0; i < _dynamicColumnList.Count; i++)
83+
{
84+
var columnName = _dynamicColumnList[i];
85+
object? value = random.Next(1000, 9999);
86+
ret.Add(columnName, value);
87+
}
88+
return ret;
89+
}
4790
}

src/BootstrapBlazor.Server/Data/CustomDynamicData.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -20,14 +20,14 @@ public class CustomDynamicData : System.Dynamic.DynamicObject
2020
/// <summary>
2121
/// 存储每列值信息 Key 列名 Value 为列值
2222
/// </summary>
23-
public Dictionary<string, string> Columns { get; set; }
23+
public Dictionary<string, object?> Columns { get; set; }
2424

2525
/// <summary>
2626
///
2727
/// </summary>
2828
/// <param name="fix"></param>
2929
/// <param name="data"></param>
30-
public CustomDynamicData(string fix, Dictionary<string, string> data)
30+
public CustomDynamicData(string fix, Dictionary<string, object?> data)
3131
{
3232
Fix = fix;
3333
Columns = data;
@@ -50,9 +50,9 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
5050
{
5151
result = Fix;
5252
}
53-
else if (Columns.ContainsKey(binder.Name))
53+
else if (Columns.TryGetValue(binder.Name, out object? value))
5454
{
55-
result = Columns[binder.Name];
55+
result = value;
5656
}
5757
else
5858
{

0 commit comments

Comments
 (0)