-
-
Notifications
You must be signed in to change notification settings - Fork 385
doc(IDynamicObject): update table dynamic data sample #7289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
@@ -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", | ||
|
|
@@ -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; | ||
| } | ||
| ret.Add(columnName, value); | ||
| } | ||
| return ret; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: The index parameter of GenerateDynamicRowData is currently unused. Since 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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
iinstead of the rowindex. This means all rows will have the same boolean pattern based on the StaticColumnList position, not varying per row. Consider changing tovalue = index % 2 == 0;to make the boolean value dependent on the row number.