-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTableAmeProBase.cs
1455 lines (1281 loc) · 45.9 KB
/
TableAmeProBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// **********************************
// Densen Informatica 中讯科技
// 作者:Alex Chow
// e-mail:zhouchuanglin@gmail.com
// **********************************
using AME;
using BootstrapBlazor.Components;
using Densen.DataAcces.FreeSql;
using Densen.Service;
using FreeSql;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using static AME.EnumsExtensions;
namespace AmeBlazor.Components;
public partial class TableAmeProBase<TItem> : TableAmeBase where TItem : class, new()
{
public TItem? SelectOneItem { get; set; }
[Parameter] public Func<IEnumerable<TItem>, ExportType, Task>? 导出 { get; set; }
[Parameter] public Func<IEnumerable<TItem>, Task>? 批量执行 { get; set; }
[Parameter] public Func<IEnumerable<TItem>, Task>? 升级 { get; set; }
[Parameter] public Func<IEnumerable<TItem>, Task>? 升级II { get; set; }
/// <summary>
/// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
/// </summary>
[Parameter] public Expression<Func<TItem, bool>>? WhereLamda { get; set; }
/// <summary>
/// 获得/设置 明细表弹窗关闭按钮异步回调方法
/// </summary>
[Parameter]
public Func<TItem, Task<bool>>? DetailsDialogCloseAsync { get; set; }
/// <summary>
/// 获得/设置 明细表弹窗保存按钮异步回调方法
/// </summary>
[Parameter]
public Func<TItem, Task<bool>>? DetailsDialogSaveAsync { get; set; }
/// <summary>
/// 获得/设置 编辑弹窗配置类扩展回调方法 新建/编辑弹窗弹出前回调此方法用于设置弹窗配置信息
/// </summary>
[Parameter]
public Action<ITableEditDialogOption<TItem>>? BeforeShowEditDialogCallback { get; set; }
/// <summary>
/// 设置禁用表单内回车自动提交功能
/// </summary>
public void SetDisableAutoSubmitFormByEnter()
{
BeforeShowEditDialogCallback = new Action<ITableEditDialogOption<TItem>>(o => o.DisableAutoSubmitFormByEnter = true);
}
[NotNull]
public Table<TItem>? TableMain { get; set; }
public ISelect<TItem>? SelectCache { get; set; }
public IEnumerable<TItem>? ItemsCache { get; set; }
public FloatPanel? FloatPanelUp { get; set; }
public FloatPanel? FloatPanel { get; set; }
public string? cacheFooterValue;
/// <summary>
/// 获得/设置 Table Footer Content
/// </summary>
[Parameter]
public RenderFragment? FooterContent { get; set; }
/// <summary>
/// 获得/设置 列创建时渲染带小数格式字段为文本框组件, 默认 true
/// </summary>
[Parameter]
[NotNull]
public bool AutoRenderComponentWithLocaleFormat { get; set; } = true;
[Inject]
[NotNull]
public IStringLocalizer<TableAmeBase>? LocalizerAme { get; set; }
#region 继承bb table的设置
[Inject]
[NotNull]
public IStringLocalizer<Table<TItem>>? Localizer { get; set; }
[Inject]
[NotNull]
private IIconTheme? IconTheme { get; set; }
/// <summary>
/// 获得/设置 数据服务
/// </summary>
[Parameter]
public IDataService<TItem>? DataService { get; set; }
/// <summary>
/// 获得/设置 数据集合,适用于无功能时仅做数据展示使用,高级功能时请使用 <see cref="OnQueryAsync"/> 回调委托
/// </summary>
[Parameter]
public IEnumerable<TItem>? Items { get; set; }
/// <summary>
/// 获得/设置 导出按钮异步回调方法
/// </summary>
[Parameter]
public Func<ITableExportDataContext<TItem>, Task<bool>>? OnExportAsync { get; set; }
/// <summary>
/// 获得/设置 是否显示每行的明细行展开图标
/// </summary>
[Parameter]
public Func<TItem, bool> ShowDetailRow { get; set; } = _ => false;
/// <summary>
/// 获得/设置 新建按钮回调方法
/// </summary>
[Parameter]
public Func<TItem, Task<TItem>>? AddAsync { get; set; }
/// <summary>
/// 获得/设置 批量添加按钮回调方法
/// </summary>
[Parameter]
public Func<TItem, Task<bool>>? BatchAddAsync { get; set; }
/// <summary>
/// 获得/设置 编辑按钮回调方法
/// </summary>
[Parameter]
public Func<TItem, Task>? EditAsync { get; set; }
/// <summary>
/// 获得/设置 保存按钮异步回调方法
/// </summary>
[Parameter]
public Func<TItem, ItemChangedType, Task<TItem?>>? SaveAsync { get; set; }
/// <summary>
/// 保存数据后异步回调方法
/// </summary>
[Parameter]
public Func<TItem, ItemChangedType, Task>? AfterSaveAsync { get; set; }
/// <summary>
/// 获得/设置 删除按钮异步回调方法
/// </summary>
[Parameter]
public Func<IEnumerable<TItem>, Task<bool>>? DeleteAsync { get; set; }
/// <summary>
/// 获得/设置 删除后回调委托方法 (SaveModelAsync)
/// </summary>
[Parameter]
[NotNull]
public Func<List<TItem>, Task>? OnAfterDeleteAsync { get; set; }
/// <summary>
/// 获得/设置 保存删除后回调委托方法 (SaveModelAsync)
/// </summary>
[Parameter]
[NotNull]
public Func<Task>? OnAfterModifyAsync { get; set; }
/// <summary>
/// 获得/设置 查询回调方法,用于计算合计之类
/// </summary>
[Parameter]
public Action<ISelect<TItem>>? AfterQueryAsync { get; set; }
/// <summary>
/// 获得/设置 查询回调方法,用于附加获取地理位置之类
/// </summary>
[Parameter]
public Func<IEnumerable<TItem>, Task<IEnumerable<TItem>?>>? AfterQueryCallBackAsync { get; set; }
/// <summary>
/// 获得/设置 单击行回调委托方法
/// </summary>
[Parameter]
public Func<TItem, Task>? OnClickRowCallback { get; set; }
/// <summary>
/// 获得/设置 RowButtonTemplate 实例 此模板生成的按钮默认放置到按钮后面 />
/// </summary>
[Parameter]
public RenderFragment<TItem>? RowButtonTemplate { get; set; }
/// <summary>
/// 获得/设置 EditTemplate 实例
/// </summary>
[Parameter]
[NotNull]
public RenderFragment<TItem>? EditTemplate { get; set; }
/// <summary>
/// 获得/设置 列创建时回调委托方法
/// </summary>
[Parameter]
[NotNull]
public Func<List<ITableColumn>, Task>? OnColumnCreating { get; set; }
/// <summary>
/// 获得/设置 获得高级搜索条件回调方法 默认 null
/// </summary>
[Parameter]
[NotNull]
public Func<PropertyInfo, TItem, List<SearchFilterAction>?>? GetAdvancedSearchFilterCallback { get; set; }
/// <summary>
/// 获得/设置 分页信息内容模板
/// </summary>
[Parameter]
[NotNull]
public RenderFragment? PageInfoBodyTemplate { get; set; }
/// <summary>
/// 获得/设置 明细行模板 <see cref="IsDetails" />
/// </summary>
[Parameter]
public RenderFragment<TItem>? DetailRowTemplate { get; set; }
/// <summary>
/// 获得/设置 TableFooter 实例
/// </summary>
[Parameter]
public RenderFragment<IEnumerable<TItem>>? TableFooter { get; set; }
/// <summary>
/// 获得/设置 Table Footer 模板
/// </summary>
[Parameter]
public RenderFragment<IEnumerable<TItem>>? FooterTemplate { get; set; }
/// <summary>
/// 获得/设置 双击行回调委托方法
/// </summary>
[Parameter]
[NotNull]
public Func<TItem, Task>? OnDoubleClickRowCallback { get; set; }
/// <summary>
/// 获得/设置 被选中数据集合
/// </summary>
[Parameter]
public List<TItem> SelectedRows { get; set; } = [];
/// <summary>
/// 获得/设置 选中行变化回调方法
/// </summary>
[Parameter]
public EventCallback<List<TItem>> SelectedRowsChanged { get; set; }
/// <summary>
/// 获得/设置 弹窗 Footer
/// </summary>
[Parameter]
[NotNull]
public RenderFragment<TItem>? EditFooterTemplate { get; set; }
/// <summary>
/// 获得/设置 是否保持选择行,默认为 false 不保持
/// </summary>
[Parameter]
public bool IsKeepSelectedRows { get; set; }
#endregion
/// <summary>
/// 附加复杂查询条件
/// </summary>
[Parameter] public DynamicFilterInfo? DynamicFilterInfo { get; set; }
/// <summary>
/// 获得/设置 TableHeader 实例*
/// </summary>
[Parameter]
public RenderFragment<TItem>? TableColumns { get; set; }
/// <summary>
/// 获得/设置 表格 列模板
/// <para>列模板,模板中内容出现在现有列后面*</para>
/// </summary>
[Parameter]
public RenderFragment<TItem>? TableColumnsTemplate { get; set; }
#region 数据服务
/// <summary>
/// 获得/设置 注入数据服务
/// </summary>
[Inject]
[NotNull]
public IEnumerable<FreeSqlDataService<TItem>>? DataServices { get; set; }
public FreeSqlDataService<TItem> GetDataService()
{
if (DataServices.Any())
{
DataServices.Last().SaveManyChildsPropertyName = SaveManyChildsPropertyName;
DataServices.Last().ConnectionString = ConnectionString;
if (!GetFsql())
{
if (ConnectionString != null)
{
DataServices.Last().Use(ConnectionString);
}
}
DataServices.Last().EnableCascadeSave = EnableCascadeSave;
DataServices.Last().AsTable = AsTable;
return DataServices.Last();
}
else
{
throw new InvalidOperationException("数据服务无效,可能是未注入");
}
}
/// <summary>
/// 自定义数据连接
/// </summary>
/// <returns></returns>
public virtual bool GetFsql()
{
return false;
}
public async Task<TItem> OnAddAsync()
{
var newone = new TItem();
if (FieldValue != null || FieldValueD != null)
{
newone.FieldSetValue(Field, FieldValue ?? FieldValueD);
}
if (AddAsync != null)
{
newone = await AddAsync(newone);
}
return newone;
}
public async Task OnBatchAddAsync()
{
if (BatchAddAsync != null)
{
var newone = new TItem();
if (FieldValue != null || FieldValueD != null)
{
newone.FieldSetValue(Field, FieldValue ?? FieldValueD);
}
if (await BatchAddAsync(newone))
{
await TableMain.QueryAsync();
}
}
}
public async Task Refresh()
{
await TableMain.QueryAsync();
}
/// <summary>
/// 设置 列可见方法
/// </summary>
/// <param name="columns"></param>
public void ResetVisibleColumns(IEnumerable<ColumnVisibleItem> columns)
{
TableMain.ResetVisibleColumns(columns);
}
public async Task<TItem> OnEditAsync(TItem item)
{
if (EditAsync != null)
{
await EditAsync(item);
}
GetDataService().ItemCache = item.Clone();
return item;
}
public async Task<bool> OnSaveAsync(TItem item, ItemChangedType changedType)
{
if (SaveAsync != null)
{
var itemRes = await SaveAsync(item, changedType);
if (itemRes != null)
{
item = itemRes;
}
else
{
return false;
}
}
var res = await GetDataService().SaveAsync(item, changedType);
if (AfterSaveAsync != null)
{
await AfterSaveAsync(item, changedType);
}
return res;
}
public async Task<bool> OnDeleteAsync(IEnumerable<TItem> items)
{
if (DeleteAsync != null)
{
if (!await DeleteAsync(items))
{
return false;
}
}
return await GetDataService().DeleteAsync(items);
}
public async Task<QueryData<TItem>> OnQueryAsync(QueryPageOptions options)
{
if (PageIndexCache != null)
{
options.PageIndex = PageIndexCache.Value;
PageIndexCache = null;
}
else
{
if (AutoSavePageIndex)
{
await StorageSetValue(AutoSavePageIndexKey, options.PageIndex);
}
else
{
PageIndex = options.PageIndex;
}
}
var itemsOrm = await GetDataService().QueryAsyncWithWhereCascade(
options,
dynamicFilterInfo,
IncludeByPropertyNames,
LeftJoinString,
OrderByPropertyName,
WhereCascadeOr,
WhereLamda
);
if (GetDataService().Message != null)
{
ToastService?.Error("出错",GetDataService().Message);
}
if (AfterQueryCallBackAsync != null && itemsOrm.Items!=null)
{
itemsOrm.Items = await AfterQueryCallBackAsync(itemsOrm.Items);
}
if (AfterQueryAsync != null)
{
var select = ISelectCache();
if (SelectCache != select)
{
SelectCache = select;
AfterQueryAsync(ISelectCache());
}
}
ItemsCache = itemsOrm.Items;
return itemsOrm;
}
/// <summary>
/// 获得查询子句
/// </summary>
public ISelect<TItem> ISelectCache() => GetDataService().ISelectCache(
dynamicFilterInfo,
IncludeByPropertyNames,
LeftJoinString,
WhereCascadeOr,
WhereLamda
);
/// <summary>
/// 全部记录
/// </summary>
public List<TItem>? GetAllItems() => GetDataService().GetAllItems(
dynamicFilterInfo,
IncludeByPropertyNames,
LeftJoinString,
OrderByPropertyName,
WhereCascadeOr,
WhereLamda
);
#endregion
/// <summary>
/// Reset all Columns Filter
/// </summary>
public async Task ResetFilters()
{
await TableMain.ResetFilters();
}
/// <summary>
/// 获得 表头集合
/// </summary>
public List<ITableColumn> Columns { get => TableMain.Columns; }
protected override void OnInitialized()
{
base.OnInitialized();
if (ShowDetailRowS)
{
ShowDetailRow = _ => true;
}
if (PageItemsSourceStart != null && PageItemsSourceStart < PageItemsSource.FirstOrDefault())
{
PageItemsSource = PageItemsSource.Append(PageItemsSourceStart.Value).OrderBy(a => a).ToList();
}
if (PageItems != 0 && !PageItemsSource.Contains(PageItems))
{
PageItemsSource = PageItemsSource.Append(PageItems).OrderBy(a => a).ToList();
}
AddModalTitle ??= Localizer[nameof(AddModalTitle)];
EditModalTitle ??= Localizer[nameof(EditModalTitle)];
BatchAddButtonText ??= Localizer[nameof(BatchAddButtonText)];
AddButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableAddButtonIcon);
if (AutoRenderComponentWithLocaleFormat && OnColumnCreating == null)
{
OnColumnCreating += AutoRenderComponentLocaleFormat;
}
}
/// <summary>
/// OnParametersSet 方法
/// </summary>
protected override void OnParametersSet()
{
base.OnParametersSet();
if (IsReadonly)
{
EditModalTitle = ReadOnlyModalTitle;
if (EditFooterTemplate == null) { EditFooterTemplate = (item) => builder => { }; }
}
else
{
if (EditModalTitle == ReadOnlyModalTitle)
{
EditModalTitle = Localizer[nameof(EditModalTitle)];
}
AddModalTitle ??= Localizer[nameof(AddModalTitle)];
EditFooterTemplate = null;
}
}
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
if (!firstRender)
{
return;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (!firstRender)
{
return;
}
}
public void SetConnectionString(string? connectionString)
{
this.ConnectionString = connectionString;
}
/// <summary>
/// 查询按钮调用此方法
/// </summary>
/// <param name="whereLamda">附加and条件,如果传入值为空不使用</param>
/// <param name="force">强制使用附加and条件,即使传入值为空也使用</param>
/// <param name="connectionString">数据库连接字符串</param>
/// <returns></returns>
public async Task QueryAsync(Expression<Func<TItem, bool>>? whereLamda = null, bool force = false, string? connectionString = null, List<string>? orderByPropertyName = null)
{
if (whereLamda != null || force)
{
WhereLamda = whereLamda;
}
if (orderByPropertyName != null)
{
OrderByPropertyName = orderByPropertyName;
}
if (connectionString != null)
{
ConnectionString = connectionString;
}
await TableMain.QueryAsync();
}
/// <summary>
/// 附加查询条件,使用and结合
/// </summary>
public DynamicFilterInfo? dynamicFilterInfo
{
get => DynamicFilterInfo ?? ((FieldValue == null && FieldValueD == null) ? null : new DynamicFilterInfo()
{
Field = FieldD ?? Field,
Operator = DynamicFilterOperator.Equal,
Value = FieldValue ?? FieldValueD,
});
}
#region 动态生成控件
/// <summary>
/// 动态获取表达式
/// </summary>
/// <param name="model"></param>
/// <param name="field">列名,默认"ID"</param>
/// <param name="fieldType">列类型,默认typeof(int)</param>
/// <returns></returns>
protected object GetExpression(object model, string? field = "ID", Type? fieldType = null)
{
// ValueExpression
var body = Expression.Property(Expression.Constant(model), typeof(TItem), field ?? "ID");
var tDelegate = typeof(Func<>).MakeGenericType(fieldType ?? typeof(int));
var valueExpression = Expression.Lambda(tDelegate, body);
return valueExpression;
}
/// <summary>
/// 动态生成控件 TableColumn 图片列
/// </summary>
/// <param name="model"></param>
/// <param name="tableImgField"></param>
/// <returns></returns>
protected RenderFragment RenderTableImgColumn(TItem model, TableImgField? tableImgField = null) => builder =>
{
tableImgField = tableImgField ?? TableImgField ?? new TableImgField();
var fieldExpresson = Utility.GenerateValueExpression(model, tableImgField.Field, tableImgField.FieldType);
builder.OpenComponent(0, typeof(TableColumn<,>).MakeGenericType(typeof(TItem), tableImgField.FieldType));
builder.AddAttribute(1, "FieldExpression", fieldExpresson);
//builder.AddAttribute(2, "Width", 200);
builder.AddAttribute(3, "Template", new RenderFragment<TableColumnContext<TItem, string>>(context => buttonBuilder =>
{
buttonBuilder.OpenComponent<ImgColumn>(0);
buttonBuilder.AddAttribute(1, nameof(ImgColumn.Title), tableImgField.Title);
buttonBuilder.AddAttribute(2, nameof(ImgColumn.Name), tableImgField.Name);
var value = (context.Row).GetIdentityKey(tableImgField.Field);
buttonBuilder.AddAttribute(3, nameof(ImgColumn.Url), value);
if (!string.IsNullOrEmpty(tableImgField.BaseUrl))
{
buttonBuilder.AddAttribute(4, nameof(ImgColumn.BaseUrl), tableImgField.BaseUrl);
}
if (!string.IsNullOrEmpty(tableImgField.Style))
{
buttonBuilder.AddAttribute(5, nameof(ImgColumn.Style), tableImgField.Style);
}
buttonBuilder.CloseComponent();
}));
if (!string.IsNullOrEmpty(tableImgField.ColumnText))
{
builder.AddAttribute(4, "Text", tableImgField.ColumnText);
}
builder.CloseComponent();
};
/// <summary>
/// 动态生成控件 TableColumn 功能列
/// </summary>
/// <param name="model"></param>
/// <param name="tableImgField"></param>
/// <returns></returns>
protected RenderFragment RenderTableFunctionsColumn(TItem model, TableImgField tableImgField) => builder =>
{
var fieldExpresson = Utility.GenerateValueExpression(model, tableImgField.Field, tableImgField.FieldType);
builder.OpenComponent(0, typeof(TableColumn<,>).MakeGenericType(typeof(TItem), tableImgField.FieldType));
builder.AddAttribute(1, "FieldExpression", fieldExpresson);
//builder.AddAttribute(2, "Width", 200);
builder.AddAttribute(3, "Template", new RenderFragment<TableColumnContext<TItem, string>>(context => buttonBuilder =>
{
buttonBuilder.OpenComponent<Button>(0);
buttonBuilder.AddAttribute(1, nameof(Button.Text), tableImgField.Title);
var value = (context.Row).GetIdentityKey(tableImgField.Field);
if (tableImgField.Callback.HasDelegate)
{
buttonBuilder.AddAttribute(1, nameof(Button.OnClickWithoutRender), new Func<Task>(async () =>
{
await tableImgField.Callback.InvokeAsync(value);
}));
}
if (!string.IsNullOrEmpty(tableImgField.Style))
{
buttonBuilder.AddAttribute(5, nameof(ImgColumn.Style), tableImgField.Style);
}
buttonBuilder.CloseComponent();
}));
if (!string.IsNullOrEmpty(tableImgField.ColumnText))
{
builder.AddAttribute(4, "Text", tableImgField.ColumnText);
}
builder.CloseComponent();
};
/// <summary>
/// 动态生成控件 TableColumn 明细行按钮
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public virtual RenderFragment RenderTableColumn(TItem model) => builder =>
{
var fieldExpresson = Utility.GenerateValueExpression(model, Field ?? FieldD ?? "ID", FieldType);
var typeTableColumn = typeof(TableColumn<,>).MakeGenericType(typeof(TItem), FieldType);
builder.OpenComponent(0, typeTableColumn);
builder.AddAttribute(1, "FieldExpression", fieldExpresson);
builder.AddAttribute(2, "Template", DialogTableDetails(FieldType));
builder.AddAttribute(3, "Visible", true);
builder.CloseComponent();
};
/// <summary>
/// 动态生成控件 TableColumn 明细行按钮
/// </summary>
/// <param name="fieldType"></param>
/// <returns></returns>
private object DialogTableDetails(Type fieldType)
{
var valueChangedInvoker = CreateLambda(this, fieldType).Compile();
return valueChangedInvoker();
static Expression<Func<object>> CreateLambda(TableAmeProBase<TItem> table, Type fieldType)
{
var method = typeof(TableAmeProBase<TItem>).GetMethod(nameof(CreateRenderFragment), BindingFlags.NonPublic | BindingFlags.Instance)!.MakeGenericMethod(fieldType);
var body = Expression.Call(Expression.Constant(table), method);
return Expression.Lambda<Func<object>>(body);
}
}
/// <summary>
/// 动态生成明细弹窗控件
/// </summary>
/// <returns></returns>
private RenderFragment<TableColumnContext<TItem, TValue>> CreateRenderFragment<TValue>()
{
return new RenderFragment<TableColumnContext<TItem, TValue>>(context => buttonBuilder =>
{
buttonBuilder.OpenComponent<Button>(0);
buttonBuilder.AddAttribute(1, nameof(Button.Text), "明细");
buttonBuilder.AddAttribute(2, nameof(Button.OnClickWithoutRender), new Func<Task>(async () =>
{
var op = new DialogOption()
{
BodyContext = context,
BodyTemplate = RenderTableDetails(context.Row),
Size = Size.Large,
FullScreenSize = FullScreenSize.Small,
ShowMaximizeButton = true,
ShowResize = true,
ShowPrintButton = true,
ShowSaveButton = true,
IsDraggable = true,
SaveButtonText = "保存并应用",
OnCloseAsync = async () =>
{
if (DetailsDialogCloseAsync != null)
{
if (await DetailsDialogCloseAsync(context.Row))
{
await TableMain.QueryAsync();
}
}
},
OnSaveAsync = async () =>
{
if (DetailsDialogSaveAsync != null)
{
if (await DetailsDialogSaveAsync(context.Row))
{
await TableMain.QueryAsync();
}
}
return true;
},
};
await DialogService.Show(op);
}));
buttonBuilder.CloseComponent();
});
}
/// <summary>
/// 动态生成控件 TableColumn 图片列
/// </summary>
/// <returns></returns>
public virtual RenderFragment RenderTableColumnPhotoUrl(TItem model) => builder =>
{
var fieldExpresson = Utility.GenerateValueExpression(model, Field ?? FieldD ?? "ID", FieldType); // 刚才你的那个获取表达式 GetExpression() 的返回值的
var value = Utility.GetPropertyValue<object, object>(model, Field ?? FieldD ?? "ID");
var valueType = value.GetType();
builder.OpenComponent(0, typeof(TableColumn<,>).MakeGenericType(typeof(TItem), FieldType));
builder.AddAttribute(1, "FieldExpression", fieldExpresson);
// 添加模板
builder.AddAttribute(2, "Template", DialogTableDetails(FieldType));
builder.CloseComponent();
};
/// <summary>
/// 动态生成明细表控件 TableAmePro
/// </summary>
/// <param name="model"></param>
/// <param name="rowType"></param>
/// <returns></returns>
public virtual RenderFragment RenderTableDetails(object model, TableDetailRowType rowType = TableDetailRowType.选项卡1) => builder =>
{
var _Field = Field;
var _FieldValue = model.GetIdentityKey(Field);
TRenderTable(rowType, builder);
if (rowType == TableDetailRowType.选项卡2)
{
//第二选项卡
if (FieldII != null && FieldII != Field)
{
_Field = FieldII ?? Field;
_FieldValue = model.GetIdentityKey(FieldII);
}
_Field = FieldIID ?? Field;
}
else if (ShowDetailRowType == ShowDetailRowType.表内明细II)
{
_Field = FieldD ?? Field;
}
else if (rowType == TableDetailRowType.选项卡3)
{
if (FieldIII != null && FieldIII != Field)
{
_Field = FieldIII ?? Field;
_FieldValue = model.GetIdentityKey(FieldIII);
}
_Field = FieldIIID ?? Field;
}
else
{
_Field = FieldD ?? Field;
}
builder.AddAttribute(0, nameof(Field), _Field);
if (IsDebug)
{
ToastService.Success($"展开详情行 {_Field} : _FieldValue");
}
builder.AddAttribute(1, nameof(FieldValue), _FieldValue);
builder.AddAttribute(2, nameof(IsBordered), true);
builder.AddAttribute(3, nameof(ShowToolbar), ShowToolbar);
builder.AddAttribute(4, nameof(ShowDefaultButtons), ShowDefaultButtons);
builder.AddAttribute(5, nameof(ShowExtendButtons), ShowExtendButtons);
builder.AddAttribute(6, nameof(ShowSearch), ShowSearch);
builder.AddAttribute(7, nameof(ShowColumnList), ShowColumnList);
builder.AddAttribute(8, nameof(IsMultipleSelect), IsMultipleSelect);
builder.AddAttribute(9, nameof(DoubleClickToEdit), DoubleClickToEdit);
if (rowType == TableDetailRowType.自定义选项卡风格)
{
builder.AddAttribute(10, nameof(TableSize), TableSize.Compact);
builder.AddAttribute(11, nameof(HeaderStyle), TableHeaderStyle.Light);
}
else
{
builder.AddAttribute(10, nameof(TableSize), TableSizeDetails);
builder.AddAttribute(11, nameof(HeaderStyle), HeaderStyleDetails);
}
builder.AddAttribute(12, nameof(ShowLineNo), ShowLineNo);
builder.AddAttribute(13, nameof(ShowSkeleton), ShowSkeleton);
builder.AddAttribute(14, nameof(ShowRefresh), ShowRefresh);
builder.AddAttribute(15, nameof(IncludeByPropertyNames), rowType == TableDetailRowType.选项卡2 ? (SubIncludeByPropertyNamesII ?? SubIncludeByPropertyNames) : rowType == TableDetailRowType.选项卡3 ? (SubIncludeByPropertyNamesIII ?? SubIncludeByPropertyNames) : SubIncludeByPropertyNames);
if (SubTableImgFields != null)
{
builder.AddAttribute(16, nameof(TableImgFields), SubTableImgFields);
}
if (SubTableImgField != null)
{
builder.AddAttribute(17, nameof(TableImgField), SubTableImgField);
}
if (SubRenderMode != null)
{
builder.AddAttribute(18, nameof(RenderMode), SubRenderMode);
}
if (ConnectionString != null)
{
builder.AddAttribute(19, nameof(ConnectionString), ConnectionString);
}
if (SubTableFunctionsFields != null)
{
builder.AddAttribute(20, nameof(TableFunctionsFields), SubTableFunctionsFields);
}
if (SubRowButtons != null)
{
builder.AddAttribute(21, nameof(RowButtons), SubRowButtons);
}
builder.AddAttribute(22, nameof(IsExtendButtonsInRowHeader), IsExtendButtonsInRowHeader);
if (SubScrollMode != null)
{
builder.AddAttribute(23, nameof(ScrollMode), SubScrollMode);
}
if (SubIsPagination != null)
{
builder.AddAttribute(24, nameof(IsPagination), SubIsPagination);
}
else
{
builder.AddAttribute(24, nameof(IsPagination), IsPagination);
}
if (SubPageItems != null)
{
builder.AddAttribute(25, nameof(PageItems), SubPageItems);
}
if (SubHeight != null)
{
builder.AddAttribute(26, nameof(Height), SubHeight);
}
if (SubEditMode != null)
{
builder.AddAttribute(27, nameof(EditMode), SubEditMode);
}
builder.AddAttribute(29, nameof(IsExcel), SubIsExcel);
builder.AddAttribute(30, nameof(IsReadonly), SubIsReadonly);
builder.AddAttribute(31, nameof(IsSimpleUI), SubIsSimpleUI);
builder.AddAttribute(32, nameof(EditDialogItemsPerRow), SubEditDialogItemsPerRow);
builder.AddAttribute(33, nameof(EditDialogRowType), SubEditDialogRowType);
builder.AddAttribute(34, nameof(EditDialogSize), SubEditDialogSize);
builder.AddAttribute(35, nameof(EditDialogFullScreenSize), EditDialogFullScreenSize);
builder.AddAttribute(36, nameof(EditDialogFullScreenSize), EditDialogFullScreenSize);
TRenderTableAdditionalAttributes(builder, rowType);
builder.CloseComponent();
};
/// <summary>
/// 附加属性
/// </summary>
/// <param name="builder"></param>
public virtual void TRenderTableAdditionalAttributes(RenderTreeBuilder builder, TableDetailRowType rowType = TableDetailRowType.选项卡1)
{
}
/// <summary>
/// 明细表控件
/// </summary>
/// <param name="rowType"></param>
/// <param name="builder"></param>
public virtual void TRenderTable(TableDetailRowType rowType, RenderTreeBuilder builder)
{
builder.OpenComponent<TableAmeProBase<TItem>>(0);
}
#endregion
#region 工具栏按钮
public async Task ImportExcel()
{
if (Items == null || !Items.Any())
{
ToastService?.Error("提示", "数据为空!");
return;
}
var option = new ToastOption()
{