add custom converter support to the table column and use it to display enum properties.

pull/7686/head
Ilkay Ilknur 5 years ago
parent f21ba0a80e
commit a0319374a3

@ -14,6 +14,7 @@ namespace Volo.Abp.AspNetCore.Components.Extensibility.TableColumns
[CanBeNull]
public Type Component { get; set; }
public List<EntityAction> Actions { get; set; }
public Func<object,string> ValueConverter { get; set; }
public TableColumn()
{

@ -18,6 +18,7 @@ using Volo.Abp.AspNetCore.Components.Extensibility.TableColumns;
using Volo.Abp.Localization;
using Volo.Abp.Authorization;
using Volo.Abp.BlazoriseUI.Components;
using Volo.Abp.BlazoriseUI.Components.ObjectExtending;
using Volo.Abp.ObjectExtending.Modularity;
using Volo.Abp.ObjectExtending;
@ -503,18 +504,24 @@ namespace Volo.Abp.BlazoriseUI
}
else
{
string displayFormat = null;
var column = new TableColumn
{
Title = propertyInfo.GetLocalizedDisplayName(StringLocalizerFactory),
Data = $"ExtraProperties[{propertyInfo.Name}]"
};
if (propertyInfo.IsDate() || propertyInfo.IsDateTime())
{
displayFormat = propertyInfo.GetDateEditInputFormatOrNull();
column.DisplayFormat = propertyInfo.GetDateEditInputFormatOrNull();
}
yield return new TableColumn
if (propertyInfo.Type.IsEnum)
{
Title = propertyInfo.GetLocalizedDisplayName(StringLocalizerFactory),
Data = $"ExtraProperties[{propertyInfo.Name}]",
DisplayFormat = displayFormat
};
column.ValueConverter = (val) =>
EnumHelper.GetLocalizedMemberName(propertyInfo.Type, val, StringLocalizerFactory);
}
yield return column;
}
}
}

@ -1,6 +1,7 @@
@typeparam TItem
@using Blazorise.DataGrid;
@using Volo.Abp.Data
@using Volo.Abp.BlazoriseUI.Components.ObjectExtending
<DataGrid TItem="TItem"
Data="@Data"
@ -56,7 +57,7 @@
{
if (!ExtensionPropertiesRegex.IsMatch(column.Data))
{
<DataGridColumn TItem="TItem" Field="@column.Data" Caption="@column.Title" />
<DataGridColumn TItem="TItem" Field="@column.Data" Caption="@column.Title"/>
}
else
{
@ -68,31 +69,45 @@
var propertyValue = entity.GetProperty(propertyName);
if (propertyValue != null && propertyValue.GetType() == typeof(bool))
{
if ((bool)propertyValue)
if ((bool) propertyValue)
{
<Icon class="text-success" Name="IconName.Check" />
<Icon class="text-success" Name="IconName.Check"/>
}
else
{
<Icon class="text-danger" Name="IconName.Times" />
<Icon class="text-danger" Name="IconName.Times"/>
}
}
else
{
if (column.DisplayFormat == null)
if (column.ValueConverter != null)
{
@(propertyValue)
if (column.DisplayFormat == null)
{
@(column.ValueConverter(propertyValue))
}
else
{
@(string.Format(column.DisplayFormat, column.ValueConverter(propertyValue)))
}
}
else
{
@(string.Format(column.DisplayFormat, propertyValue))
if (column.DisplayFormat == null)
{
@(propertyValue)
}
else
{
@(string.Format(column.DisplayFormat, propertyValue))
}
}
}
}
</DisplayTemplate>
</DataGridColumn>
}
}
}
}

@ -7,6 +7,7 @@ using JetBrains.Annotations;
using Volo.Abp.AspNetCore.Components.Extensibility.TableColumns;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Localization;
namespace Volo.Abp.BlazoriseUI.Components
{
@ -31,6 +32,9 @@ namespace Volo.Abp.BlazoriseUI.Components
[Parameter] public IEnumerable<TableColumn> Columns { get; set; }
[Inject]
public IStringLocalizerFactory StringLocalizerFactory { get; set; }
protected virtual RenderFragment RenderCustomTableColumnComponent(Type type, object data)
{
return (builder) =>

@ -0,0 +1,33 @@
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Localization;
namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
{
public static class EnumHelper
{
public static string GetLocalizedMemberName(Type enumType, object value, IStringLocalizerFactory stringLocalizerFactory)
{
var memberName = enumType.GetEnumName(value);
var localizedMemberName = AbpInternalLocalizationHelper.LocalizeWithFallback(
new[]
{
stringLocalizerFactory.CreateDefaultOrNull()
},
new[]
{
$"Enum:{enumType.Name}.{memberName}",
$"{enumType.Name}.{memberName}",
memberName
},
memberName
);
return localizedMemberName;
}
}
}

@ -4,7 +4,7 @@
<Field>
<FieldLabel>@PropertyInfo.GetLocalizedDisplayName(StringLocalizerFactory)</FieldLabel>
<Select TValue="int" SelectedValue="@SelectedValue" SelectedValueChanged="@SelectedValueChanged">
<Select TValue="int" @bind-SelectedValue="@SelectedValue">
@foreach (var item in GetSelectItemsFromEnum())
{
<SelectItem Value="@item.Value">@item.Text</SelectItem>

@ -11,27 +11,18 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
public partial class SelectExtensionProperty<TEntity, TResourceType> : ComponentBase
where TEntity : IHasExtraProperties
{
[Inject]
public IStringLocalizerFactory StringLocalizerFactory { get; set; }
[Inject] public IStringLocalizerFactory StringLocalizerFactory { get; set; }
[Parameter]
public TEntity Entity { get; set; }
[Parameter] public TEntity Entity { get; set; }
[Parameter]
public ObjectExtensionPropertyInfo PropertyInfo { get; set; }
[Parameter] public ObjectExtensionPropertyInfo PropertyInfo { get; set; }
public int SelectedValue
{
get
{
return Entity.GetProperty<int>(PropertyInfo.Name);
}
set
{
Entity.SetProperty(PropertyInfo.Name, value);
}
get { return Entity.GetProperty<int>(PropertyInfo.Name); }
set { Entity.SetProperty(PropertyInfo.Name, value); }
}
protected virtual IEnumerable<SelectItem<int>> GetSelectItemsFromEnum()
{
var isNullableType = Nullable.GetUnderlyingType(PropertyInfo.Type) != null;
@ -45,37 +36,21 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
foreach (var enumValue in enumType.GetEnumValues())
{
var memberName = enumType.GetEnumName(enumValue);
var localizedMemberName = AbpInternalLocalizationHelper.LocalizeWithFallback(
new[]
{
// containerLocalizer,
StringLocalizerFactory.CreateDefaultOrNull()
},
new[]
{
$"Enum:{enumType.Name}.{memberName}",
$"{enumType.Name}.{memberName}",
memberName
},
memberName
);
yield return new SelectItem<int>
{
Value = (int)enumValue,
Text = localizedMemberName
Value = (int) enumValue,
Text = EnumHelper.GetLocalizedMemberName(enumType, enumValue, StringLocalizerFactory)
};
}
}
protected virtual void SelectedValueChanged(int value)
protected override void OnInitialized()
{
SelectedValue = value;
SelectedValue = 0;
}
}
public class SelectItem<TValue>
{
public string Text { get; set; }

Loading…
Cancel
Save