!1723 feat(#I45L4I): support Nullable enum data type when AutoGenerateTemplate

* doc: 更新示例文档增加 Row="3" 设置示例
* refactor: InCell 编辑模式使用 Display 渲染只读列
* revert: 移除自动创建组件对 Readonly 兼容代码
* doc: 修复示例说明 Select 默认项问题
* doc: 地址字段增加默认值
* doc: 新建时对只读字段进行默认值赋值
This commit is contained in:
Argo 2021-08-15 03:27:38 +00:00
parent 21857beb44
commit c861bd5c34
6 changed files with 18 additions and 31 deletions

View File

@ -49,7 +49,7 @@
<p>数据绑定类型为可为空类型时自动允许为空,如日期绑定列为 <code>DateTime?</code> 类型</p>
<p>数据绑定类型为数值类型时如,如数量绑定列为 <code>int</code> 类型,自动进行数值验证</p>
<p>表格呈现的有些数据列是计算得到的结果,此种类型的列是无法参与编辑的,通过设置 <code>Editable=false</code> 自动生成编辑 UI 时就不会生成此列编辑组件,如本示例中 <code>Count</code> 列在编辑弹窗中是不出现的</p>
<p>通过设置 <code>Readonly=true</code> 自动生成编辑 UI 会将此字段进行只读处理</p>
<p>通过设置 <code>Readonly=true</code> 自动生成编辑 UI 会将此字段进行只读处理,新建时请对 <code>Model</code> 进行默认值赋值 </p>
<Table TItem="Foo"
IsPagination="true" PageItemsSource="@PageItemsSource"
IsStriped="true" IsBordered="true" IsMultipleSelect="true"
@ -58,8 +58,8 @@
OnAddAsync="@OnAddAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync">
<TableColumns>
<TableColumn @bind-Field="@context.DateTime" Width="180" />
<TableColumn @bind-Field="@context.Name" Readonly="true" />
<TableColumn @bind-Field="@context.Address" Rows="3" />
<TableColumn @bind-Field="@context.Name" />
<TableColumn @bind-Field="@context.Address" Readonly="true" />
<TableColumn @bind-Field="@context.Education" />
<TableColumn @bind-Field="@context.Count" Editable="false" />
<TableColumn @bind-Field="@context.Complete" ComponentType="@typeof(Switch)" />
@ -68,7 +68,11 @@
</Block>
<Block Title="自定义列编辑模板" Introduction="当设置列的 <code>EditTemplate</code> 时,组件自动生成表单维护 UI 时使用此模板作为呈现 UI">
<p>通过设置姓名列的 <code>EditTemplate</code> 自定义编辑时使用下拉框来选择姓名</p>
<p>
<div>通过设置姓名列的 <code>EditTemplate</code> 自定义编辑时使用下拉框来选择姓名</div>
<div>本例中 <code>Name</code> 列为自定义组件 <code>TableNameDrop</code>,新建时默认为 <code>请选择 ...</code></div>
</p>
<Table TItem="Foo"
IsPagination="true" PageItemsSource="@PageItemsSource"
IsStriped="true" IsBordered="true" IsMultipleSelect="true"
@ -84,8 +88,7 @@
</div>
</EditTemplate>
</TableColumn>
<TableColumn @bind-Field="@context.Address" />
<TableColumn @bind-Field="@context.Education" />
<TableColumn @bind-Field="@context.Address" Rows="3" />
<TableColumn @bind-Field="@context.Count" />
<TableColumn @bind-Field="@context.Complete" ComponentType="@typeof(Switch)" />
</TableColumns>

View File

@ -48,7 +48,7 @@ namespace BootstrapBlazor.Shared.Pages.Table
CustomerDataService = new FooDataService<Foo>(Localizer);
}
private static Task<Foo> OnAddAsync() => Task.FromResult(new Foo() { DateTime = DateTime.Now });
private static Task<Foo> OnAddAsync() => Task.FromResult(new Foo() { DateTime = DateTime.Now, Address = $"自定义地址 {DateTime.Now.Second}" });
private Task<bool> OnSaveAsync(Foo item)
{

View File

@ -1 +1 @@
<Select Items="@items" @bind-Value="@Model.Name" />
<Select Items="Items" @bind-Value="@Model.Name" />

View File

@ -22,8 +22,9 @@ namespace BootstrapBlazor.Shared.Pages.Table
[NotNull]
public Foo? Model { get; set; }
private readonly List<SelectedItem> items = new()
private List<SelectedItem> Items { get; } = new()
{
new SelectedItem { Text = "请选择 ...", Value = "" },
new SelectedItem { Text = "自定义姓名1", Value = "自定义姓名1" },
new SelectedItem { Text = "自定义姓名2", Value = "自定义姓名2" },
new SelectedItem { Text = "自定义姓名3", Value = "自定义姓名3" },

View File

@ -840,7 +840,9 @@ namespace BootstrapBlazor.Components
#endregion
private RenderFragment RenderCell(ITableColumn col) => col.EditTemplate == null
? builder => builder.CreateComponentByFieldType(this, col, EditModel, addInCell: AddInCell, editInCell: EditInCell)
? (col.Readonly
? builder => builder.CreateDisplayByFieldType(this, col, EditModel, false)
: builder => builder.CreateComponentByFieldType(this, col, EditModel, false))
: col.EditTemplate.Invoke(EditModel);
#region Filter

View File

@ -306,9 +306,7 @@ namespace BootstrapBlazor.Components
/// <param name="item"></param>
/// <param name="showLabel"></param>
/// <param name="placeholder"></param>
/// <param name="addInCell"></param>
/// <param name="editInCell"></param>
public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null, string? placeholder = null, bool addInCell = false, bool editInCell = false)
public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null, string? placeholder = null)
{
var fieldType = item.PropertyType;
var fieldName = item.GetFieldName();
@ -317,30 +315,13 @@ namespace BootstrapBlazor.Components
var fieldValue = GenerateValue(model, fieldName);
var fieldValueChanged = GenerateValueChanged(component, model, fieldName, fieldType);
var valueExpression = GenerateValueExpression(model, fieldName, fieldType);
var editable = item.Editable;
var @readonly = item.Readonly;
if (!addInCell && editInCell && (!editable || @readonly))
{
item.ComponentType = typeof(Display<>).MakeGenericType(fieldType);
}
if (addInCell)
{
@readonly = false;
}
var componentType = item.ComponentType ?? GenerateComponentType(fieldType, item.Rows != 0);
builder.OpenComponent(0, componentType);
builder.AddAttribute(1, nameof(ValidateBase<string>.DisplayText), displayName);
builder.AddAttribute(2, nameof(ValidateBase<string>.Value), fieldValue);
builder.AddAttribute(3, nameof(ValidateBase<string>.ValueChanged), fieldValueChanged);
builder.AddAttribute(4, nameof(ValidateBase<string>.ValueExpression), valueExpression);
if (componentType != typeof(Display<>).MakeGenericType(fieldType))
{
builder.AddAttribute(5, nameof(ValidateBase<string>.IsDisabled), @readonly);
}
builder.AddAttribute(5, nameof(ValidateBase<string>.IsDisabled), item.Readonly);
if (IsCheckboxList(fieldType) && item.Data != null)
{