Skip to content

Commit f11bd8e

Browse files
authored
feat(TableColumn): remove Formatter on TableColumn (#4892)
* refactor: 移除列格式化操作日志 * doc: 更新示例 * test: 更新单元测试 * refactor: 精简逻辑 * test: 更新单元测试 * refactor: 删除冗余代码 * test: 更新单元测试 * revert: 撤销更新文档 * doc: 更新示例 * doc: 精简代码 * refactor: 精简代码 * refactor: 移除可为空标记 * refactor: 代码重构 * refactor: 支持异步 Formatter 方法 * refactor: 撤销代码更改 * doc: 更新示例 * refactor: 撤销代码更改 * refactor: 精简代码 * test: 更新单元测试
1 parent 7de5f7e commit f11bd8e

File tree

17 files changed

+131
-139
lines changed

17 files changed

+131
-139
lines changed

src/BootstrapBlazor.Server/Components/Samples/DockViews/BaseDockView.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ public class TreeFoo : Foo
147147
ParentId = parentId,
148148
Name = localizer["Foo.Name", $"{id + i:d4}"],
149149
DateTime = System.DateTime.Now.AddDays(i - 1),
150-
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
151-
Count = Random.Next(1, 100),
152-
Complete = Random.Next(1, 100) > 50,
153-
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
150+
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
151+
Count = Random.Shared.Next(1, 100),
152+
Complete = Random.Shared.Next(1, 100) > 50,
153+
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
154154
}).ToList();
155155
}
156156
}

src/BootstrapBlazor.Server/Components/Samples/DockViews2/BaseDockView.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ public class TreeFoo : Foo
141141
ParentId = parentId,
142142
Name = localizer["Foo.Name", $"{id + i:d4}"],
143143
DateTime = System.DateTime.Now.AddDays(i - 1),
144-
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
145-
Count = Random.Next(1, 100),
146-
Complete = Random.Next(1, 100) > 50,
147-
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
144+
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
145+
Count = Random.Shared.Next(1, 100),
146+
Complete = Random.Shared.Next(1, 100) > 50,
147+
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
148148
}).ToList();
149149
}
150150
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
<TableColumn @bind-Field="@context.DateTime" Width="120" FormatString="yyyy-MM-dd" Align="Alignment.Center" />
147147
<TableColumn @bind-Field="@context.Name" Width="100" />
148148
<TableColumn @bind-Field="@context.Address" />
149-
<TableColumn @bind-Field="@context.Count" Formatter="@IntFormatter" Width="60" Align="Alignment.Right" />
149+
<TableColumn @bind-Field="@context.Count" Width="60" Align="Alignment.Right" />
150150
</TableColumns>
151151
</Table>
152152
</DemoBlock>

src/BootstrapBlazor.Server/Components/Samples/Table/TablesColumn.razor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ protected override void OnInitialized()
4141
/// </summary>
4242
/// <param name="d"></param>
4343
/// <returns></returns>
44-
private static Task<string> IntFormatter(object? d)
44+
private static Task<string?> IntFormatter(object d)
4545
{
46-
var ret = "";
46+
string? ret = null;
4747
if (d is TableColumnContext<Foo, object?> data && data.Value != null)
4848
{
4949
var val = (int)data.Value;
50-
ret = val.ToString("0.00");
50+
ret = $"Sales: {val:0.00}";
5151
}
5252
return Task.FromResult(ret);
5353
}
@@ -58,7 +58,7 @@ private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
5858

