Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/table/dynamic-object"
@page "/table/dynamic-object"
@inject IStringLocalizer<NavMenu> NavMenuLocalizer
@inject IStringLocalizer<TablesDynamicObject> Localizer

Expand All @@ -12,13 +12,9 @@
IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
<TableColumns>
<TableColumn @bind-Field="@context.Fix" Sortable="true" Filterable="true" />
@foreach (var element in DynamicColumnList)
@foreach (var element in _dynamicColumnList)
{
<TableColumn Field="@element.ToString()" FieldName="@element.ToString()" Text="@element" Sortable="true" Filterable="true">
<Template Context="v">
<div>Template - @v.Value</div>
</Template>
</TableColumn>
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true"></TableColumn>
}
</TableColumns>
</Table>
Expand All @@ -27,17 +23,13 @@
<DemoBlock Title="@Localizer["TablesDynamicObjectIDynamicObjectTitle"]"
Introduction="@Localizer["TablesDynamicObjectIDynamicObjectIntro"]"
Name="IDynamicObject">
<Table TItem="CustomDynamicColumnsObjectData" Items="CustomDynamicItems"
<Table TItem="CustomDynamicColumnsObjectData" Items="_customDynamicItems"
IsStriped="true" IsBordered="true" ShowToolbar="true" ShowColumnList="true" ShowDefaultButtons="false" ShowRefresh="false">
<TableColumns>
<TableColumn @bind-Field="@context.Fix" Sortable="true" Filterable="true" />
@foreach (var element in StaticColumnList)
{
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true">
<Template Context="v">
<div>Template - @v.Value</div>
</Template>
</TableColumn>
<TableColumn Field="@element" FieldName="@element" Text="@element" Sortable="true" Filterable="true"></TableColumn>
}
</TableColumns>
</Table>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
Expand All @@ -10,8 +10,8 @@ namespace BootstrapBlazor.Server.Components.Samples.Table;
/// </summary>
public partial class TablesDynamicObject
{
[NotNull]
private IEnumerable<CustomDynamicColumnsObjectData>? CustomDynamicItems { get; set; }
private IEnumerable<CustomDynamicColumnsObjectData> _customDynamicItems = [];

private static List<string> StaticColumnList =>
[
"A",
Expand All @@ -20,28 +20,71 @@ public partial class TablesDynamicObject
"Z"
];

[NotNull]
private List<string>? DynamicColumnList { get; set; }
private List<string> _dynamicColumnList = [];

/// <summary>
/// OnInitialized
/// <inheritdoc/>
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

// 构造动态列
var now = DateTime.Now;
DynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
CustomDynamicItems = Enumerable.Range(1, 10).Select(index => new CustomDynamicColumnsObjectData(index.ToString(), StaticColumnList.ToDictionary(d => d, d => (object?)$"{d}{index}")));
_dynamicColumnList = Enumerable.Range(1, 5).Select(index => now.AddMinutes(-1 * index).ToString("HH:mm")).ToList();
_customDynamicItems = GenerateDynamicColumnsObjectData();
}

private static IEnumerable<CustomDynamicColumnsObjectData> GenerateDynamicColumnsObjectData() => Enumerable.Range(1, 10)
.Select(index => new CustomDynamicColumnsObjectData(index.ToString(), GenerateRowData(index)));

private static Dictionary<string, object?> GenerateRowData(int index)
{
var ret = new Dictionary<string, object?>();
for (int i = 0; i < StaticColumnList.Count; i++)
{
var columnName = StaticColumnList[i];
object? value = null;
if (columnName == "A")
{
value = $"Template - A{index}";
}
else if (columnName == "B")
{
value = index * 100;
}
else if (columnName == "C")
{
value = DateTime.Now.AddDays(index);
}
else if (columnName == "Z")
{
value = i % 2 == 0;
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boolean value is calculated using the loop counter i instead of the row index. This means all rows will have the same boolean pattern based on the StaticColumnList position, not varying per row. Consider changing to value = index % 2 == 0; to make the boolean value dependent on the row number.

Suggested change
value = i % 2 == 0;
value = index % 2 == 0;

Copilot uses AI. Check for mistakes.
}
ret.Add(columnName, value);
}
return ret;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The index parameter of GenerateDynamicRowData is currently unused.

Since index isn’t used, the signature and call GenerateDynamicRowData(index) are misleading. Either use index in the generated data if it’s meant to influence the row, or remove the parameter and adjust the call site so it’s clear the row data doesn’t depend on the row index.

Suggested implementation:

        var items = Enumerable.Range(1, 10).Select(index => new CustomDynamicData(index.ToString(), GenerateDynamicRowData()));
    private Dictionary<string, object?> GenerateDynamicRowData()

None required beyond this file, as the only visible call site has been updated and the removed parameter was unused in the method body.

}

private readonly static Random random = new();

private Task<QueryData<CustomDynamicData>> OnQueryAsync(QueryPageOptions options)
{
var items = Enumerable.Range(1, 10).Select(index => new CustomDynamicData(index.ToString(), DynamicColumnList.ToDictionary(d => d.ToString(), d => $"{random.Next(1000, 9999)}")));
var items = Enumerable.Range(1, 10).Select(index => new CustomDynamicData(index.ToString(), GenerateDynamicRowData(index)));
// sort logic ...
// filter logic ...
return Task.FromResult(new QueryData<CustomDynamicData>() { Items = items, TotalCount = 10, IsSorted = true, IsFiltered = true });
}

private Dictionary<string, object?> GenerateDynamicRowData(int index)
{
var ret = new Dictionary<string, object?>();
for (int i = 0; i < _dynamicColumnList.Count; i++)
{
var columnName = _dynamicColumnList[i];
object? value = random.Next(1000, 9999);
ret.Add(columnName, value);
}
return ret;
}
}
10 changes: 5 additions & 5 deletions src/BootstrapBlazor.Server/Data/CustomDynamicData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
Expand All @@ -20,14 +20,14 @@ public class CustomDynamicData : System.Dynamic.DynamicObject
/// <summary>
/// 存储每列值信息 Key 列名 Value 为列值
/// </summary>
public Dictionary<string, string> Columns { get; set; }
public Dictionary<string, object?> Columns { get; set; }

/// <summary>
///
/// </summary>
/// <param name="fix"></param>
/// <param name="data"></param>
public CustomDynamicData(string fix, Dictionary<string, string> data)
public CustomDynamicData(string fix, Dictionary<string, object?> data)
{
Fix = fix;
Columns = data;
Expand All @@ -50,9 +50,9 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
result = Fix;
}
else if (Columns.ContainsKey(binder.Name))
else if (Columns.TryGetValue(binder.Name, out object? value))
{
result = Columns[binder.Name];
result = value;
}
else
{
Expand Down
Loading