From f9292bef3cc092e4b17ba406462ab6ffb7ad22ad Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Mon, 13 Jun 2022 20:05:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=20Table=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8A=A8=E6=80=81=E6=A8=A1=E5=BC=8F=E4=B8=8B?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Table/TablesDynamic.razor | 6 + .../Samples/Table/TablesDynamic.razor.cs | 107 +++++++++++++++++- 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor index 05c342fc2..f18c6c9db 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor @@ -25,3 +25,9 @@ + + + + + + diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs index b5d33ff87..84579c16e 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs @@ -18,6 +18,9 @@ public partial class TablesDynamic [NotNull] private DataTableDynamicContext? DataTableDynamicContext { get; set; } + [NotNull] + private DataTableDynamicContext? DataTablePageDynamicContext { get; set; } + private DataTable UserData { get; } = new DataTable(); [Inject] @@ -50,6 +53,9 @@ public partial class TablesDynamic // 初始化 DataTable InitDataTable(); + + // 初始化分页表格 + InitPageDataTable(); } private void CreateContext() @@ -62,8 +68,9 @@ public partial class TablesDynamic { context.AddRequiredAttribute(nameof(Foo.DateTime)); // 使用 AutoGenerateColumnAttribute 设置显示名称示例 - context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair[] { - new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) + context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair[] + { + new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) }); } else if (propertyName == nameof(Foo.Name)) @@ -82,8 +89,9 @@ public partial class TablesDynamic { col.Filterable = true; // 使用 DisplayAttribute 设置显示名称示例 - context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair[] { - new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value) + context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair[] + { + new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value) }); } else if (propertyName == nameof(Foo.Id)) @@ -169,4 +177,95 @@ public partial class TablesDynamic } return Task.CompletedTask; } + + private DataTable PageDataTable { get; set; } = new DataTable(); + + private IEnumerable PageItemsSource { get; set; } = new int[] { 2, 4, 8, 10 }; + + private int PageItems { get; set; } + + private int TotalCount { get; set; } + + private int PageIndex { get; set; } = 1; + + [NotNull] + private List? PageFoos { get; set; } + + [NotNull] + private Table? PageTable { get; set; } + + private void InitPageDataTable() + { + PageDataTable.Columns.Add(nameof(Foo.Id), typeof(int)); + PageDataTable.Columns.Add(nameof(Foo.DateTime), typeof(DateTime)); + PageDataTable.Columns.Add(nameof(Foo.Name), typeof(string)); + PageDataTable.Columns.Add(nameof(Foo.Count), typeof(int)); + + PageFoos = Foo.GenerateFoo(Localizer, 80); + PageIndex = 1; + PageItems = PageItemsSource.First(); + TotalCount = PageFoos.Count; + + RebuildPageDataTable(); + } + + private void RebuildPageDataTable() + { + PageDataTable.Rows.Clear(); + // 此处代码可以通过数据库获得分页后的数据转化成 DataTable 再给 DynamicContext 即可实现数据库分页 + foreach (var f in PageFoos.Skip((PageIndex - 1) * PageItems).Take(PageItems).ToList()) + { + PageDataTable.Rows.Add(f.Id, f.DateTime, f.Name, f.Count); + } + PageDataTable.AcceptChanges(); + + DataTablePageDynamicContext = new DataTableDynamicContext(PageDataTable, (context, col) => + { + var propertyName = col.GetFieldName(); + if (propertyName == nameof(Foo.DateTime)) + { + context.AddRequiredAttribute(nameof(Foo.DateTime)); + // 使用 AutoGenerateColumnAttribute 设置显示名称示例 + context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair[] + { + new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) + }); + } + else if (propertyName == nameof(Foo.Name)) + { + context.AddRequiredAttribute(nameof(Foo.Name), Localizer["Name.Required"]); + // 使用 Text 设置显示名称示例 + col.Text = Localizer[nameof(Foo.Name)]; + } + else if (propertyName == nameof(Foo.Count)) + { + context.AddRequiredAttribute(nameof(Foo.Count)); + // 使用 DisplayNameAttribute 设置显示名称示例 + context.AddDisplayNameAttribute(nameof(Foo.Count), Localizer[nameof(Foo.Count)].Value); + } + else if (propertyName == nameof(Foo.Id)) + { + col.Editable = false; + col.Visible = false; + } + }); + } + + private Task OnPageClick(int pageIndex, int pageItems) + { + PageIndex = pageIndex; + PageItems = pageItems; + RebuildPageDataTable(); + StateHasChanged(); + return Task.CompletedTask; + } + + private Task OnPageItemsChanged(int pageItems) + { + PageIndex = 1; + PageItems = pageItems; + RebuildPageDataTable(); + StateHasChanged(); + return Task.CompletedTask; + } } -- Gitee From b9ad9b70c0556fc81959dd2c22e1ff0e92c51111 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Mon, 13 Jun 2022 20:14:53 +0800 Subject: [PATCH 2/3] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Table/TablesDynamic.razor.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs index 84579c16e..9f3694d7f 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesDynamic.razor.cs @@ -191,9 +191,6 @@ public partial class TablesDynamic [NotNull] private List? PageFoos { get; set; } - [NotNull] - private Table? PageTable { get; set; } - private void InitPageDataTable() { PageDataTable.Columns.Add(nameof(Foo.Id), typeof(int)); @@ -251,6 +248,12 @@ public partial class TablesDynamic }); } + /// + /// 点击页码处理函数 + /// + /// + /// + /// private Task OnPageClick(int pageIndex, int pageItems) { PageIndex = pageIndex; @@ -260,6 +263,11 @@ public partial class TablesDynamic return Task.CompletedTask; } + /// + /// 点击每页显示数量下拉框处理函数 + /// + /// + /// private Task OnPageItemsChanged(int pageItems) { PageIndex = 1; -- Gitee From 4492779cf6d418370b4180555184cb5c64d9a819 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Mon, 13 Jun 2022 20:16:39 +0800 Subject: [PATCH 3/3] chore: bump version to 6.7.11 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 67128e752..9d30a8289 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -3,7 +3,7 @@ - 6.7.10 + 6.7.11 -- Gitee