5959
// 先处理过滤再处理排序 提高性能
6060
var isFiltered = false;
61-
if (options.Filters.Any())
61+
if (options.Filters.Count != 0)
6262
{
6363
items = items.Where(options.Filters.GetFilterFunc<Foo>());
6464
isFiltered = true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<TableColumn @bind-Field="@context.DateTime" Width="120" FormatString="yyyy-MM-dd" Align="Alignment.Center" />
6363
<TableColumn @bind-Field="@context.Name" Width="100" />
6464
<TableColumn @bind-Field="@context.Address" ShownWithBreakPoint="BreakPoint.Medium" />
65-
<TableColumn @bind-Field="@context.Count" ShownWithBreakPoint="BreakPoint.Large" Formatter="@IntFormatter" Width="60" />
65+
<TableColumn @bind-Field="@context.Count" ShownWithBreakPoint="BreakPoint.Large" Width="60" />
6666
</TableColumns>
6767
</Table>
6868
</DemoBlock>

src/BootstrapBlazor.Server/Components/Samples/Table/TablesColumnList.razor.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@ public partial class TablesColumnList
2727
[NotNull]
2828
private Table<Foo>? TableColumnVisible { get; set; }
2929

30-
/// <summary>
31-
/// IntFormatter
32-
/// </summary>
33-
/// <param name = "d"></param>
34-
/// <returns></returns>
35-
private static Task<string> IntFormatter(object? d)
36-
{
37-
var ret = "";
38-
if (d is TableColumnContext<Foo, object?> data && data.Value != null)
39-
{
40-
var val = (int)data.Value;
41-
ret = val.ToString("0.00");
42-
}
43-
44-
return Task.FromResult(ret);
45-
}
46-
4730
/// <summary>
4831
/// OnInitialized 方法
4932
/// </summary>

src/BootstrapBlazor.Server/Components/Samples/Table/TablesTree.razor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ private class TreeFoo : Foo
125125
ParentId = parentId,
126126
Name = localizer["Foo.Name", $"{id + i:d4}"],
127127
DateTime = System.DateTime.Now.AddDays(i - 1),
128-
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
129-
Count = Random.Next(1, 100),
130-
Complete = Random.Next(1, 100) > 50,
131-
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
128+
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
129+
Count = Random.Shared.Next(1, 100),
130+
Complete = Random.Shared.Next(1, 100) > 50,
131+
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
132132
}).ToList();
133133
}
134134
}

src/BootstrapBlazor.Server/Data/Foo.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ public class Foo
8787
public int ReadonlyColumn { get; init; }
8888

8989
#region Static methods
90-
/// <summary>
91-
/// 随机数 Random 实例
92-
/// </summary>
93-
protected static readonly Random Random = new();
94-
9590
/// <summary>
9691
/// 生成Foo类,随机数据
9792
/// Generate Foo class, random data
@@ -103,10 +98,10 @@ public class Foo
10398
Id = 1,
10499
Name = localizer["Foo.Name", "1000"],
105100
DateTime = System.DateTime.Now,
106-
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
107-
Count = Random.Next(1, 100),
108-
Complete = Random.Next(1, 100) > 50,
109-
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
101+
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
102+
Count = Random.Shared.Next(1, 100),
103+
Complete = Random.Shared.Next(1, 100) > 50,
104+
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle
110105
};
111106

112107
/// <summary>
@@ -119,11 +114,11 @@ public class Foo
119114
Id = i,
120115
Name = localizer["Foo.Name", $"{i:d4}"],
121116
DateTime = System.DateTime.Now.AddDays(i - 1),
122-
Address = localizer["Foo.Address", $"{Random.Next(1000, 2000)}"],
123-
Count = Random.Next(1, 100),
124-
Complete = Random.Next(1, 100) > 50,
125-
Education = Random.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle,
126-
ReadonlyColumn = Random.Next(10, 50)
117+
Address = localizer["Foo.Address", $"{Random.Shared.Next(1000, 2000)}"],
118+
Count = Random.Shared.Next(1, 100),
119+
Complete = Random.Shared.Next(1, 100) > 50,
120+
Education = Random.Shared.Next(1, 100) > 50 ? EnumEducation.Primary : EnumEducation.Middle,
121+
ReadonlyColumn = Random.Shared.Next(10, 50)
127122
}).ToList();
128123

129124
/// <summary>
@@ -162,7 +157,7 @@ public static List<SelectedItem> GetCompleteItems(IStringLocalizer<Foo> localize
162157
/// 通过 Id 获取 Title
163158
/// </summary>
164159
/// <returns></returns>
165-
private static string GetTitle() => Random.Next(1, 80) switch
160+
private static string GetTitle() => Random.Shared.Next(1, 80) switch
166161
{
167162
>= 1 and < 10 => "Clerk",
168163
>= 10 and < 50 => "Engineer",

src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu
158158
/// <summary>
159159
/// <inheritdoc/>
160160
/// </summary>
161-
public Func<object?, Task<string?>>? Formatter { get; set; }
161+
public Func<object, Task<string?>>? Formatter { get; set; }
162162

163163
/// <summary>
164164
/// 获得/设置 编辑模板

src/BootstrapBlazor/Components/Table/ITableColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public interface ITableColumn : IEditorItem
128128
/// <summary>
129129
/// 获得/设置 列格式化回调委托 <see cref="TableColumnContext{TItem, TValue}"/>
130130
/// </summary>
131-
Func<object?, Task<string?>>? Formatter { get; set; }
131+
Func<object, Task<string?>>? Formatter { get; set; }
132132

133133
/// <summary>
134134
/// 获得/设置 文字对齐方式 默认为 null 使用 Alignment.None

0 commit comments

Comments
 (0)