1- // Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3- // Website: https://www.blazor.zone or https://argozhang.github.io/
4-
5- using System . Data ;
6-
7- namespace BootstrapBlazor . Shared . Samples . Table ;
8-
9- /// <summary>
10- /// 动态表格示例
11- /// </summary>
12- public partial class TablesDynamic
13- {
1+ @using System .Data ;
2+ @using BootstrapBlazor .Shared .Samples .Table ;
3+ @inject IStringLocalizer <Foo > FooLocalizer
4+ @inject IStringLocalizer <TablesDynamicDynamicCol > Localizer
5+
6+ <div >
7+ <Table TItem =" DynamicObject" DynamicContext =" DataTableDynamicContext" ModelEqualityComparer =" ModelEqualityComparer"
8+ IsMultipleSelect =" true" IsBordered =" true" IsStriped =" true"
9+ ShowToolbar =" true" ShowExtendButtons =" true" >
10+ <TableToolbarTemplate >
11+ <TableToolbarButton TItem =" DynamicObject" Color =" Color.Info" Icon =" fa-fw fa-solid fa-circle-plus" Text =" @ButtonAddColumnText" OnClick =" OnAddColumn" />
12+ <TableToolbarButton TItem =" DynamicObject" Color =" Color.Secondary" Icon =" fa-fw fa-solid fa-circle-minus" Text =" @ButtonRemoveColumnText" OnClick =" OnRemoveColumn" />
13+ </TableToolbarTemplate >
14+ </Table >
15+ </div >
16+
17+ @code {
1418 [NotNull ]
1519 private DataTableDynamicContext ? DataTableDynamicContext { get ; set ; }
1620
17- [ NotNull ]
18- private DataTableDynamicContext ? DataTablePageDynamicContext { get ; set ; }
19-
2021 private DataTable UserData { get ; } = new DataTable ();
2122
22- [ Inject ]
23- [ NotNull ]
24- private IStringLocalizer < Foo > ? Localizer { get ; set ; }
25-
26- [ Inject ]
27- [ NotNull ]
28- private IStringLocalizer < Tables > ? TablesLocalizer { get ; set ; }
29-
30- [ Inject ]
31- [ NotNull ]
32- private IStringLocalizer < TablesDynamic > ? DynamicLocalizer { get ; set ; }
33-
3423 private string ? ButtonAddColumnText { get ; set ; }
3524
3625 private string ? ButtonRemoveColumnText { get ; set ; }
3726
38- private List < DynamicObject > SelectedItems { get ; set ; } = new List < DynamicObject > ( ) ;
39-
4027 /// <summary >
4128 /// OnInitialized 方法
4229 /// </summary >
4330 protected override void OnInitialized ()
4431 {
4532 base .OnInitialized ();
4633
47- ButtonAddColumnText ??= TablesLocalizer [ nameof ( ButtonAddColumnText ) ] ;
48- ButtonRemoveColumnText ??= TablesLocalizer [ nameof ( ButtonRemoveColumnText ) ] ;
34+ ButtonAddColumnText ??= Localizer [ " TablesDynamicDynamicColButtonAddColumnText " ];
35+ ButtonRemoveColumnText ??= Localizer [ " TablesDynamicDynamicColButtonRemoveColumnText " ];
4936
5037 // 初始化 DataTable
5138 InitDataTable ();
@@ -68,28 +55,28 @@ private void CreateContext()
6855 // 使用 AutoGenerateColumnAttribute 设置显示名称示例
6956 context .AddAutoGenerateColumnAttribute (nameof (Foo .DateTime ), new KeyValuePair <string , object ?>[]
7057 {
71- new ( nameof ( AutoGenerateColumnAttribute . Text ) , Localizer [ nameof ( Foo . DateTime ) ] . Value )
58+ new (nameof (AutoGenerateColumnAttribute .Text ), FooLocalizer [nameof (Foo .DateTime )].Value )
7259 });
7360 }
7461 else if (propertyName == nameof (Foo .Name ))
7562 {
76- context . AddRequiredAttribute ( nameof ( Foo . Name ) , Localizer [ "Name.Required" ] ) ;
63+ context .AddRequiredAttribute (nameof (Foo .Name ), FooLocalizer [" Name.Required" ]);
7764 // 使用 Text 设置显示名称示例
78- col . Text = Localizer [ nameof ( Foo . Name ) ] ;
65+ col .Text = FooLocalizer [nameof (Foo .Name )];
7966 }
8067 else if (propertyName == nameof (Foo .Count ))
8168 {
8269 context .AddRequiredAttribute (nameof (Foo .Count ));
8370 // 使用 DisplayNameAttribute 设置显示名称示例
84- context . AddDisplayNameAttribute ( nameof ( Foo . Count ) , Localizer [ nameof ( Foo . Count ) ] . Value ) ;
71+ context .AddDisplayNameAttribute (nameof (Foo .Count ), FooLocalizer [nameof (Foo .Count )].Value );
8572 }
8673 else if (propertyName == nameof (Foo .Complete ))
8774 {
8875 col .Filterable = true ;
8976 // 使用 DisplayAttribute 设置显示名称示例
9077 context .AddDisplayAttribute (nameof (Foo .Complete ), new KeyValuePair <string , object ?>[]
9178 {
92- new ( nameof ( DisplayAttribute . Name ) , Localizer [ nameof ( Foo . Complete ) ] . Value )
79+ new (nameof (DisplayAttribute .Name ), FooLocalizer [nameof (Foo .Complete )].Value )
9380 });
9481 }
9582 else if (propertyName == nameof (Foo .Id ))
@@ -139,7 +126,7 @@ private void InitDataTable()
139126 UserData .PrimaryKey = new DataColumn [] { UserData .Columns [0 ] };
140127 UserData .Columns [0 ].AutoIncrement = true ;
141128
142- Foo . GenerateFoo ( Localizer , 10 ) . ForEach ( f =>
129+ Foo .GenerateFoo (FooLocalizer , 10 ).ForEach (f =>
143130 {
144131 UserData .Rows .Add (f .Id , f .DateTime , f .Name , f .Count );
145132 });
@@ -154,7 +141,7 @@ private Task OnAddColumn()
154141 UserData .Columns .Add (nameof (Foo .Complete ), typeof (bool ));
155142
156143 // 更新数据
157- var fs = Foo . GenerateFoo ( Localizer , 10 ) ;
144+ var fs = Foo .GenerateFoo (FooLocalizer , 10 );
158145 for (var i = 0 ; i < fs .Count ; i ++ )
159146 {
160147 UserData .Rows [i ][nameof (Foo .Complete )] = fs [i ].Complete ;
@@ -196,7 +183,7 @@ private void InitPageDataTable()
196183 PageDataTable .Columns .Add (nameof (Foo .Name ), typeof (string ));
197184 PageDataTable .Columns .Add (nameof (Foo .Count ), typeof (int ));
198185
199- PageFoos = Foo . GenerateFoo ( Localizer , 80 ) ;
186+ PageFoos = Foo .GenerateFoo (FooLocalizer , 80 );
200187 TotalCount = PageFoos .Count ;
201188
202189 PageIndex = 1 ;
@@ -215,49 +202,5 @@ private void RebuildPageDataTable()
215202 PageDataTable .Rows .Add (f .Id , f .DateTime , f .Name , f .Count );
216203 }
217204 PageDataTable .AcceptChanges ();
218-
219- DataTablePageDynamicContext = new DataTableDynamicContext ( PageDataTable , ( context , col ) =>
220- {
221- var propertyName = col . GetFieldName ( ) ;
222- if ( propertyName == nameof ( Foo . DateTime ) )
223- {
224- context . AddRequiredAttribute ( nameof ( Foo . DateTime ) ) ;
225- // 使用 AutoGenerateColumnAttribute 设置显示名称示例
226- context . AddAutoGenerateColumnAttribute ( nameof ( Foo . DateTime ) , new KeyValuePair < string , object ? > [ ]
227- {
228- new ( nameof ( AutoGenerateColumnAttribute . Text ) , Localizer [ nameof ( Foo . DateTime ) ] . Value )
229- } ) ;
230- }
231- else if ( propertyName == nameof ( Foo . Name ) )
232- {
233- context . AddRequiredAttribute ( nameof ( Foo . Name ) , Localizer [ "Name.Required" ] ) ;
234- // 使用 Text 设置显示名称示例
235- col . Text = Localizer [ nameof ( Foo . Name ) ] ;
236- }
237- else if ( propertyName == nameof ( Foo . Count ) )
238- {
239- context . AddRequiredAttribute ( nameof ( Foo . Count ) ) ;
240- // 使用 DisplayNameAttribute 设置显示名称示例
241- context . AddDisplayNameAttribute ( nameof ( Foo . Count ) , Localizer [ nameof ( Foo . Count ) ] . Value ) ;
242- }
243- else if ( propertyName == nameof ( Foo . Id ) )
244- {
245- col . Editable = false ;
246- col . Visible = false ;
247- }
248- } ) ;
249- }
250-
251- /// <summary>
252- /// 点击页码处理函数
253- /// </summary>
254- /// <param name="pageIndex"></param>
255- /// <returns></returns>
256- private Task OnPageLinkClick ( int pageIndex )
257- {
258- PageIndex = pageIndex ;
259- RebuildPageDataTable ( ) ;
260- StateHasChanged ( ) ;
261- return Task . CompletedTask ;
262205 }
263206}
0 commit comments