1- // Licensed to the .NET Foundation under one or more agreements.
1+ // Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the Apache 2.0 License
33// See the LICENSE file in the project root for more information.
44// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -10,8 +10,8 @@ namespace BootstrapBlazor.Server.Components.Samples.Table;
1010/// </summary>
1111public partial class TablesDynamicObject
1212{
13- [ NotNull ]
14- private IEnumerable < CustomDynamicColumnsObjectData > ? CustomDynamicItems { get ; set ; }
13+ private IEnumerable < CustomDynamicColumnsObjectData > _customDynamicItems = [ ] ;
14+
1515 private static List < string > StaticColumnList =>
1616 [
1717 "A" ,
@@ -20,28 +20,71 @@ public partial class TablesDynamicObject
2020 "Z"
2121 ] ;
2222
23- [ NotNull ]
24- private List < string > ? DynamicColumnList { get ; set ; }
23+ private List < string > _dynamicColumnList = [ ] ;
2524
2625 /// <summary>
27- /// OnInitialized
26+ /// <inheritdoc/>
2827 /// </summary>
2928 protected override void OnInitialized ( )
3029 {
3130 base . OnInitialized ( ) ;
31+
3232 // 构造动态列
3333 var now = DateTime . Now ;
34- DynamicColumnList = Enumerable . Range ( 1 , 5 ) . Select ( index => now . AddMinutes ( - 1 * index ) . ToString ( "HH:mm" ) ) . ToList ( ) ;
35- CustomDynamicItems = Enumerable . Range ( 1 , 10 ) . Select ( index => new CustomDynamicColumnsObjectData ( index . ToString ( ) , StaticColumnList . ToDictionary ( d => d , d => ( object ? ) $ "{ d } { index } ") ) ) ;
34+ _dynamicColumnList = Enumerable . Range ( 1 , 5 ) . Select ( index => now . AddMinutes ( - 1 * index ) . ToString ( "HH:mm" ) ) . ToList ( ) ;
35+ _customDynamicItems = GenerateDynamicColumnsObjectData ( ) ;
36+ }
37+
38+ private static IEnumerable < CustomDynamicColumnsObjectData > GenerateDynamicColumnsObjectData ( ) => Enumerable . Range ( 1 , 10 )
39+ . Select ( index => new CustomDynamicColumnsObjectData ( index . ToString ( ) , GenerateRowData ( index ) ) ) ;
40+
41+ private static Dictionary < string , object ? > GenerateRowData ( int index )
42+ {
43+ var ret = new Dictionary < string , object ? > ( ) ;
44+ for ( int i = 0 ; i < StaticColumnList . Count ; i ++ )
45+ {
46+ var columnName = StaticColumnList [ i ] ;
47+ object ? value = null ;
48+ if ( columnName == "A" )
49+ {
50+ value = $ "Template - A{ index } ";
51+ }
52+ else if ( columnName == "B" )
53+ {
54+ value = index * 100 ;
55+ }
56+ else if ( columnName == "C" )
57+ {
58+ value = DateTime . Now . AddDays ( index ) ;
59+ }
60+ else if ( columnName == "Z" )
61+ {
62+ value = i % 2 == 0 ;
63+ }
64+ ret . Add ( columnName , value ) ;
65+ }
66+ return ret ;
3667 }
3768
3869 private readonly static Random random = new ( ) ;
3970
4071 private Task < QueryData < CustomDynamicData > > OnQueryAsync ( QueryPageOptions options )
4172 {
42- var items = Enumerable . Range ( 1 , 10 ) . Select ( index => new CustomDynamicData ( index . ToString ( ) , DynamicColumnList . ToDictionary ( d => d . ToString ( ) , d => $ " { random . Next ( 1000 , 9999 ) } " ) ) ) ;
73+ var items = Enumerable . Range ( 1 , 10 ) . Select ( index => new CustomDynamicData ( index . ToString ( ) , GenerateDynamicRowData ( index ) ) ) ;
4374 // sort logic ...
4475 // filter logic ...
4576 return Task . FromResult ( new QueryData < CustomDynamicData > ( ) { Items = items , TotalCount = 10 , IsSorted = true , IsFiltered = true } ) ;
4677 }
78+
79+ private Dictionary < string , object ? > GenerateDynamicRowData ( int index )
80+ {
81+ var ret = new Dictionary < string , object ? > ( ) ;
82+ for ( int i = 0 ; i < _dynamicColumnList . Count ; i ++ )
83+ {
84+ var columnName = _dynamicColumnList [ i ] ;
85+ object ? value = random . Next ( 1000 , 9999 ) ;
86+ ret . Add ( columnName , value ) ;
87+ }
88+ return ret ;
89+ }
4790}
0 commit comments