From a11e17faa0b061f179df36153b7758e3c774bdc5 Mon Sep 17 00:00:00 2001 From: Argo Date: Mon, 7 Dec 2020 19:46:09 +0800 Subject: [PATCH] =?UTF-8?q?!735=20feat(#I28H8X):=20Table=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E8=83=BD=E5=8A=9B=20*=20doc:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20UseInjectDataService=20=E6=96=87=E6=A1=A3=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20*=20doc:=20=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=20*=20doc:=20=E5=A2=9E=E5=8A=A0=E5=88=A9=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=9C=8D=E5=8A=A1=E7=A4=BA=E4=BE=8B=20*=20fe?= =?UTF-8?q?at:=20=E5=A2=9E=E5=8A=A0=E6=BC=94=E7=A4=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=AE=9E=E7=8E=B0=E7=B1=BB=20*=20doc:=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E8=A1=A8=E5=8D=95=E7=BC=96=E8=BE=91=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=20*=20refactor:=20=E5=A2=9E=E5=8A=A0=E6=BC=94?= =?UTF-8?q?=E7=A4=BA=E7=A4=BA=E4=BE=8B=E6=95=B0=E6=8D=AE=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=20*=20refactor:=20=E4=BF=AE=E5=A4=8D=20PetaP?= =?UTF-8?q?oco=20=E6=89=A9=E5=B1=95=E5=B7=A5=E7=A8=8B=E9=94=99=E8=AF=AF=20?= =?UTF-8?q?*=20refactor:=20=E5=A2=9E=E5=8A=A0=20UseInjectDataService=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=88=A4=E6=96=AD=20*=20build:=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=AF=B9=20PetaPoco=20=E6=89=A9=E5=B1=95=E7=B1=BB?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=20*=20refactor:=20=E9=87=8D=E6=9E=84=20PetaP?= =?UTF-8?q?oco=20DataService=20=E7=B1=BB=20*=20feat:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20DataService=20=E9=80=BB=E8=BE=91=20*=20refactor:=20=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=20IDataService=20=E5=9F=BA=E7=B1=BB=20*=20feat:=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20IDataService=20=E6=B3=A8=E5=85=A5=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=8A=9F=E8=83=BD=20*=20build:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=20PetaPoco=20=E6=95=B0=E6=8D=AE=E6=9C=8D=E5=8A=A1=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=93=8D=E4=BD=9C=E7=B1=BB=20*=20doc:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20Table=20=E7=BB=84=E4=BB=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=9C=8D=E5=8A=A1=E7=A4=BA=E4=BE=8B=20*=20do?= =?UTF-8?q?c:=20=E6=9B=B4=E6=96=B0=20Tables/Edit=20Upd=20=E6=A0=87?= =?UTF-8?q?=E7=A4=BA=20*=20doc:=20=E5=A2=9E=E5=8A=A0=20DateTimeRange=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=20Upd=20=E6=A0=87=E7=A4=BA=20*=20build:=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96=E5=88=B0=205.0=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BootstrapBlazor.sln | 7 +++ .../BootstrapBlazor.DataAcces.PetaPoco.csproj | 22 +++++++ .../DefaultDataService.cs | 63 +++++++++++++++++++ .../PetaPocoServiceCollectionExtensions.cs | 31 +++++++++ .../wwwroot/docs/table/table.58.html | 28 +++++++++ src/BootstrapBlazor.Server/Startup.cs | 3 + src/BootstrapBlazor.Server/appsettings.json | 3 + .../BootstrapBlazor.Shared.csproj | 6 +- ...ableDemoDataServiceCollectionExtensions.cs | 62 ++++++++++++++++++ .../Pages/Table/TablesBase.cs | 2 +- .../Pages/Table/TablesEdit.razor | 42 +++++++++++++ .../Shared/NavMenu.razor.cs | 6 +- .../Components/Table/DataServiceBase.cs | 48 ++++++++++++++ .../Components/Table/IDataService.cs | 47 ++++++++++++++ .../Components/Table/Table.razor | 2 +- .../Components/Table/Table.razor.Edit.cs | 19 ++++++ .../Components/Table/Table.razor.Toolbar.cs | 19 ++++-- .../Components/Table/Table.razor.cs | 6 ++ 18 files changed, 403 insertions(+), 13 deletions(-) create mode 100644 src/BootstrapBlazor.DataAcces.PetaPoco/BootstrapBlazor.DataAcces.PetaPoco.csproj create mode 100644 src/BootstrapBlazor.DataAcces.PetaPoco/DefaultDataService.cs create mode 100644 src/BootstrapBlazor.DataAcces.PetaPoco/PetaPocoServiceCollectionExtensions.cs create mode 100644 src/BootstrapBlazor.Docs/wwwroot/docs/table/table.58.html create mode 100644 src/BootstrapBlazor.Shared/Extensions/TableDemoDataServiceCollectionExtensions.cs create mode 100644 src/BootstrapBlazor/Components/Table/DataServiceBase.cs create mode 100644 src/BootstrapBlazor/Components/Table/IDataService.cs diff --git a/BootstrapBlazor.sln b/BootstrapBlazor.sln index 5c6ea3d27..306e6395f 100644 --- a/BootstrapBlazor.sln +++ b/BootstrapBlazor.sln @@ -81,6 +81,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.Markdown", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "extensions", "extensions", "{22328011-53B3-447A-B781-AC3C196B2847}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.DataAcces.PetaPoco", "src\BootstrapBlazor.DataAcces.PetaPoco\BootstrapBlazor.DataAcces.PetaPoco.csproj", "{0D81BCD6-6533-49DA-82A3-0A410B87CF56}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -127,6 +129,10 @@ Global {B37DA844-38E1-4F39-9562-41F84F3F6C68}.Debug|Any CPU.Build.0 = Debug|Any CPU {B37DA844-38E1-4F39-9562-41F84F3F6C68}.Release|Any CPU.ActiveCfg = Release|Any CPU {B37DA844-38E1-4F39-9562-41F84F3F6C68}.Release|Any CPU.Build.0 = Release|Any CPU + {0D81BCD6-6533-49DA-82A3-0A410B87CF56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0D81BCD6-6533-49DA-82A3-0A410B87CF56}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0D81BCD6-6533-49DA-82A3-0A410B87CF56}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0D81BCD6-6533-49DA-82A3-0A410B87CF56}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -147,6 +153,7 @@ Global {FB6352BD-B93D-4BE4-B8B5-BEC75D81D483} = {22328011-53B3-447A-B781-AC3C196B2847} {7D8DFDF8-35F7-43A9-8304-1726890B0432} = {22328011-53B3-447A-B781-AC3C196B2847} {B37DA844-38E1-4F39-9562-41F84F3F6C68} = {22328011-53B3-447A-B781-AC3C196B2847} + {0D81BCD6-6533-49DA-82A3-0A410B87CF56} = {22328011-53B3-447A-B781-AC3C196B2847} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B} diff --git a/src/BootstrapBlazor.DataAcces.PetaPoco/BootstrapBlazor.DataAcces.PetaPoco.csproj b/src/BootstrapBlazor.DataAcces.PetaPoco/BootstrapBlazor.DataAcces.PetaPoco.csproj new file mode 100644 index 000000000..d4849ba30 --- /dev/null +++ b/src/BootstrapBlazor.DataAcces.PetaPoco/BootstrapBlazor.DataAcces.PetaPoco.csproj @@ -0,0 +1,22 @@ + + + + net5.0 + true + logo.png + + + + + + + + + + + + + + + + diff --git a/src/BootstrapBlazor.DataAcces.PetaPoco/DefaultDataService.cs b/src/BootstrapBlazor.DataAcces.PetaPoco/DefaultDataService.cs new file mode 100644 index 000000000..60fe4a566 --- /dev/null +++ b/src/BootstrapBlazor.DataAcces.PetaPoco/DefaultDataService.cs @@ -0,0 +1,63 @@ +// ********************************** +// 框架名称:BootstrapBlazor +// 框架作者:Argo Zhang +// 开源地址: +// Gitee : https://gitee.com/LongbowEnterprise/BootstrapBlazor +// GitHub: https://github.com/ArgoZhang/BootstrapBlazor +// 开源协议:LGPL-3.0 (https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/dev/LICENSE) +// ********************************** + +using BootstrapBlazor.Components; +using Microsoft.Extensions.Configuration; +using System.Collections.Generic; +using System.Threading.Tasks; +using PP = PetaPoco; + +namespace BootstrapBlazor.DataAcces.PetaPoco +{ + /// + /// + /// + internal class DefaultDataService : DataServiceBase where TModel : class, new() + { + /// + /// + /// + public DefaultDataService(IConfiguration config) + { + // 获取连接字符串 + + } + + /// + /// + /// + /// + /// + public override Task SaveAsync(TModel model) + { + return base.SaveAsync(model); + } + + /// + /// + /// + /// + /// + public override Task> QueryAsync(QueryPageOptions option) + { + return Task.FromResult(new QueryData() + { + IsFiltered = true, + IsSearch = true, + IsSorted = true, + TotalCount = 2, + Items = new TModel[] + { + new TModel(), + new TModel() + } + }); + } + } +} diff --git a/src/BootstrapBlazor.DataAcces.PetaPoco/PetaPocoServiceCollectionExtensions.cs b/src/BootstrapBlazor.DataAcces.PetaPoco/PetaPocoServiceCollectionExtensions.cs new file mode 100644 index 000000000..ff0cd137b --- /dev/null +++ b/src/BootstrapBlazor.DataAcces.PetaPoco/PetaPocoServiceCollectionExtensions.cs @@ -0,0 +1,31 @@ +// ********************************** +// 框架名称:BootstrapBlazor +// 框架作者:Argo Zhang +// 开源地址: +// Gitee : https://gitee.com/LongbowEnterprise/BootstrapBlazor +// GitHub: https://github.com/ArgoZhang/BootstrapBlazor +// 开源协议:LGPL-3.0 (https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/dev/LICENSE) +// ********************************** + +using BootstrapBlazor.Components; +using BootstrapBlazor.DataAcces.PetaPoco; + +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// BootstrapBlazor 服务扩展类 + /// + public static class PetaPocoServiceCollectionExtensions + { + /// + /// 增加 PetaPoco 数据库操作服务 + /// + /// + /// + public static IServiceCollection AddPetaPoco(this IServiceCollection services) + { + services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>)); + return services; + } + } +} diff --git a/src/BootstrapBlazor.Docs/wwwroot/docs/table/table.58.html b/src/BootstrapBlazor.Docs/wwwroot/docs/table/table.58.html new file mode 100644 index 000000000..cee43fbb5 --- /dev/null +++ b/src/BootstrapBlazor.Docs/wwwroot/docs/table/table.58.html @@ -0,0 +1,28 @@ + + + + + +
+ +
+
+
+ + + + + + +
+ +
+
+
+
+
diff --git a/src/BootstrapBlazor.Server/Startup.cs b/src/BootstrapBlazor.Server/Startup.cs index 603f5b58a..041c21b11 100644 --- a/src/BootstrapBlazor.Server/Startup.cs +++ b/src/BootstrapBlazor.Server/Startup.cs @@ -71,6 +71,9 @@ namespace BootstrapBlazor.Server // 增加 Table Excel 导出服务 services.AddBootstrapBlazorTableExcelExport(); + // 增加 Table 数据服务操作类 + services.AddTableDemoDataService(); + // 统一设置 Toast 组件自动消失时间 services.Configure(options => { diff --git a/src/BootstrapBlazor.Server/appsettings.json b/src/BootstrapBlazor.Server/appsettings.json index 15c3900f0..c5f3dfa00 100644 --- a/src/BootstrapBlazor.Server/appsettings.json +++ b/src/BootstrapBlazor.Server/appsettings.json @@ -7,6 +7,9 @@ } }, "AllowedHosts": "*", + "ConnectionStrings": { + "ba": "Data Source=bb.db;" + }, "SupportCultures": [ "en-US", "de-DE", diff --git a/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj b/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj index f0fd7f4c4..20541bc6c 100644 --- a/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj +++ b/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj @@ -16,9 +16,9 @@ - - - + + + diff --git a/src/BootstrapBlazor.Shared/Extensions/TableDemoDataServiceCollectionExtensions.cs b/src/BootstrapBlazor.Shared/Extensions/TableDemoDataServiceCollectionExtensions.cs new file mode 100644 index 000000000..fafbcfc2a --- /dev/null +++ b/src/BootstrapBlazor.Shared/Extensions/TableDemoDataServiceCollectionExtensions.cs @@ -0,0 +1,62 @@ +// ********************************** +// 框架名称:BootstrapBlazor +// 框架作者:Argo Zhang +// 开源地址: +// Gitee : https://gitee.com/LongbowEnterprise/BootstrapBlazor +// GitHub: https://github.com/ArgoZhang/BootstrapBlazor +// 开源协议:LGPL-3.0 (https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/dev/LICENSE) +// ********************************** + +using BootstrapBlazor.Components; +using BootstrapBlazor.Shared.Pages; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// BootstrapBlazor 服务扩展类 + /// + public static class TableDemoDataServiceCollectionExtensions + { + /// + /// 增加 PetaPoco 数据库操作服务 + /// + /// + /// + public static IServiceCollection AddTableDemoDataService(this IServiceCollection services) + { + services.AddSingleton(typeof(IDataService<>), typeof(TableDemoDataService<>)); + return services; + } + } + + /// + /// 演示网站示例数据注入服务实现类 + /// + internal class TableDemoDataService : DataServiceBase where TModel : class, new() + { + /// + /// 查询操作方法 + /// + /// + /// + public override Task> QueryAsync(QueryPageOptions options) + { + // 此处代码实战中不可用,仅仅为演示而写 + + var items = TablesBase.GenerateItems().Cast(); + + var total = items.Count(); + + // 内存分页 + items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList(); + + return Task.FromResult(new QueryData() + { + Items = items, + TotalCount = total + }); + } + } +} diff --git a/src/BootstrapBlazor.Shared/Pages/Table/TablesBase.cs b/src/BootstrapBlazor.Shared/Pages/Table/TablesBase.cs index 762b76d84..c08df2bb3 100644 --- a/src/BootstrapBlazor.Shared/Pages/Table/TablesBase.cs +++ b/src/BootstrapBlazor.Shared/Pages/Table/TablesBase.cs @@ -38,7 +38,7 @@ namespace BootstrapBlazor.Shared.Pages /// /// /// - protected static List GenerateItems() => Enumerable.Range(1, 80).Select(i => new BindItem() + internal static List GenerateItems() => Enumerable.Range(1, 80).Select(i => new BindItem() { Id = i, Name = $"张三 {i:d4}", diff --git a/src/BootstrapBlazor.Shared/Pages/Table/TablesEdit.razor b/src/BootstrapBlazor.Shared/Pages/Table/TablesEdit.razor index b217a0c5d..bdb7438ff 100644 --- a/src/BootstrapBlazor.Shared/Pages/Table/TablesEdit.razor +++ b/src/BootstrapBlazor.Shared/Pages/Table/TablesEdit.razor @@ -162,3 +162,45 @@ + + +

+ 通过 UseInjectDataService="true" 开启使用数据服务进行增、删、改、查的数据库操作,而无需对以下回调委托进行赋值,优先级别为有回调方法优先调用回调方法,如无则调用注入服务进行数据操作 +

    +
  • OnAddAsync
  • +
  • OnDeleteAsync
  • +
  • OnSaveAsync
  • +
  • OnQueryAsync
  • +
+
Startup 文件注入数据服务 实现原理与用法介绍
+

+
services.AddTableDemoDataService();
+ + + + + +
+ +
+
+
+ + + + + + +
+ +
+
+
+
+
+
diff --git a/src/BootstrapBlazor.Shared/Shared/NavMenu.razor.cs b/src/BootstrapBlazor.Shared/Shared/NavMenu.razor.cs index 6ec1f1858..1a4ba1339 100644 --- a/src/BootstrapBlazor.Shared/Shared/NavMenu.razor.cs +++ b/src/BootstrapBlazor.Shared/Shared/NavMenu.razor.cs @@ -189,7 +189,8 @@ namespace BootstrapBlazor.Shared.Shared item.AddItem(new DemoMenuItem() { Text = "时间范围框 DateTimeRange", - Url = "datetimeranges" + Url = "datetimeranges", + IsUpdate = true }); item.AddItem(new DemoMenuItem() { @@ -455,7 +456,8 @@ namespace BootstrapBlazor.Shared.Shared it.AddItem(new DemoMenuItem() { Text = "表单维护", - Url = "tables/edit" + Url = "tables/edit", + IsUpdate = true }); it.AddItem(new DemoMenuItem() diff --git a/src/BootstrapBlazor/Components/Table/DataServiceBase.cs b/src/BootstrapBlazor/Components/Table/DataServiceBase.cs new file mode 100644 index 000000000..2a0c3df8a --- /dev/null +++ b/src/BootstrapBlazor/Components/Table/DataServiceBase.cs @@ -0,0 +1,48 @@ +// ********************************** +// 框架名称:BootstrapBlazor +// 框架作者:Argo Zhang +// 开源地址: +// Gitee : https://gitee.com/LongbowEnterprise/BootstrapBlazor +// GitHub: https://github.com/ArgoZhang/BootstrapBlazor +// 开源协议:LGPL-3.0 (https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/dev/LICENSE) +// ********************************** + +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace BootstrapBlazor.Components +{ + /// + /// IDataServie 实现类基类 + /// + public abstract class DataServiceBase : IDataService where TModel : class, new() + { + /// + /// 新建实体类配置方法 + /// + /// + /// + public virtual Task Config(in TModel model) => Task.CompletedTask; + + /// + /// 删除数据操作方法 + /// + /// + /// + public virtual Task DeleteAsync(IEnumerable models) => Task.FromResult(true); + + /// + /// 保存数据操作方法 + /// + /// + /// + public virtual Task SaveAsync(TModel model) => Task.FromResult(true); + + /// + /// 查询数据操作方法 + /// + /// + /// + public abstract Task> QueryAsync(QueryPageOptions option); + } +} diff --git a/src/BootstrapBlazor/Components/Table/IDataService.cs b/src/BootstrapBlazor/Components/Table/IDataService.cs new file mode 100644 index 000000000..af58770a8 --- /dev/null +++ b/src/BootstrapBlazor/Components/Table/IDataService.cs @@ -0,0 +1,47 @@ +// ********************************** +// 框架名称:BootstrapBlazor +// 框架作者:Argo Zhang +// 开源地址: +// Gitee : https://gitee.com/LongbowEnterprise/BootstrapBlazor +// GitHub: https://github.com/ArgoZhang/BootstrapBlazor +// 开源协议:LGPL-3.0 (https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/dev/LICENSE) +// ********************************** + +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace BootstrapBlazor.Components +{ + /// + /// IDataService 接口 + /// + public interface IDataService where TModel : class, new() + { + /// + /// 创建新数据实体类回调方法 + /// + /// + Task Config(in TModel model); + + /// + /// 删除数据方法 + /// + /// 要删除的数据集合 + /// 成功返回真,失败返回假 + Task DeleteAsync(IEnumerable models); + + /// + /// 保存数据方法 + /// + /// 保存实体类实例 + /// + Task SaveAsync(TModel model); + + /// + /// 查询数据方法 + /// + /// 查询条件参数集合 + /// + Task> QueryAsync(QueryPageOptions option); + } +} diff --git a/src/BootstrapBlazor/Components/Table/Table.razor b/src/BootstrapBlazor/Components/Table/Table.razor index 7fb1aa4f6..722b718a7 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor +++ b/src/BootstrapBlazor/Components/Table/Table.razor @@ -503,7 +503,7 @@ RenderFragment RenderEditForm => item => } else { - + diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs index a574cdd9d..c07e44cc4 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs @@ -175,6 +175,12 @@ namespace BootstrapBlazor.Components [Parameter] public bool AutoGenerateColumns { get; set; } + /// + /// 获得/设置 注入数据服务 + /// + [Inject] + public IDataService? DataService { get; set; } + /// /// 单选模式下选择行时调用此方法 /// @@ -237,6 +243,19 @@ namespace BootstrapBlazor.Components SearchModel = SearchModel }); } + else if (UseInjectDataService && DataService != null) + { + queryData = await DataService.QueryAsync(new QueryPageOptions() + { + PageIndex = PageIndex, + PageItems = PageItems, + SearchText = SearchText, + SortOrder = SortOrder, + SortName = SortName, + Filters = Filters.Values, + SearchModel = SearchModel + }); + } if (queryData != null) { Items = queryData.Items; diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index b9aebef91..14af0ecf3 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -179,9 +179,14 @@ namespace BootstrapBlazor.Components /// public async Task AddAsync() { - if (OnSaveAsync != null) + if ((UseInjectDataService && DataService != null) || OnSaveAsync != null) { if (OnAddAsync != null) EditModel = await OnAddAsync(); + else if (DataService != null) + { + EditModel = new TItem(); + await DataService.Config(EditModel); + } else EditModel = new TItem(); SelectedItems.Clear(); @@ -217,7 +222,7 @@ namespace BootstrapBlazor.Components /// public Task EditAsync() { - if (OnSaveAsync != null) + if ((UseInjectDataService && DataService != null) || OnSaveAsync != null) { if (SelectedItems.Count == 1) { @@ -275,10 +280,10 @@ namespace BootstrapBlazor.Components /// 保存数据 /// /// - protected async Task Save(EditContext context) + protected async Task SaveAsync(EditContext context) { var valid = false; - if (OnSaveAsync != null) + if (DataService != null || OnSaveAsync != null) { if (EditMode == EditMode.EditForm) { @@ -286,7 +291,8 @@ namespace BootstrapBlazor.Components ShowEditForm = false; } - valid = await OnSaveAsync((TItem)context.Model); + if (OnSaveAsync != null) valid = await OnSaveAsync((TItem)context.Model); + else if (DataService != null) valid = await DataService.SaveAsync((TItem)context.Model); var option = new ToastOption { Category = valid ? ToastCategory.Success : ToastCategory.Error, @@ -328,7 +334,7 @@ namespace BootstrapBlazor.Components { new KeyValuePair(nameof(TableEditorDialog.Model), EditModel), new KeyValuePair(nameof(TableEditorDialog.Columns), Columns.Where(i => i.Editable)), - new KeyValuePair(nameof(TableEditorDialog.OnSaveAsync), new Func(Save)), + new KeyValuePair(nameof(TableEditorDialog.OnSaveAsync), new Func(SaveAsync)), new KeyValuePair(nameof(TableEditorDialog.ShowLabel), false), new KeyValuePair(nameof(TableEditorDialog.BodyTemplate), EditTemplate!) }; @@ -368,6 +374,7 @@ namespace BootstrapBlazor.Components { var ret = false; if (OnDeleteAsync != null) ret = await OnDeleteAsync(SelectedItems); + else if (UseInjectDataService && DataService != null) ret = await DataService.DeleteAsync(SelectedItems); var option = new ToastOption() { Title = DeleteButtonToastTitle diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 701a3549a..644b29b00 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -125,6 +125,12 @@ namespace BootstrapBlazor.Components /// public List Columns { get; } = new List(50); + /// + /// 获得/设置 是否使用注入的数据服务 + /// + [Parameter] + public bool UseInjectDataService { get; set; } + /// /// 获得/设置 明细行模板 ///