Skip to content

Commit f5eac6b

Browse files
authored
fix(TableDynamicContext): AddAutoGenerateColumnAttribute parameter Text not work (#6966)
* perf: 优化性能减少一次 new * refactor: 精简代码 * doc: 更新示例 * refactor: 精简代码 * fix: 修复动态时设置 Text 无效问题 * chore: bump version 9.11.4 * refactor: 更新示例
1 parent 1b117f3 commit f5eac6b

File tree

7 files changed

+13
-14
lines changed

7 files changed

+13
-14
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,3 @@
4747
<Table TItem="DynamicObject" DynamicContext="DataTablePageDynamicContext" />
4848
<Pagination PageCount="@PageCount" PageIndex="@PageIndex" OnPageLinkClick="@OnPageLinkClick" class="mt-3" />
4949
</DemoBlock>
50-
51-

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,25 @@ private void CreateContext()
5151
{
5252
context.AddRequiredAttribute(nameof(Foo.DateTime));
5353
// 使用 AutoGenerateColumnAttribute 设置显示名称示例
54-
context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair<string, object?>[] { new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) });
54+
context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair<string, object?>[] { new(nameof(AutoGenerateColumnAttribute.Text), LocalizerFoo[nameof(Foo.DateTime)].Value) });
5555
}
5656
else if (propertyName == nameof(Foo.Name))
5757
{
58-
context.AddRequiredAttribute(nameof(Foo.Name), Localizer["Name.Required"]);
58+
context.AddRequiredAttribute(nameof(Foo.Name), LocalizerFoo["Name.Required"]);
5959
// 使用 Text 设置显示名称示例
60-
col.Text = Localizer[nameof(Foo.Name)];
60+
col.Text = LocalizerFoo[nameof(Foo.Name)];
6161
}
6262
else if (propertyName == nameof(Foo.Count))
6363
{
6464
context.AddRequiredAttribute(nameof(Foo.Count));
6565
// 使用 DisplayNameAttribute 设置显示名称示例
66-
context.AddDisplayNameAttribute(nameof(Foo.Count), Localizer[nameof(Foo.Count)].Value);
66+
context.AddDisplayNameAttribute(nameof(Foo.Count), LocalizerFoo[nameof(Foo.Count)].Value);
6767
}
6868
else if (propertyName == nameof(Foo.Complete))
6969
{
7070
col.Filterable = true;
7171
// 使用 DisplayAttribute 设置显示名称示例
72-
context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair<string, object?>[] { new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value) });
72+
context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair<string, object?>[] { new(nameof(DisplayAttribute.Name), LocalizerFoo[nameof(Foo.Complete)].Value) });
7373
}
7474
else if (propertyName == nameof(Foo.Id))
7575
{

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.11.3-beta06</Version>
4+
<Version>9.11.4</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private List<ITableColumn> InternalGetColumns()
164164
var ret = new List<ITableColumn>();
165165
foreach (DataColumn col in DataTable.Columns)
166166
{
167-
ret.Add(new InternalTableColumn(col.ColumnName, col.DataType, col.Caption));
167+
ret.Add(new InternalTableColumn(col.ColumnName, col.DataType));
168168
}
169169
return ret;
170170
}

src/BootstrapBlazor/Dynamic/DynamicObjectContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ public void AddAttribute(string columnName, Type attributeType, Type[] types, ob
4545
var attr = attributeType.GetConstructor(types);
4646
if (attr != null)
4747
{
48-
var cab = new CustomAttributeBuilder(attr, constructorArgs, namedProperties: propertyInfos ?? [], propertyValues: propertyValues ?? []);
49-
CustomerAttributeBuilderCache.AddOrUpdate(columnName, key => [cab], (key, builders) =>
48+
CustomerAttributeBuilderCache.AddOrUpdate(columnName, key => [CreateCustomAttributeBuilder()], (key, builders) =>
5049
{
51-
builders.Add(cab);
50+
builders.Add(CreateCustomAttributeBuilder());
5251
return builders;
5352
});
5453
}
54+
55+
CustomAttributeBuilder CreateCustomAttributeBuilder() => new(attr, constructorArgs, propertyInfos ?? [], propertyValues ?? []);
5556
}
5657

5758
/// <summary>

src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void AddMultipleParameterAttribute<TAttribute>(this DynamicObjectC
6767
propertyValues.Add(kv.Value);
6868
}
6969
}
70-
context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty<object>(), propertyInfos.ToArray(), propertyValues.ToArray());
70+
context.AddAttribute(columnName, type, Type.EmptyTypes, [], [.. propertyInfos], [.. propertyValues]);
7171
}
7272

7373
/// <summary>

src/BootstrapBlazor/Utils/EmitHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static void CreateProperty(this TypeBuilder typeBuilder, string property
6767
propertyId.SetGetMethod(methodGetField);
6868
propertyId.SetSetMethod(methodSetField);
6969

70-
foreach (var cab in attributeBuilds ?? Enumerable.Empty<CustomAttributeBuilder>())
70+
foreach (var cab in attributeBuilds ?? [])
7171
{
7272
propertyId.SetCustomAttribute(cab);
7373
}

0 commit comments

Comments
 (0)